Commit 96c3dbda authored by Jonas Jelten's avatar Jonas Jelten

only reenable mpd when it was playing before

fixes #6 and fixes #7
parent 29a3bb96
......@@ -10,6 +10,14 @@ Another url terminates the previous link.
Optionally, `mpd` can be paused during playback when using `-m`.
Dependencies
------------
* Python3
* [`mpv` player](https://mpv.io/)
* [`mpc` client](http://www.musicpd.org/clients/mpc/) (optional)
Invocation
----------
......
......@@ -5,6 +5,7 @@ The video screen.
import asyncio
import subprocess
from .mpd import MPD
from .player import Player
from .urlreceiver import UrlReceiver
......@@ -24,8 +25,8 @@ class VideoScreen:
self.mpv_options = args.options[1:]
self.control_mpd = args.music
self.sock = None
self.mpd = MPD(args.mpdhost, args.mpdport)
self.mpd_was_playing = False
def display(self, sender, data):
"""
......@@ -35,7 +36,6 @@ class VideoScreen:
print("%s showing '%s'" % (sender, data))
# kill the existing mpv:
if self.mpv is not None:
self.mpv.kill()
self.mpv.join()
......@@ -47,14 +47,12 @@ class VideoScreen:
def on_player_launch(self):
""" what to do right before the player starts """
if self.control_mpd:
print("pausing mpd...")
subprocess.call(["mpc", "pause"])
self.mpd_was_playing = self.mpd.pause()
def on_player_terminate(self):
""" what to do when the videoscreen player terminates """
if self.control_mpd:
print("resuming mpd...")
subprocess.call(["mpc", "play"])
if self.control_mpd and self.mpd_was_playing:
self.mpd.play()
def launch(self):
""" run the videoscreen """
......
......@@ -18,6 +18,10 @@ def main():
help="ip to listen on")
cli.add_argument("-m", "--music", action="store_true",
help="control mpd player by pausing and unpausing")
cli.add_argument("--mpdhost", default=None,
help="server where mpd is reached")
cli.add_argument("--mpdport", default=None,
help="port where mpd is reached")
cli.add_argument("options", default="", nargs=argparse.REMAINDER,
help="mpv options")
......
"""
mpd interaction
"""
import subprocess
class MPD:
"""
represents the mpd state
"""
def __init__(self, host=None, port=None):
self.host = host
self.port = port
def is_running(self):
""" test if mpd is playing """
try:
# parse the "[state]..." mpc output
output = self.run_cmd("status")
statusline = output.split(b"\n")[1]
stoppos = statusline.find(b"]")
status = statusline[1:stoppos]
return status in {b"playing"}
except IndexError:
print("failed to check mpd status, assume stopped.")
return False
def pause(self):
"""
pause the playback
return if mpd was playing before the command
"""
running = self.is_running()
if running:
print("pausing mpd...")
self.run_cmd("pause")
return running
def play(self):
"""
resume the playback
return if mpd was playing before the command
"""
running = self.is_running()
if not running:
print("playing mpd...")
self.run_cmd("play")
return running
def run_cmd(self, cmd):
""" run a mpd control command """
call = ["mpc"]
if self.host:
call.extend(["--host", self.host])
if self.port:
call.extend(["--port", self.port])
call.append(cmd)
proc = subprocess.Popen(call, stdout=subprocess.PIPE)
output, _ = proc.communicate()
try:
proc.wait(1)
except subprocess.TimeoutExpired:
raise Exception("mpc doesn't terminate!")
return output
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment