Changeset - 1082f0725c32
[Not reviewed]
default
0 1 0
drewp@bigasterisk.com - 8 months ago 2024-05-28 22:34:03
drewp@bigasterisk.com
fix PlayerState semantics
1 file changed with 6 insertions and 4 deletions:
0 comments (0 inline, 0 general)
src/light9/ascoltami/player.py
Show inline comments
 
@@ -44,55 +44,57 @@ class Player:
 
        self.playerState: PlayerState = PlayerState()
 

	
 
        self._lastWatchTime = 0
 
        self._autoStopTime = 0
 
        self._lastSetSongUri: GstFileUri | None = None
 

	
 
        # task.LoopingCall(self._watchTime).start(.050)
 
        asyncio.create_task(self._watchTime())
 

	
 
        #bus = self.pipeline.get_bus()
 
        # not working- see notes in pollForMessages
 
        #self._watchForMessages(bus)
 

	
 
    async def _watchTime(self):
 
        while True:
 
            now = time.time()
 
            try:
 
                self._pollForMessages()
 
                t = self.currentTime()
 
                # log.debug("watch %s < %s < %s", self._lastWatchTime, self._autoStopTime, t)
 
                if self._lastWatchTime < self._autoStopTime < t:
 
                    log.info("autostop")
 
                    self.pause()
 

	
 
                eos = t >= self.duration() - .1  #todo
 
                playing = self.isPlaying() and not eos
 
                ps = PlayerState(
 
                    song=self._getSongFileUri(),
 
                    duration=round(self.duration(), 2),
 
                    wallStartTime=round(now - t, 2) if self.isPlaying() else None,
 
                    playing=self.isPlaying(),
 
                    pausedSongTime=None if self.isPlaying() else t,
 
                    endOfSong=t >= self.duration() - .1,  #todo
 
                    wallStartTime=round(now - t, 2) if playing else None,
 
                    playing=playing,
 
                    pausedSongTime=None if playing else t,
 
                    endOfSong=eos,
 
                )
 

	
 
                if self.playerState != ps:
 
                    self.onStateChange(ps)
 
                    self.playerState = ps
 

	
 
                self._lastWatchTime = t
 
            except Exception:
 
                traceback.print_exc()
 
            await asyncio.sleep(0.5)
 

	
 
    def _watchForMessages(self, bus):
 
        """this would be nicer than pollForMessages but it's not working for
 
        me. It's like add_signal_watch isn't running."""
 
        bus.add_signal_watch()
 

	
 
        def onEos(*args):
 
            print("onEos", args)
 
            if self._onEOS is not None:
 
                self._onEOS(self._getSongFileUri())
 

	
 
        bus.connect('message::eos', onEos)
 

	
 
        def onStreamStatus(bus, message):
0 comments (0 inline, 0 general)