jeff-hughes/shellcaster

Limit to single simultaneous play

Opened this issue · 2 comments

Hello. Is it possible to limit to amount of started instances of player to single one? So I don't need to terminate (using htop) when I start many of them.

Start episode 1 (run player). Start episode 2 (if instance of player exist then terminate previous and start with file #2).

In my case player is mpv.

Given how shellcaster is set up, right now it's entirely agnostic to what command is used to play the media files. So there's not a particularly easy way to build this sort of thing in, though I can appreciate how it would be useful.

I was going to suggest changing the play_command option in the config file to something like "pkill mpv && mpv %s". However, when testing it I realized that I don't have it set up in a way where it properly interprets the &&. (It just treats mpv && mpv %s as arguments to pkill.)

I'll add it to the list of things to add; I think it would be useful to have the ability to set up some chains of commands like this.

Hey! Yes, that is possible. Here is part of a script I use I save it as toggle_mpv_socket:

#!/usr/bin/env bash

# socat, mpv, jq

SOCKETNAME="/tmp/mpvsocket"

_toggle(){
    echo '{"command":["cycle","pause"]}' \
        | socat - "$SOCKETNAME" >/dev/null
}
_quit(){
    echo '{"command":["quit"]}' \
        | socat - "$SOCKETNAME" >/dev/null
}

_is_playing(){
    PATH=$(echo '{"command": ["get_property", "path"]}' \
    | socat - "$SOCKETNAME" \
    | jq '.data')

    # remove quotes
    PATH="${PATH%\"}"
    PATH="${PATH#\"}"

    if [ "$PATH" = "$1" ];then
        echo 0
    else
        echo 1
    fi
}

_start_mpv() {
    mpv \
    --x11-name=mpv_shellcaster \
    --title=mpv_shellcaster \
    --input-ipc-server="$SOCKETNAME" \
    "$1" \
    &
}

main() {
if [[ $(_is_playing "$1") = 0 ]];then
    _toggle
else
    _quit
    _start_mpv "$1"
fi
}

main "$@"

Now we can change the play command to play_command= "toggle_mpv_socket %s"

That way, if you hit play on the same podcast it toggles between play and pause, if you start a different podcast it kills the specific mpv instance and starts a new one. I also suggest adding saving the position to your configuration, that way if you go back to a podcast you already started it starts playing where you left off.

Sadly in addition to socat and mpv, this depends on jq, but that could be mitigated. I just found it the easiest solution.