MediaCtrl on Linux doesn't load a file the first time

Hi everyone,

My MediaCtrl won't load a file on Linux unless I load it twice (or
uncomment the code in "do_load_file":

#!/usr/bin/python
import wx
import wx.media

class MediaPanel(wx.Frame):
    """
    A panel that contains a MediaCtrl for playing videos/audio, and
buttons for
    controlling it: open (file)/pause/stop/play, and a slider bar.
    """
    def __init__(self):
        wx.Frame.__init__(self, None)
        self.mc = wx.media.MediaCtrl(self, style=wx.SIMPLE_BORDER)
        self.open = wx.Button(self, label="open")
        self.play = wx.Button(self, label="play")
        self.pause = wx.Button(self, label="pause")
        self.stop = wx.Button(self, label="stop")
        self.slider = wx.Slider(self)
        self.slider.SetMinSize((150, -1))

        sizer = wx.GridBagSizer(5, 5)
        sizer.Add(self.mc, (1, 1), span=(5, 1))
        sizer.Add(self.open, (1, 3), flag=wx.RIGHT, border=10)
        sizer.Add(self.play, (2, 3), flag=wx.RIGHT, border=10)
        sizer.Add(self.pause, (3, 3), flag=wx.RIGHT, border=10)
        sizer.Add(self.stop, (4, 3), flag=wx.RIGHT, border=10)
        sizer.Add(self.slider, (6, 1), flag=wx.EXPAND)
        self.SetSizer(sizer)
        self.Layout()
        self.Fit()

        self.Bind(wx.media.EVT_MEDIA_LOADED, self.media_loaded)
        self.Bind(wx.media.EVT_MEDIA_STOP, self.media_stopped)
        self.Bind(wx.EVT_BUTTON, self.load_file, self.open)
        self.Bind(wx.EVT_BUTTON, self.on_play, self.play)
        self.Bind(wx.EVT_BUTTON, self.on_pause, self.pause)
        self.Bind(wx.EVT_BUTTON, self.on_stop, self.stop)
        self.Bind(wx.EVT_SLIDER, self.on_seek, self.slider)

    def load_file(self, evt):
        vids = "*.avi; *.mkv; *.mov; *.mpg; *ogg; *.wmv"
        audio = "*.mp3; *.oga; *.ogg; *.wav"
        wc = "Media Files"+" |%s;%s|" % (vids, audio)
        wc += "Video Files"+" (%s)|%s|" % (vids, vids)
        wc += "Audio Files"+" (%s)|%s" % (audio, audio)

        dlg = wx.FileDialog(self, message="Choose a media file",
                            wildcard=wc, style=wx.OPEN | wx.CHANGE_DIR )
        if dlg.ShowModal() == wx.ID_OK:
            self.do_load_file(dlg.GetPath())
        dlg.Destroy()

    def do_load_file(self, path):
        if not self.mc.Load(path):
            wx.MessageBox("Unable to load %s: Unsupported format?" % path,
                          "Error", wx.ICON_ERROR | wx.OK)
        #else:
        # self.mc.Load(path)

    def media_loaded(self, evt):
        self.mc.SetInitialSize()
        self.slider.SetRange(0, self.mc.Length())
        self.GetSizer().Layout()
        self.Fit()

    def media_stopped(self, evt):
        self.on_stop(None, True)

    def on_play(self, evt):
        if not self.mc.Play():
            wx.MessageBox("Unable to Play media : Unsupported format?",
                          "Error", wx.ICON_ERROR | wx.OK)

    def on_pause(self, evt):
        self.mc.Pause()

    def on_stop(self, evt, ignore=False):
        if not ignore:
            self.mc.Stop()
        self.slider.SetValue(0)

    def on_seek(self, evt):
        self.mc.Seek(self.slider.GetValue())

app = wx.App(False)
f = MediaPanel()
f.Show()
app.MainLoop()

The code is based upon my program's MediaPanel, which I've stripped down
to its bare essentials above (removing volume, button enable/disable
etc)...which was based upon the demo code. The demo code does not have
this issue.

Sometimes the file will load, because my calls to
self.mc.SetInitialSize() and self.Layout() will change the size of the
Frame, but pressing play does nothing. A solution seems to be to Load()
the media file twice, but I'm not sure of the internet details of it,
and how much more memory that uses etc.

Any ideas would be much appreciated. Thanks!

Sorry, forgot to add:

wxPython 2.8.9.10, python 2.6.2, ubuntu 9.04. It works fine, and loads the first time, on Windows (XP, SP3)

···

2009/12/5 Steven Sproat sproaty@gmail.com

Hi everyone,

Hi everyone,

My MediaCtrl won't load a file on Linux unless I load it twice (or
uncomment the code in "do_load_file":

The code is based upon my program's MediaPanel, which I've stripped down
to its bare essentials above (removing volume, button enable/disable
etc)...which was based upon the demo code. The demo code does not have
this issue.

What is different between your sample and the demo? (Look widgets, widget relationships, program flow, events handled, etc.) Is there anything different about the media or the runtime environment between the two?

Sometimes the file will load, because my calls to
self.mc.SetInitialSize() and self.Layout() will change the size of the
Frame, but pressing play does nothing. A solution seems to be to Load()
the media file twice, but I'm not sure of the internet details of it,
and how much more memory that uses etc.

If you ignore the False return value does it eventually load and send the _LOADED event?

···

On 12/4/09 8:29 PM, Steven Sproat wrote:

--
Robin Dunn
Software Craftsman

Robin Dunn wrote:

  

The code is based upon my program's MediaPanel, which I've stripped down
to its bare essentials above (removing volume, button enable/disable
etc)...which was based upon the demo code. The demo code does not have
this issue.
    
What is different between your sample and the demo? (Look widgets, widget relationships, program flow, events handled, etc.) Is there anything different about the media or the runtime environment between the two?

I'm using bitmap buttons for the pause/stop etc, have two statictexts: one for the filename, another for showing the time elapsed as xx:xx/xx:xx:xx, a volume slider and a timer which will periodically update the elapsed statictext. I've tried many files in both (mp3s, avi, wmv etc), including trying the same file in both the demo and my panel, but it seems the demo just works more frequently.

It did have the problem described in my quote below occasionally, but not as often as my panel.

Sometimes the file will load, because my calls to
self.mc.SetInitialSize() and self.Layout() will change the size of the
Frame, but pressing play does nothing. A solution seems to be to Load()
the media file twice, but I'm not sure of the internet details of it,
and how much more memory that uses etc.
    
If you ignore the False return value does it eventually load and send the _LOADED event?

I'll check it out when I'm on Linux next; so it could be an issue that the media just takes a little longer to load sometimes, and I don't allow it enough time?

···

On 12/4/09 8:29 PM, Steven Sproat wrote:

--
Steven Sproat, BSc
http://www.basicrpg.com/