我想用gst创建一个播放音乐的播放器类。这是代码:
from gi.repository import GObject
GObject.threads_init()
import gst
class PlayerControl:
def __init__(self, main_window):
self.player = gst.element_factory_make("playbin2", "player")
self.sink = gst.element_factory_make("autoaudiosink", "player Output")
self.videosink= gst.element_factory_make("fakesink", "player fake Output")
self.player.set_property("video-sink", self.videosink)
self.player.set_property("audio-sink", self.sink)
self.player_bus = self.player.get_bus()
self.player_bus.add_signal_watch()
self.player_bus.connect("message", self.on_messages)
self.player.connect("about-to-finish", self.on_finish)
self.main_window = main_window
self.main_window.seek_bar.connect("value-changed", self.on_slider_changed)
self.main_window.seek_bar.set_range(0, 100)
self.playing = False
def on_messages(self, bus, message):
print(message)
def play(self, file):
self.playing = True
self.player.set_state(gst.STATE_READY)
self.player.set_property("uri", "file:///"+str(file)) # örnek olsun diye
self.player.set_state(gst.STATE_PLAYING)
GObject.timeout_add(100, self.update_slider)
[...]
我从另一个这样的类中调用它来播放声音:
from file import PlayerControl
class PlayerExample():
def __init__( self ):
self.player_gst = PlayerControl(self)
[...]
def play(self, file):
self.playing = True
self.player.set_state(gst.STATE_READY)
self.player.set_property("uri", "file:///"+str(file))
self.player.set_state(gst.STATE_PLAYING)
GObject.timeout_add(100, self.update_slider)
[...]
我已经连接了gtk按钮来播放功能。当我点击按钮时,我会segmentation fault
没有任何信息。
此外,如果我从我的PlayerControl类中删除self.player_bus.add_signal_watch()
和self.player.connect("about-to-finish", self.on_finish)
行,它会工作并播放声音,但我需要使用“即将完成”信号调用on_finished函数。
为什么它不起作用?
答案 0 :(得分:0)
GObject.threads_init()
不适用于pygst“0.10”
你需要添加:
import gobject
gobject.threads_init()
import gst
播放器类的顶部。 或者使用Gst-1.0 https://wiki.ubuntu.com/Novacut/GStreamer1.0