如何在python中使用gstreamer进行搜索?

时间:2013-05-17 09:44:27

标签: python gstreamer seek

我用python制作了一个简单的音频播放器但是我的搜索不起作用。下面,你看到我的代码。我大多在http://pygstdocs.berlios.de/pygst-tutorial/seeking.html的教程中接受了建议。

import pygst
import threading
import glib
import gst
import gobject
import os.path


class Player(object):
    """plays sounds"""
    def __init__(self):
        super(Player, self).__init__()
        self.playing_entry = None
        gobject.threads_init()
        self.glib_main_loop = glib.MainLoop()
        self.glib_main_loop_thread = threading.Thread(target=self.glib_main_loop.run)
        self.glib_main_loop_thread.start()
        self.player = gst.element_factory_make("playbin2", "podpyplayer")
        fakesink = gst.element_factory_make("fakesink", "fakesink")
        self.player.set_property("video-sink", fakesink)
        bus = self.player.get_bus()
        bus.add_signal_watch()
        bus.connect("message", self.on_message)

    def play(self, entry):
        self.playing_entry = entry
        self.player.set_property("uri", "file://" + os.path.abspath(entry.path()))
        self.player.seek_simple(gst.FORMAT_TIME, gst.SEEK_FLAG_FLUSH, entry.seek)
        self.player.set_state(gst.STATE_PLAYING)
        while True:
            try:
                duration = self.player.query_duration(gst.FORMAT_TIME, None)[0]
                break
            except gst.QueryError:
                pass
        self.playing_entry.duration = duration
        # self.player.seek_simple(gst.FORMAT_TIME, gst.SEEK_FLAG_FLUSH, entry.seek)
        # neither worked

    def stop(self):
        self.player.set_state(gst.STATE_NULL)

    def update(self):
        if not self.playing_entry is None:
            pos_int = self.player.query_position(gst.FORMAT_TIME, None)[0]
            self.playing_entry.seek = pos_int

    def on_message(self, bus, message):
        t = message.type
        if t == gst.MESSAGE_EOS or t == gst.MESSAGE_ERROR:
            self.playing_entry = None
            self.player.set_state(gst.STATE_NULL)

if __name__ == '__main__':
    import time
    import sys

    class DummyEntry(object):
        """docstring for DummyEntry"""
        def __init__(self):
            super(DummyEntry, self).__init__()
            self.seek = 4995284000

        def path(self):
            return sys.argv[1]

    player = Player()
    entry = DummyEntry()
    player.play(entry)
    for x in xrange(1, 10):
        time.sleep(1)
        player.update()
        print entry.seek, entry.duration
    player.stop()
    print "stopped"

我希望你能帮助我。

0 个答案:

没有答案