-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmpv-play-pause
executable file
·47 lines (44 loc) · 1.34 KB
/
mpv-play-pause
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env bash
# keeps track of which sockets were recently
# paused by this script
#
# if that socket can be resumed, does that
# else, tries to look for another paused
# mpv instance and resumes that
readonly last_playing="${TMPDIR:-/tmp}/mpv-recently-paused"
declare -a currently_playing all_sockets
toggle() {
mpv-communicate "$1" 'cycle pause'
return $?
}
IFS=$'\n'
# if something is playing, stop it and link it to /tmp/mpv-recently-paused
# shellcheck disable=SC2207
if currently_playing=($(mpv-currently-playing --socket)); then
rm -f "${last_playing}"
# get the most recently launched currently playing mpv instance
most_recent="${currently_playing[-1]}"
echo "Pausing ${most_recent}..."
toggle "${most_recent}" || exit $?
ln -s "${most_recent}" "${last_playing}"
else
# nothing is playing, try to start '/tmp/mpv-recently-paused' if that mpv instance is still active
if toggle "${last_playing}"; then
echo 'Found recently paused socket, resuming...'
else
echo "Couldn't start a recently paused socket..." >&2
echo 'Starting the most recent mpv instance, if one exists...'
if all_sockets=($(mpv-active-sockets)); then
most_recent="${all_sockets[-1]}"
if toggle "${most_recent}"; then
echo 'Success!'
else
echo 'Failed...' >&2
exit 1
fi
else
echo 'No active mpv instances... exiting' >&2
exit 1
fi
fi
fi