可以播放大部分的音視頻.
demo download: http://www.sandy1219.com/python/media.rar
playMP3.py
# -*- coding: utf-8 -*-<br />import wx;<br />import wx.media;<br />import os;<br />import SPrint;<br />import mediaStateBar;<br />import mediaList;<br />import SaveLog;<br />import MediaItem;<br />woldcart = "media files|*.*|avi|*.avi|rmvb|*.rmvb|rm|*.rm|wma|*.wma|mp3|*.mp3";<br />class MediaFrame(wx.Frame):<br /> def __init__(self):<br /> wx.Frame.__init__(self,None,wx.NewId(),u"媒體播放器",pos=wx.DefaultPosition,size=(500,500));<br /> self.media = '';<br /> try:<br /> self.media = wx.media.MediaCtrl(self,style=wx.SIMPLE_BORDER,<br /> #szBackend=wx.media.MEDIABACKEND_DIRECTSHOW<br /> #szBackend=wx.media.MEDIABACKEND_QUICKTIME<br /> #szBackend=wx.media.MEDIABACKEND_WMP10<br /> );<br /> self.media.Bind(wx.media.EVT_MEDIA_LOADED,self.mediaLoaded);<br /> self.media.Bind(wx.media.EVT_MEDIA_STATECHANGED,self.mediaStateChange)<br /> except NotImplementedError:<br /> self.Destroy()<br /> raise;<br /> self.listView = '';<br /> self.item = MediaItem.MediaItem();<br /> self.sb = mediaStateBar.MediaStateBar(self);<br /> self.SetStatusBar(self.sb);<br /> self.log = SaveLog.SaveLog();<br /> #self.log.test();<br /> self.menu = wx.Menu()<br /> self.openMenu = self.menu.Append(wx.NewId(),u"開啟檔案");<br /> self.Bind(wx.EVT_MENU,self._openFile,self.openMenu);<br /> self.quitMenu = self.menu.Append(wx.NewId(),u"退出");<br /> self.Bind(wx.EVT_MENU,self.quit,self.quitMenu);<br /> self.viewMenu = wx.Menu();<br /> self.listMenu = self.viewMenu.Append(wx.NewId(),u"開啟播放清單");<br /> self.Bind(wx.EVT_MENU,self.openList,self.listMenu);<br /> self.loadListMenu = self.viewMenu.Append(wx.NewId(),u"載入播放清單");<br /> self.Bind(wx.EVT_MENU,self.loadMediaList,self.loadListMenu);<br /> self.saveListMenu = self.viewMenu.Append(wx.NewId(),u"儲存播放清單")<br /> self.Bind(wx.EVT_MENU,self.saveMediaList,self.saveListMenu);<br /> menuBar = wx.MenuBar();<br /> menuBar.Append(self.menu,u"檔案")<br /> menuBar.Append(self.viewMenu,u"播放清單")<br /> self.SetMenuBar(menuBar);<br /> self.slider = wx.Slider(self,wx.NewId(), 0,0,100);<br /> self.Bind(wx.EVT_SLIDER,self.onSeek,self.slider);<br /> self.btnGroupSizer = wx.BoxSizer(wx.HORIZONTAL);<br /> self.btnGroupSizer.Add((5,5),0)<br /> self.playBtn = self.createButton("./pic/play.png",self.playMp3,u"播放");<br /> self.playBtn.Disable()<br /> self.btnGroupSizer.Add(self.playBtn);<br /> self.btnGroupSizer.Add((5,5),0)<br /> self.pauseBtn = self.createButton("./pic/pause.png",self.pauseMp3,u"暫停");<br /> self.pauseBtn.Disable()<br /> self.btnGroupSizer.Add(self.pauseBtn);<br /> self.btnGroupSizer.Add((5,5),0)<br /> self.stopBtn = self.createButton("./pic/stop.png",self.stopMp3,u"停止");<br /> self.stopBtn.Disable()<br /> self.btnGroupSizer.Add(self.stopBtn);<br /> self.btnGroupSizer.Add((5,5),0)<br /> self.preBtn = self.createButton("./pic/pre.png",self.preMp3,u"前一個");<br /> self.preBtn.Disable()<br /> self.btnGroupSizer.Add(self.preBtn);<br /> self.btnGroupSizer.Add((5,5),0)<br /> self.nextBtn = self.createButton("./pic/next.png",self.nextMp3,u"下一個");<br /> self.nextBtn.Disable()<br /> self.btnGroupSizer.Add(self.nextBtn);<br /> self.btnGroupSizer.Add((5,5),0)<br /> self.openBtn = self.createButton("./pic/media.png",self._openFile,u"開啟檔案");<br /> #self.openBtn.Disable()<br /> self.btnGroupSizer.Add(self.openBtn);<br /> self.btnGroupSizer.Add((5,5),0)<br /> self.listBtn = self.createButton("./pic/list.png",self.openList,u"開啟列表");<br /> #self.listBtn.Disable()<br /> self.btnGroupSizer.Add(self.listBtn)<br /> self.btnGroupSizer.Add((5,5),0)<br /> self.volSlider = wx.Slider(self,wx.NewId(), 0,0,100);<br /> self.Bind(wx.EVT_SLIDER,self.volumeSeek,self.volSlider);<br /> self.btnGroupSizer.Add(self.volSlider)<br /> self.btnGroupSizer.Add((5,5),0)<br /> self.volTxt = wx.StaticText(self,wx.NewId(),"")<br /> self.btnGroupSizer.Add(self.volTxt)<br /> self.btnGroupSizer.Add((5,5),0)<br /> mainSizer = wx.BoxSizer(wx.VERTICAL);<br /> mainSizer.Add(self.media,1,wx.EXPAND|wx.ALL,5);<br /> #mainSizer.Add((5,5),0)<br /> mainSizer.Add(self.slider,0,wx.EXPAND|wx.ALL,5);<br /> #mainSizer.Add((2,2),0)<br /> mainSizer.Add(self.btnGroupSizer,0,wx.EXPAND|wx.ALL,5);<br /> #mainSizer.Add((2,2),0)<br /> self.SetSizer(mainSizer);<br /> self.Layout();<br /> self.timer = wx.Timer(self)<br /> self.Bind(wx.EVT_TIMER,self.onTimer);<br /> self.timer.Start(200);<br /> def createButton(self,img,clickFun,tip=""):<br /> bmp = wx.Bitmap(img, wx.BITMAP_TYPE_PNG);<br /> btn = wx.BitmapButton(self,wx.NewId(),bmp);<br /> if tip != '' : btn.SetToolTipString(tip);<br /> btn.Bind(wx.EVT_BUTTON,clickFun);<br /> return btn;<br /> def quit(self,event):<br /> self.Destroy();<br /> def playMp3(self,e):<br /> if self.item != '':<br /> self.media.Play()<br /> # self.media.SetInitialSize()<br /> def pauseMp3(self,e=''):<br /> self.media.Pause()<br /> def stopMp3(self,e=''):<br /> self.media.Stop()<br /> def preMp3(self,e):<br /> if self.item!='':<br /> self.item = self.log.getPreItem(self.item.index)<br /> if self.item != '':<br /> self.playMedia(self.item.url,self.item.fileName);<br /> def nextMp3(self,e):<br /> if self.item!='':<br /> self.item = self.log.getNextItem(self.item.index)<br /> if self.item != '':<br /> self.playMedia(self.item.url,self.item.fileName);<br /> def onSeek(self,event):<br /> self.media.Seek(self.slider.GetValue())<br /> def callAfterPlayMedia(self,url):<br /> #self.media.SetInitialSize();<br /> self.media.Play();<br /> def playMedia(self,url,filename):<br /> #print SPrint.encodeFromSystem( u"播放檔案: " ) , SPrint.encodeFromSystem(url) ;<br /> if self.media.Load(url) != True:<br /> return ;<br /> self.item = MediaItem.MediaItem();<br /> self.item.fileName = filename;<br /> self.item.url = url;<br /> self.SetTitle(filename)<br /> self.SetStatusText(filename,1)<br /> #wx.FutureCall(2000,self.callAfterPlayMedia,url);<br /> def _openFile(self,event):<br /> dialog = wx.FileDialog(self,u"開啟檔案",'F://movie//',"",woldcart,style=wx.OPEN|wx.CHANGE_DIR)<br /> if dialog.ShowModal() == wx.ID_OK:<br /> self.playMedia(dialog.GetPath(),dialog.GetFilename())<br /> dialog.Destroy();<br /> def cellRemove(self,url,all=False):<br /> if all == True:<br /> self.stopMp3()<br /> self.item = '';<br /> return ;<br /> if self.item.url == url:<br /> self.stopMp3();<br /> self.item = '';<br /> def volumeSeek(self,evt):<br /> #print float(self.volSlider.GetValue()/100);<br /> self.media.SetVolume(self.volSlider.GetValue()/100.0)<br /> self.volTxt.SetLabel(str(self.volSlider.GetValue())+'%')<br /> def mediaLoaded(self,e):<br /> print "media loaded!"<br /> self.media.Play();<br /> self.slider.SetRange(0,self.media.Length());<br /> self.item.length = self.getAccurateTime(self.media.Length()/1000);<br /> if self.log.addItem(self.item):<br /> if self.listView != '':<br /> self.listView.reflash(self.log);<br /> if self.log.haveNextItem(self.item.index):<br /> self.nextBtn.Enable();<br /> else:<br /> self.nextBtn.Disable()<br /> if self.log.havePreItem(self.item.index):<br /> self.preBtn.Enable()<br /> else:<br /> self.preBtn.Disable();<br /> self.volSlider.SetValue(self.media.GetVolume()*100);<br /> self.volTxt.SetLabel(str(self.volSlider.GetValue())+'%')<br /> def mediaStateChange(self,evt):<br /> if self.media.GetState() == wx.media.MEDIASTATE_PAUSED:<br /> self.playBtn.Enable()<br /> self.pauseBtn.Disable()<br /> self.stopBtn.Enable();<br /> self.SetStatusText(u"暫停")<br /> elif self.media.GetState() == wx.media.MEDIASTATE_PLAYING:<br /> self.playBtn.Disable()<br /> self.pauseBtn.Enable()<br /> self.stopBtn.Enable();<br /> self.SetStatusText(u"播放")<br /> elif self.media.GetState() == wx.media.MEDIASTATE_STOPPED:<br /> self.playBtn.Enable()<br /> self.pauseBtn.Enable()<br /> self.stopBtn.Disable();<br /> self.SetStatusText(u"停止")<br /> def onTimer(self,evt):<br /> self.slider.SetValue(self.media.Tell());<br /> self.SetStatusText(self.creatTimeStatusTEXT(),1)<br /> def creatTimeStatusTEXT(self):<br /> current = self.getAccurateTime(self.media.Tell()/1000);<br /> total = self.getAccurateTime(self.media.Length()/1000);<br /> return ' ' + current + '/' + total;<br /> def openList(self,evt):<br /> if self.listView == '':<br /> self.listView = mediaList.MediaListView(self,self.log);<br /> self.listView.CenterOnParent(wx.BOTH);<br /> self.listView.Show();<br /> self.listView.Bind(wx.EVT_CLOSE,self.closeOpenList);<br /> else:<br /> self.closeOpenList(evt);<br /> def closeOpenList(self,evt):<br /> self.listView.Destroy();<br /> self.listView = '';<br /> def loadMediaList(self,evt):<br /> woldcart1 = "txt|*.txt";<br /> dialog = wx.FileDialog(self,u"載入播放清單",'C:',"",woldcart1,style=wx.OPEN|wx.CHANGE_DIR)<br /> if dialog.ShowModal() == wx.ID_OK:<br /> self._loadMediaList(dialog.GetPath())<br /> dialog.Destroy();<br /> def _loadMediaList(self,url):<br /> f = file(url)<br /> lines = f.readlines()<br /> for line in lines:<br /> if line:<br /> self.log.loadLog(line);<br /> def saveMediaList(self,evt):<br /> woldcart1 = "txt|*.txt";<br /> dialog = wx.FileDialog(self,u"儲存播放清單",'C:',"",woldcart1,style=wx.SAVE|wx.CHANGE_DIR)<br /> if dialog.ShowModal() == wx.ID_OK:<br /> self._saveMediaList(dialog.GetPath())<br /> dialog.Destroy();<br /> def _saveMediaList(self,url):<br /> f = file(url,"w+")<br /> f.writelines(self.log.createLog());<br /> f.close();<br /> self.showMessage(u"儲存成功!");<br /> def showMessage(self,str):<br /> dlg = wx.MessageDialog(self, str,u"訊息", wx.OK | wx.ICON_INFORMATION)<br /> dlg.ShowModal()<br /> dlg.Destroy()<br /> def getAccurateTime(self,s):<br /> h = 0<br /> m = 0<br /> if s/60 !=0:<br /> m = s/60<br /> s = s%60<br /> if m/60 !=0:<br /> h = m/60<br /> m = m%60<br /> return str(h)+':'+str(m)+':'+str(s)</p><p>if __name__ == "__main__":<br /> app = wx.PySimpleApp()<br /> frame = MediaFrame();<br /> bmp = wx.Icon("./pic/media.png",wx.BITMAP_TYPE_PNG)<br /> frame.SetIcon(bmp)<br /> frame.Show();<br /> app.MainLoop();
mediaList.py
# -*- coding: utf-8 -*-<br />import wx;<br />import sys;<br />import os;<br />import SaveLog;<br />import subprocess;<br />class MediaListView(wx.MiniFrame):<br /> def __init__(self,parent,log):<br /> wx.MiniFrame.__init__(self,parent,wx.NewId(),u"檔案清單",style=wx.DEFAULT_FRAME_STYLE);<br /> self.SetAutoLayout(True)<br /> self.mediaMain = parent;<br /> self.menu = '';<br /> self.selectIndex = -1<br /> self.listDataLog = SaveLog.SaveLog();<br /> self.listDataLog = log;<br /> self.listct = wx.ListCtrl(self,wx.NewId(),style=wx.LC_REPORT|<br /> wx.LC_VRULES|<br /> wx.LC_SORT_ASCENDING);<br /> self.listct.InsertColumn(0,u"編號")<br /> self.listct.SetColumnWidth(0,38)<br /> self.listct.InsertColumn(1,u"名字")<br /> self.listct.SetColumnWidth(1,235)<br /> self.listct.InsertColumn(2,u"時間");<br /> self.listct.SetColumnWidth(2,105)<br /> frSizer = wx.BoxSizer(wx.VERTICAL)<br /> frSizer.Add(self.listct,1,wx.EXPAND|wx.ALL,5)<br /> self.listct.Bind(wx.EVT_CONTEXT_MENU,self.onRightClick)<br /> self.listct.Bind(wx.EVT_LEFT_DCLICK,self.doubleClick)<br /> self.listct.Bind(wx.EVT_LIST_ITEM_SELECTED,self.itemSelected)<br /> self.SetSizer(frSizer)<br /> self.Layout();<br /> wx.CallAfter(self.reflash,self.listDataLog)<br /> def reflash(self,log=''):<br /> self.listct.DeleteAllItems();<br /> if log != '':<br /> self.listDataLog = log;<br /> i = 0;<br /> ls = log.getList();<br /> for it in ls:<br /> if it:<br /> index = self.listct.InsertStringItem(i,str(it.index));<br /> self.listct.SetStringItem(index,0,str(it.index))<br /> self.listct.SetStringItem(index,1,it.fileName)<br /> self.listct.SetStringItem(index,2,it.length)<br /> i = i+1;<br /> def onRightClick(self,evt):<br /> self.menu = wx.Menu();<br /> self.playMenu = self.menu.Append(wx.NewId(),u"播放")<br /> self.Bind(wx.EVT_MENU,self.cellPlay,self.playMenu)<br /> self.delMenu = self.menu.Append(wx.NewId(),u"刪除")<br /> self.Bind(wx.EVT_MENU,self.cellRomve,self.delMenu);<br /> self.dirMenu = self.menu.Append(wx.NewId(),u"開啟檔案目錄")<br /> self.Bind(wx.EVT_MENU,self.openDir,self.dirMenu);<br /> self.delAllMenu = self.menu.Append(wx.NewId(),u"清除播放清單")<br /> self.Bind(wx.EVT_MENU,self.clearAll,self.delAllMenu);<br /> self.PopupMenu(self.menu);<br /> self.menu.Destroy();<br /> def cellPlay(self,evt):<br /> self.selectIndex = self.listct.GetFirstSelected();<br /> item = self.listDataLog.getItemByIndex(self.selectIndex);<br /> self.mediaMain.playMedia(item.url,item.fileName);<br /> def cellRomve(self,evt):<br /> #self.selectIndex = self.listct.GetFirstSelected();<br /> url = self.listDataLog.delItemByIndex(self.listct.GetFirstSelected());<br /> self.listDataLog.reflashDataByIndex();<br /> self.reflash(self.listDataLog);<br /> self.mediaMain.cellRemove(url)<br /> def doubleClick(self,evt):<br /> item = self.listDataLog.getItemByIndex(self.selectIndex);<br /> self.mediaMain.playMedia(item.url,item.fileName);<br /> evt.Skip();<br /> def itemSelected(self,evt):<br /> self.selectIndex = evt.GetIndex();<br /> evt.Skip();<br /> def openDir(self,evt):<br /> self.selectIndex = self.listct.GetFirstSelected();<br /> item = self.listDataLog.getItemByIndex(self.selectIndex);<br /> subprocess.Popen("explorer " + os.path.split(item.url)[0]);<br /> def clearAll(self,evt):<br /> self.listDataLog.clearAll();<br /> self.reflash(self.listDataLog);<br /> self.mediaMain.cellRemove('',True)<br />
SaveLog.py
import MediaItem;<br />class SaveLog():<br /> list = [];<br /> def addItem(self,it=MediaItem.MediaItem):<br /> if self.getItemByURL(it.url) == '' :<br /> it.index = len(self.list) + 1;<br /> self.list.append(it);<br /> return True;<br /> return False;<br /> def clearAll(self):<br /> self.list = [];<br /> def getItemByURL(self,url):<br /> for it in self.list:<br /> if it and it.url == url:<br /> return it;<br /> return '';<br /> def getItemByIndex(self,ind):<br /> return self.list[ind];<br /> def delItemByIndex(self,ind):<br /> it = self.getItemByIndex(ind);<br /> if it :<br /> url = it.url;<br /> self.list.remove(it);<br /> return url;<br /> return '';<br /> def reflashDataByIndex(self):<br /> i = 1;<br /> for it in self.list:<br /> if it:<br /> it.index = i;<br /> i = i+ 1;<br /> def havePreItem(self,ind):<br /> if ind-1>0:<br /> return True;<br /> return False;<br /> def haveNextItem(self,ind):<br /> if ind < len(self.list)-1:<br /> return True;<br /> return False;<br /> def getPreItem(self,ind):<br /> if ind>0:<br /> ind = ind -1;<br /> return self.list[ind];<br /> return '';<br /> def getNextItem(self,ind):<br /> if ind < len(self.list)-1:<br /> ind = ind + 1;<br /> return self.list[ind];<br /> return '';<br /> def getList(self):<br /> return self.list;<br /> def createLog(self):<br /> out = [];<br /> for it in self.list:<br /> if it:<br /> out.append(it.createLog())<br /> return out;<br /> def loadLog(self,s=''):<br /> ls = s.split("|");<br /> item = MediaItem.MediaItem();<br /> item.index = ls[0]<br /> item.fileName = ls[1]<br /> item.url = ls[2]<br /> item.length = ls[3];<br /> self.addItem(item);</p><p> def test(self):<br /> item = MediaItem.MediaItem();<br /> item.index = 0;<br /> item.fileName = '123'<br /> item.length = '123'<br /> item.url = '123'<br /> self.list.append(item)<br /> item2 = MediaItem.MediaItem();<br /> item2.index = 1;<br /> item2.fileName = '1234'<br /> item2.length = '1234'<br /> item2.url = '1234'<br /> self.list.append(item2)<br /> item3 = MediaItem.MediaItem();<br /> item3.index = 2;<br /> item3.fileName = '12345'<br /> item3.length = '12345'<br /> item3.url = '12345'<br /> self.list.append(item3)<br />