Esempio n. 1
0
 def __init__(self):
     self.playlist = Playlist()
     self._mpv = mpv.Context(ytdl=True)
     self._mpv.set_option('video', 'no')
     self._mpv.set_option('pause', True)
     self._mpv.initialize()
     self._mpv.set_wakeup_callback(self._event_cb)
Esempio n. 2
0
def test_delete_next_when_none_playing():
    playlist = Playlist()
    playlist.add('123')
    playlist.add('456')
    assert playlist.current_path is None
    playlist.delete(0)
    playlist.jump_next()
    assert playlist.current_path == '456'
    playlist.jump_next()
    assert playlist.current_path == '456'
Esempio n. 3
0
def test_next():
    playlist = Playlist()
    playlist.add('123')
    playlist.add('456')
    assert playlist.current_path is None
    playlist.jump_next()
    assert playlist.current_path == '123'
    playlist.jump_next()
    assert playlist.current_path == '456'
    playlist.jump_next()
    assert playlist.current_path == '123'
Esempio n. 4
0
def test_delete_next_when_playing_earlier():
    playlist = Playlist()
    playlist.add('123')
    playlist.add('456')
    playlist.add('789')
    playlist.jump_next()
    playlist.jump_next()
    assert playlist.current_path == '456'
    playlist.delete(0)
    assert playlist.current_path == '456'
    playlist.jump_next()
    assert playlist.current_path == '789'
    playlist.jump_next()
    assert playlist.current_path == '456'
Esempio n. 5
0
def test_delete_prev_when_playing_deleted():
    playlist = Playlist()
    playlist.add('123')
    playlist.add('456')
    playlist.add('789')
    playlist.jump_next()
    playlist.jump_next()
    assert playlist.current_path == '456'
    playlist.delete(1)
    assert playlist.current_path == '456'
    playlist.jump_prev()
    assert playlist.current_path == '123'
    playlist.jump_prev()
    assert playlist.current_path == '789'
Esempio n. 6
0
class State:
    def __init__(self):
        self.playlist = Playlist()
        self._mpv = mpv.Context(ytdl=True)
        self._mpv.set_option('video', 'no')
        self._mpv.set_option('pause', True)
        self._mpv.initialize()
        self._mpv.set_wakeup_callback(self._event_cb)

    @property
    def path(self) -> Optional[str]:
        try:
            return self._mpv.get_property('path')
        except mpv.MPVError:
            return None

    @property
    def time_pos(self) -> Optional[int]:
        try:
            return self._mpv.get_property('time-pos')
        except mpv.MPVError:
            return None

    @property
    def duration(self) -> Optional[int]:
        try:
            return self._mpv.get_property('duration')
        except mpv.MPVError:
            return None

    @property
    def metadata(self) -> Optional[Dict]:
        try:
            return self._mpv.get_property('metadata')
        except mpv.MPVError:
            return None

    @property
    def pause(self) -> bool:
        return self._mpv.get_property('pause')

    @pause.setter
    def pause(self, value: bool):
        self._mpv.set_property('pause', value)

    @property
    def volume(self) -> float:
        return self._mpv.get_property('volume')

    @volume.setter
    def volume(self, value: float):
        self._mpv.set_property('volume', value)

    def seek(self, origin: str, mode: Optional[str] = None):
        self._mpv.command('seek', origin, mode)

        def wait_for_seek() -> None:
            # XXX: super lame
            import time
            time.sleep(0.1)

        wait_for_seek()

    def play(self, file: str):
        self._mpv.command('loadfile', file)
        self.pause = False

        def wait_for_file_load() -> None:
            # XXX: super lame
            import time
            time.sleep(0.1)

        wait_for_file_load()

    def stop_playback(self) -> None:
        self._mpv.command('playlist-clear')
        self.pause = True

        def wait_for_file_end() -> None:
            # XXX: super lame
            import time
            time.sleep(0.1)

        wait_for_file_end()

    def _event_cb(self) -> None:
        while self._mpv:
            event = self._mpv.wait_event(.01)
            if event.id == mpv.Events.none:
                break

            if event.id == mpv.Events.end_file \
                    and event.data.reason in (
                        MPV_END_FILE_REASON_EOF,
                        MPV_END_FILE_REASON_ERROR):
                self._next_file()

    def _next_file(self) -> None:
        try:
            self.playlist.jump_next()
        except ValueError:
            self.stop_playback()
            logging.info('No more files to play')
            return
        logging.info('Playing next file (%s)...', self.playlist.current_path)
        self.play(self.playlist.current_path)