Win*_*son 2 media multithreading wxpython blocking media-player
我正在开展一个学校项目,在python平台上开发一个定制的媒体播放器.问题是当我使用time.sleep(duration)时,它会阻止我的GUI的主循环,阻止它更新.我咨询了我的主管,并被告知要使用多线程,但我不知道如何使用线程.有人会建议我如何在下面的场景中实现线程吗?
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) #THIS IS THE PROBLEM#
Run Code Online (Sandbox Code Playgroud)
你可能想看看wxPython wiki,它有几个使用线程,队列和其他有趣的东西的例子:
我还在这里写了一个关于这个主题的教程:http://www.blog.pythonlibrary.org/2010/05/22/wxpython-and-threads/
要记住的主要事情是,当你使用线程时,你不能直接调用你的wx方法(即myWidget.SetValue等).相反,您需要使用其中一个wxPython线程安全方法:wx.CallAfter,wx.CallLater或wx.PostEvent
| 归档时间: |
|
| 查看次数: |
6127 次 |
| 最近记录: |