0

i'm working on a school project regarding making a customized media player. I've got some source code on the web which i have been using. I wanted to add in another new feature which is to make a playlist which the source code does not have.

However i encounter an error whereby the window "stop responding" when i try to drag the window around. I wasn't able to click on anything as it seem like there is some background tread running because my cursor show the "loading sign" (Circular cursor).

I have tried leaving it running without dragging it and it seems to work properly.

Do anyone of you happens to know why is this problem occuring when i uses the function "time.sleep(second)"?

Reference: http://www.blog.pythonlibrary.org/2010/07/24/wxpython-creating-a-simple-media-player/

Logic (code):

def load_playlist(self, event):
    playlist = ["D:\Videos\test1.mp4", "D:\Videos\test2.avi"]
    for path in playlist:
        #calculate each media file duration
        ffmpeg_command = ['C:\\MPlayer-rtm-svn-31170\\ffmpeg.exe', '-i' , path]

        pipe = subprocess.Popen(ffmpeg_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        results = pipe.communicate()

        #Regular expression to get the duration
        length_regexp = 'Duration: (\d{2}):(\d{2}):(\d{2})\.\d+,'
        re_length = re.compile(length_regexp)

        # find the matches using the regexp that to compare with the buffer/string
        matches = re_length.search(str(results))
        #print matches

        hour = matches.group(1)
        minute = matches.group(2)
        second = matches.group(3)

        #Converting to second
        hour_to_second = int(hour) * 60 * 60
        minute_to_second = int(minute) * 60
        second_to_second = int(second)

        num_second = hour_to_second + minute_to_second + second_to_second
        print num_second

        #Play the media file
        trackPath = '"%s"' % path.replace("\\", "/")
        self.mplayer.Loadfile(trackPath)

        #Sleep for the duration of second(s) for the video before jumping to another video
        time.sleep(num_second)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Winson
  • 35
  • 2
  • 11

1 Answers1

0

The problem is that time.sleep() blocks wxPython's main loop so it cannot update, thus it appears unresponsive. If you need to insert a break between videos, then you should use a wx.Timer instead. Otherwise, you'll need to look into using threads.

Here's a tutorial on wx.Timer: http://www.blog.pythonlibrary.org/2009/08/25/wxpython-using-wx-timers/

You would basically create a timer, turn it on at the end of the end of your method and when it finishes, it will fire an event which you can use to load the next video. Or you can use wx.CallLater(numOfMillSec, self.loadVideo)

Mike Driscoll
  • 32,629
  • 8
  • 45
  • 88
  • I'm still not quite sure how to use wx.timer() to help in delaying my loop until the current video play finish. I've search on other post where people are using wx.CallAfter() & wx.CallLater() but wasn't sure how to implement those too. Do you have like an example to illustrate that it will wait for a certain time before it do another loop again? @Mike Driscoll – Winson Dec 10 '12 at 08:35
  • I linked to a tutorial and what not – Mike Driscoll Dec 10 '12 at 14:35