Skip to main content
 首页 » 编程设计

python之如何在 wx.media 中播放 mp4 文件

2024年11月01日10EasonJim

我想做一个 mp4 播放器,我试图让模块播放视频,但在给它的那一刻没有发生错误

def prueba(self,z): 
    self.open = wx.media.MediaCtrl(panel) 
    self.open.Load("video.mp4") 
    self.open.GetBestSize() 
    self.open.Play() 
panel = wx.Panel(frame,id=-1,size=(500,500)) 
self.Bind(wx.media.EVT_MEDIA_LOADED,self.prueba) 

编辑:

当我尝试以下代码时:

import wx 
import wx.media 
 
class TestPanel(wx.Frame): 
    def __init__(self): 
        wx.Frame.__init__(self, None) 
        self.testMedia = wx.media.MediaCtrl(self, style=wx.SIMPLE_BORDER,) 
        self.media = '/home/rolf/BBB.ogv' 
        self.testMedia.Bind(wx.media.EVT_MEDIA_LOADED, self.play) 
        self.testMedia.Bind(wx.media.EVT_MEDIA_FINISHED, self.quit) 
        if self.testMedia.Load(self.media): 
            pass 
        else: 
            print("Media not found") 
            self.quit(None) 
 
    def play(self, event): 
        self.testMedia.Play() 
 
    def quit(self, event): 
        self.Destroy() 
 
if __name__ == '__main__': 
    app = wx.App() 
    Frame = TestPanel() 
    Frame.Show() 
    app.MainLoop() 

用一个mp4文件...

ERROR

请您参考如下方法:

您需要在事件 EVT_MEDIA_LOADED 触发之前加载媒体。
试试这个作为骨架程序:

import wx 
import wx.media 
 
class TestPanel(wx.Frame): 
    def __init__(self): 
        wx.Frame.__init__(self, None) 
        self.testMedia = wx.media.MediaCtrl(self, style=wx.SIMPLE_BORDER,) 
        self.media = '/home/rolf/BBB.ogv' 
        self.testMedia.Bind(wx.media.EVT_MEDIA_LOADED, self.play) 
        self.testMedia.Bind(wx.media.EVT_MEDIA_FINISHED, self.quit) 
        if self.testMedia.Load(self.media): 
            pass 
        else: 
            print("Media not found") 
            self.quit(None) 
 
    def play(self, event): 
        self.testMedia.Play() 
 
    def quit(self, event): 
        self.Destroy() 
 
if __name__ == '__main__': 
    app = wx.App() 
    Frame = TestPanel() 
    Frame.Show() 
    app.MainLoop() 

根据评论编辑
上面的代码适用于 Linux。

如果您使用的是不同的操作系统,或者如果您使用的是 Linux 但没有安装 Gstreamer,则可能会遇到问题。
有一个选项可以选择您的媒体后端:

Generally, you should almost certainly leave this part up to wx.media.MediaCtrl - but if you need a certain backend for a particular reason, such as QuickTime for playing .mov files, all you need to do to choose a specific backend is to pass the name of the backend class to wx.media.MediaCtrl.Create . The following are valid backend identifiers:

MEDIABACKEND_DIRECTSHOW: Use ActiveMovie/DirectShow. Uses the native ActiveMovie (I.E. DirectShow) control. Default backend on Windows and supported by nearly all Windows versions, even some Windows CE versions. May display a windows media player logo while inactive. MEDIABACKEND_QUICKTIME: Use QuickTime. Mac Only. WARNING: May not working correctly embedded in a wx.Notebook. MEDIABACKEND_GSTREAMER, Use GStreamer. Unix Only. Requires GStreamer 0.8 along with at the very least the xvimagesink, xoverlay, and gst-play modules of gstreamer to function. You need the correct modules to play the relevant files, for example the mad module to play mp3s, etc. MEDIABACKEND_WMP10, Uses Windows Media Player 10 (Windows only) - works on mobile machines with Windows Media Player 10 and desktop machines with either Windows Media Player 9 or 10.

这将导致您的媒体定义如下所示:

self.testMedia = wx.media.MediaCtrl(self, style=wx.SIMPLE_BORDER,szBackend=wx.media.MEDIABACKEND_WMP10) 

例如。