commit 0ef70cefb10db474ab4984a0e941c0a5a75f9f85
parent a9e43ad70ec93b7820b80e4f4c2be828471aee22
Author: Paco Esteban <paco@e1e0.net>
Date: Sun, 27 Jun 2021 19:01:46 +0200
add super basic 1 song play on enter
Diffstat:
3 files changed, 50 insertions(+), 43 deletions(-)
diff --git a/subclient/playback.py b/subclient/playback.py
@@ -1,38 +0,0 @@
-from python_mpv_jsonipc import MPV
-import tempfile
-
-
-class Player:
- def __init__(self):
- self.mpv = MPV(ipc_socket='/tmp/subclient-mpv-socket',
- audio_display='no')
-
- def play(self, stream, callback):
- with tempfile.NamedTemporaryFile() as fp:
- while True:
- chunk = stream.read(512*1024)
- if not chunk:
- break
- fp.write(chunk)
- fp.seek(0)
- self.mpv.play(fp.name)
- self.mpv.wait_for_property('eof-reached')
- self.mpv.bind_event('end-file', callback)
-
- def exit(self):
- self.mpv.terminate()
-
- def is_paused(self):
- return self.mpv.pause
-
- def is_idle(self):
- return self.mpv.core_idle
-
- def set_pause(self, state):
- self.mpv.pause = state
-
- def stop(self):
- self.mpv.command('stop')
-
- def seek(self, duration='+5'):
- self.mpv.command('seek', duration)
diff --git a/subclient/player.py b/subclient/player.py
@@ -0,0 +1,41 @@
+from python_mpv_jsonipc import MPV
+import tempfile
+
+
+class Player:
+ def __init__(self, origin):
+ self.mpv = MPV(ipc_socket='/tmp/subclient-mpv-socket',
+ audio_display='no')
+ self.orig = origin
+
+ def play(self, playlist):
+ self.stop()
+ for s in playlist:
+ stream = self.orig.get_song_stream(s)
+ with tempfile.NamedTemporaryFile() as fp:
+ while True:
+ chunk = stream.read(512*1024)
+ if not chunk:
+ break
+ fp.write(chunk)
+ fp.seek(0)
+ self.mpv.play(fp.name)
+ self.mpv.wait_for_property('eof-reached')
+
+ def exit(self):
+ self.mpv.terminate()
+
+ def is_paused(self):
+ return self.mpv.pause
+
+ def is_idle(self):
+ return self.mpv.core_idle
+
+ def set_pause(self, state):
+ self.mpv.pause = state
+
+ def stop(self):
+ self.mpv.command('stop')
+
+ def seek(self, duration='+5'):
+ self.mpv.command('seek', duration)
diff --git a/subclient/subclient.py b/subclient/subclient.py
@@ -4,6 +4,7 @@
import configparser
import curses
from subclient import subsonic
+from subclient import player
import os
@@ -35,6 +36,8 @@ class SubClient:
self.subsonic = subsonic.Subsonic(self.config['subclient'])
+ self.player = player.Player(self.subsonic)
+
self.window = None
self.hight = 0
self.width = 0
@@ -67,6 +70,7 @@ class SubClient:
except KeyboardInterrupt:
pass
finally:
+ self.player.exit()
curses.endwin()
def input_loop(self):
@@ -90,12 +94,12 @@ class SubClient:
self.nav_in_out(self.IN)
elif c == 'h':
self.nav_in_out(self.OUT)
+ elif c == ' ':
+ self.player.set_pause(not self.player.is_paused())
elif c in [curses.KEY_ENTER, '\r', '\n']:
- with open('foo.txt', 'w') as f:
- print(f"top: {self.nav_top}", file=f)
- print(f"bottom: {self.nav_bottom}", file=f)
- print(f"selected: {self.nav_selected}", file=f)
- break
+ if self.nav_list_type == self.SONGS:
+ self.player.play(
+ [self.nav_list[self.nav_top + self.nav_selected]])
def load_list(self):
for i, t in enumerate(