我有这个代码:
import pygst
import st, pygtk
player_name = gst.element_make_factory("playbin", "Multimedia Player")
player_name.set_property("uri", "../media/alert.mp3")
player_name.set_state(gst.PLAYING)
它不断向我抛出以下错误:
player_name = gst.element_make_factory("playbin", "Multimedia Player")
AttributeError: 'module' object has no attribute 'element_make_factory'
有什么方法可以解决这个问题?为什么会发生这种情况?
如果我print gst
得到以下内容:
<module 'gst' from '/usr/lib/python2.7/dist-packages/gst-0.10/gst/__init__.pyc'>
所以它是一个模块!
答案1
这是一些工作代码...它仍在进行中(但它可以工作)。它应该有一个 glade 文件和一些按钮回调实现。不过 pygi Gst 部分可以工作。
'''
Example multimedia player
dependencies:
gir and GdkX11
adapted from here:
http://bazaar.launchpad.net/~jderose/+junk/gst-examples/view/head:/video-player-1.0
'''
#use gir if you're using quickly...or just use it anyway
from gi.repository import GObject, Gst, Gtk, GdkX11, GstVideo
import os, base64
class DemoFiles:
'''
Smashing our .glade file and our mp3 into our py
'''
def __init__(self, root):
self.root = root
self.glade = '' +\
''
self.testaudio = '' +\
''
def drop_files(self):
pass
class Settings:
'''
Using our home directory
'''
def __init__(self, root):
self.root = root
home = os.environ['HOME']
working = os.path.join(home, 'com.example.pygi.gst')
uri = os.path.join(working, 'makeitbetter.mp3')
glade = os.path.join(working, 'player.glade')
self.params = {
'HOME': home,
'WORKING': working,
'DEMOFILE': uri,
'GLADE': glade
}
def __call__(self, param):
return self.params[param]
def set_param(self, param, data):
self.params[param] = data
return True
class Handler:
'''
Callbacks for Glade
'''
def __init__(self, root):
self.root = root
pass
def on_file_button_clicked(self, *args):
pass
def on_file_ok_button_clicked(self, *args):
print args[0]
self.root.ui.gst.playbin.set_property('uri', 'file://' + args[0])
self.root.ui.gst.pipeline.set_state(Gst.State.PLAYING)
def on_file_cancel_button_clicked(self, *args):
pass
def on_main_win_delete_event(self, *args):
#clean up our pipeline
self.root.ui.gst.pipeline.set_state(Gst.State.NULL)
Gtk.main_quit()
class GstreamerStuff:
'''
All the gstreamer stuff
'''
def __init__(self, root):
self.root = root
#Threading init
GObject.threads_init()
Gst.init(None)
# GStreamer init
self.pipeline = Gst.Pipeline()
self.bus = self.pipeline.get_bus()
self.bus.add_signal_watch()
self.bus.connect('message::eos', self.on_eos)
self.bus.connect('message::error', self.on_error)
# This is needed to make the video output in our DrawingArea:
self.bus.enable_sync_message_emission()
self.bus.connect('sync-message::element', self.on_sync_message)
# Create GStreamer elements
self.playbin = Gst.ElementFactory.make('playbin', None)
# Add playbin to the pipeline
self.pipeline.add(self.playbin)
self.xid = ''
def on_sync_message(self, bus, msg):
if msg.get_structure().get_name() == 'prepare-window-handle':
msg.src.set_window_handle(self.xid)
def on_eos(self, bus, msg):
self.root.ui.update_status('Seeking to start of video')
self.pipeline.seek_simple(
Gst.Format.TIME,
Gst.SeekFlags.FLUSH | Gst.SeekFlags.KEY_UNIT,
0
)
def on_error(self, bus, msg):
self.root.ui.update_status(msg.parse_error())
def set_xid(self, xid):
self.xid = xid
class UI:
'''
User interface code
'''
def __init__(self, root):
self.root = root
#Handle Gtk setup
self.builder = Gtk.Builder()
self.handler = Handler(root)
#Load the glade file
self.builder.add_from_file(self.root.settings('GLADE'))
#Connect callbacks
self.builder.connect_signals(self.handler)
#Handle Gst setup
self.gst = GstreamerStuff(root)
def init(self):
self.show_main()
self.gst.set_xid(self.builder.get_object('main_drawing_area').get_property('window').get_xid())
def show_main(self):
self.builder.get_object('main_win').show_all()
def update_status(self, status):
print('Status bar update')
self.builder.get_object('status_bar')
class SamplePlayer:
'''
Meat
'''
def __init__(self):
#Settings instance
self.settings = Settings(self)
#Dump demo files
demo = DemoFiles(self)
demo.drop_files()
#UI Init - I put gstreamer in here
self.ui = UI(self)
def run(self):
self.ui.init()
print('Trying with: ' + self.settings('DEMOFILE'))
self.ui.handler.on_file_ok_button_clicked(self.settings('DEMOFILE'))
Gtk.main()
if __name__ == '__main__':
player = SamplePlayer()
player.run()
答案2
这个错误其实很简单:gst
模块没有element_make_factory
方法。请查看我的以下交互式会话以获取更多信息:
>>> import gst
>>> gst.element_make_factory
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'element_make_factory'
>>> gst.some_none_existent_method
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'some_none_existent_method'
>>> dir(gst)
['ACTIVATE_NONE', 'ACTIVATE_PULL', 'ACTIVATE_PUSH', 'ALLOC_TRACE_LIVE',
#### Snipping out lots of results...
'warnings', 'xml_make_element', 'xml_write', 'xml_write_file']
>>> 'element_make_factory' in dir(gst)
False
>>> 'element_factory_make' in dir(gst)
True
>>> gst.element_factory_make
<built-in function element_factory_make>
>>>