Changeset - fc14e2e87e9e
[Not reviewed]
default
0 1 0
drewp@bigasterisk.com - 15 years ago 2010-06-15 05:44:32
drewp@bigasterisk.com
add preload safety to ascoltami2
Ignore-this: e60dad9c288f05ed91acd2a2ed827389
1 file changed with 14 insertions and 0 deletions:
0 comments (0 inline, 0 general)
light9/ascoltami/player.py
Show inline comments
 
@@ -42,53 +42,67 @@ class Player(object):
 
                return True
 
            log.debug("watch %s < %s < %s",
 
                      self.lastWatchTime, self.autoStopTime, t)
 
            if self.lastWatchTime < self.autoStopTime < t:
 
                log.info("autostop")
 
                self.pause()
 
            self.lastWatchTime = t
 
        except:
 
            traceback.print_exc()
 
        return True
 

	
 
    def seek(self, t):
 
        assert self.playbin.seek_simple(
 
            gst.FORMAT_TIME,
 
            gst.SEEK_FLAG_FLUSH | gst.SEEK_FLAG_ACCURATE | gst.SEEK_FLAG_SKIP,
 
            t * gst.SECOND)
 
        self.playStartTime = time.time()
 

	
 
    def setSong(self, songUri):
 
        """
 
        uri like file:///my/proj/light9/show/dance2010/music/07.wav
 
        """
 
        log.info("set song to %r" % songUri)
 
        self.pipeline.set_state(gst.STATE_READY)
 
        self.preload(songUri)
 
        self.pipeline.set_property("uri", songUri)
 
        # todo: don't have any error report yet if the uri can't be read
 
        self.pipeline.set_state(gst.STATE_PLAYING)
 
        self.playStartTime = time.time()
 

	
 
    def preload(self, songUri):
 
        """
 
        to avoid disk seek stutters, which happened sometimes (in 2007) with the
 
        non-gst version of this program, we read the whole file to get
 
        more OS caching.
 

	
 
        i don't care that it's blocking.
 
        """
 
        assert songUri.startswith('file://')
 
        p = songUri[len('file://'):]
 
        log.info("preloading %s", p)
 
        open(p).read()
 

	
 
    def currentTime(self):
 
        try:
 
            cur, _format = self.playbin.query_position(gst.FORMAT_TIME)
 
        except gst.QueryError:
 
            return 0
 
        return cur / gst.SECOND
 

	
 
    def duration(self):
 
        try:
 
            return self.playbin.query_duration(gst.FORMAT_TIME)[0] / gst.SECOND
 
        except gst.QueryError:
 
            return 0
 
        
 
    def pause(self):
 
        self.pipeline.set_state(gst.STATE_PAUSED)
 

	
 
    def resume(self):
 
        self.pipeline.set_state(gst.STATE_PLAYING)
 
        pos = self.currentTime()
 
        autoStop = self.duration() - self.autoStopOffset
 
        if abs(pos - autoStop) < .01:
 
            self.releaseAutostop()
 

	
 
    def setupAutostop(self):
0 comments (0 inline, 0 general)