fix termux-media-player

This commit is contained in:
Tom Yan 2018-04-04 11:17:57 +08:00
parent 1686e053fd
commit 3714387eb3

View File

@ -1,103 +1,53 @@
#!/data/data/com.termux/files/usr/bin/bash #!/data/data/com.termux/files/usr/bin/bash
set -e -u set -e
SCRIPTNAME=termux-media-player SCRIPTNAME=termux-media-player
show_usage () { show_usage () {
echo "Usage: $SCRIPTNAME cmd [args]" echo "Usage: $SCRIPTNAME cmd [args]"
echo;
echo -e "help Shows this help"
echo -e "info Displays current playback information"
echo -e "play Resumes playback if paused"
echo -e "play <file> Plays specified media file"
echo -e "pause Pauses playback"
echo -e "stop Quits playback"
echo;
echo "--NOTE--"
echo "To playback media files they must reside in a publicly"
echo "accessible directory, such as /storage."
echo "Otherwise, they will fail to load!"
echo echo
echo "**Run 'termux-setup-storage' before using!**" echo "help Shows this help"
echo; echo "info Displays current playback information"
echo "play Resumes playback if paused"
echo "play <file> Plays specified media file"
echo "pause Pauses playback"
echo "stop Quits playback"
} }
if [ $# = 0 ]; then main() {
show_usage /data/data/com.termux/files/usr/libexec/termux-api MediaPlayer "$@"
exit 1
fi
# Params we're sending to MediaPlayerAPI
PARAMS=""
play_media () {
if [ $# -gt 1 ]; then
echo "Error! play only takes one argument which is the name of file to play"
exit 1
# If no arguments, we should resume the last played media file if it exists
elif [ $# -eq 0 ]; then
PARAMS="-a resume"
else
# Make sure we actually receive a file
if [ ! -f $1 ]; then
echo "Error: Not a file!"
exit 1
else
local file=$(readlink -q -f $1)
local PATTERN='.3gp|.flac|.mkv|.mp3|.ogg|.wav$'
# Ensure that file is a supported media file
if ! ls $file | grep -E $PATTERN -q; then
echo "Error: File is not a valid media file!"
exit 1
else
PARAMS="-a play --es file $file"
fi
fi
fi
} }
media_info () { case "$1" in
if [ $# -ne 0 ]; then "play" )
echo "Error! info takes no arguments!" if [ $# -gt 2 ]; then
exit 1 echo "Error! $SCRIPTNAME can only play one file at a time!"
fi exit 1
PARAMS="-a info" elif [ $# -gt 1 ]; then
} if [ ! -f "$2" ]; then
echo "Error: '$2' is not a file!"
pause_media () { exit 1
if [ $# -ne 0 ]; then else
echo "Error! pause takes no arguments!" main -a play --es file "$(realpath "$2")"
exit 1 fi
fi else
PARAMS="-a pause" main -a resume
} fi
;;
stop_media () { "info" | "pause" | "stop" )
if [ $# -ne 0 ]; then if [ $# -gt 1 ]; then
echo "Error! stop takes no arguments!" echo "Error! '$1' takes no arguments!"
exit 1 exit 1
fi else
PARAMS="-a stop" main -a "$1"
} fi
;;
# Get command and command args "help" | "" )
while [ $# -gt 0 ]; do show_usage
# MediaPlayer command ;;
cmd=$1 * )
echo "$SCRIPTNAME: Invalid cmd: '$1'"
# Command arguments show_usage
arg_array=( $@ ); exit 1
cmd_args=${arg_array[@]:1} ;;
esac
# Find correct command and execute w/ args
case $cmd in
"play" ) play_media $cmd_args; break ;;
"info" ) media_info $cmd_args; break ;;
"pause" ) pause_media $cmd_args; break ;;
"stop" ) stop_media $cmd_args; break ;;
"help" | * ) echo "$SCRIPTNAME: Invalid cmd: $cmd"; show_usage; exit 1 ;;
esac
done
/data/data/com.termux/files/usr/libexec/termux-api MediaPlayer $PARAMS