Send pause action only if a media is playing !

This commit is contained in:
gardouille 2022-07-04 19:10:41 +02:00
parent 3a9cc468fa
commit c97f868ef2
Signed by: gardouille
GPG Key ID: E759BAA22501AF32
1 changed files with 73 additions and 4 deletions

View File

@ -5,7 +5,13 @@
# 1. Start Kodi if not already running.
# 2. Send user command to Kodi running instance.
#
# 2022-05-22
# In Kodi, pause|play|toggle = TOGGLE ! 😑
# So for a real PAUSE, some extra tests are required :
# * Request the API (with curl) to check if a playlist is active.
# * Request the API to get the media speed.
# * Exit if the media is already paused (speed = 0).
#
# 2022-07-04
# }}}
# Vars {{{
PROGNAME=$(basename "${0}"); readonly PROGNAME
@ -54,7 +60,8 @@ EXAMPLES:
OPTIONS:
-c,--command
Send a command to running Kodi. Available commands :
* toggle, play, pause
* toggle, play
* pause (real one thanks to the API and curl!)
* stop
* next
* previous
@ -120,9 +127,12 @@ define_vars() { # {{{
## }}}
## Translate user command to Kodi action {{{
case "${USER_MULTIMEDIA_COMMAND}" in
toggle|play|pause ) ## Toggle current play
toggle|play ) ## Toggle current play
KODI_ACTION="PlayerControl(play)"
;;
pause ) ## Pause current media if anything is playing
KODI_ACTION="PlayerControl(pause)"
;;
stop ) ## Stop current play
KODI_ACTION="PlayerControl(stop)"
;;
@ -146,7 +156,7 @@ define_vars() { # {{{
}
# }}}
is_proc_absent() { # {{{
is_proc_absent() { # {{{
local_proc_pattern="${1}"
@ -172,6 +182,43 @@ procs running (with the pattern: ${RED}${local_proc_pattern}${COLOR_DEBUG}) on t
}
# }}}
get_current_media_type() { # {{{
debug_message "get_current_media_type \
Run a request to ${KODI_HOST}:${KODI_PORT} API :"
KODI_CURRENT_MEDIA_TYPE=$(curl -s -X POST http://"${KODI_HOST}:${KODI_PORT}"/jsonrpc -H 'Content-Type: application/json' --data '{"jsonrpc":"2.0","method":"Player.GetActivePlayers","id":0}' | sed -n 's/.*"type":"\(.*\)".*/\1/p')
}
# }}}
is_audio_paused() { # {{{
## Media is playing by default
return_is_audio_paused="1"
debug_message "is_audio_paused \
Run a request to ${KODI_HOST}:${KODI_PORT} API to get status of audio media."
KODI_CURRENT_AUDIO_SPEED=$(curl -s -X POST http://"${KODI_HOST}:${KODI_PORT}"/jsonrpc -H 'Content-Type: application/json' --data '{"jsonrpc": "2.0", "method": "Player.GetProperties", "params": { "playerid": 0, "properties": [ "speed" ] }, "id": "AudioGetItem" }' | sed -n 's/.*"speed":\(.\).*/\1/p')
case "${KODI_CURRENT_AUDIO_SPEED}" in
0 ) ## Media is paused
debug_message "is_audio_paused \
The current media is paused (speed: ${RED}${KODI_CURRENT_MEDIA_TYPE}${COLOR_DEBUG})."
return_is_audio_paused="0"
;;
1 ) ## Media is playing
debug_message "is_audio_paused \
The current media is playing (speed: ${RED}${KODI_CURRENT_MEDIA_TYPE}${COLOR_DEBUG})."
return_is_audio_paused="1"
;;
* ) ## Error getting current media speed
error_message "Error with media speed (${KODI_CURRENT_AUDIO_SPEED})." 1
;;
esac
return "${return_is_audio_paused}"
}
# }}}
main() { # {{{
@ -187,6 +234,28 @@ main() { # {{{
&& exit 0
## }}}
## If the media should be paused {{{
if [ "${USER_MULTIMEDIA_COMMAND}" = "pause" ]; then
## Get current playlist type
get_current_media_type
case "${KODI_CURRENT_MEDIA_TYPE}" in
audio ) ## Manage audio media
is_audio_paused \
&& debug_message "${RED}audio${COLOR_DEBUG} media is already paused, no more action is required." \
&& exit 0
;;
video ) ## Manage video media
debug_message "${RED}video${COLOR_DEBUG} type for current media is not yet managed."
;;
* ) ## Unknown media type or no media at all
debug_message "Unknown media type ->${RED}${KODI_CURRENT_MEDIA_TYPE}${COLOR_DEBUG}<- or no media is playing (if empty), exit."
exit 0
;;
esac
fi
## }}}
## Send Kodi action to running instance {{{
### And exit
debug_message "Send ${RED}${USER_MULTIMEDIA_COMMAND}${COLOR_DEBUG} (${RED}${KODI_ACTION}${COLOR_DEBUG}) action to running Kodi instance (on ${RED}${KODI_HOST}${COLOR_DEBUG} host)." \