python实现媒体播放器功能


Posted in Python onFebruary 11, 2018

本文实例为大家分享了python实现媒体播放器功能的具体代码,供大家参考,具体内容如下

playMP3.py

# -*- coding: utf-8 -*- 
import wx; 
import wx.media; 
import os; 
import SPrint; 
import mediaStateBar; 
import mediaList; 
import SaveLog; 
import MediaItem; 
woldcart = "media files|*.*|avi|*.avi|rmvb|*.rmvb|rm|*.rm|wma|*.wma|mp3|*.mp3"; 
class MediaFrame(wx.Frame): 
 def __init__(self): 
  wx.Frame.__init__(self,None,wx.NewId(),u"媒体播放器",pos=wx.DefaultPosition,size=(500,500)); 
  self.media = ''; 
  try: 
   self.media = wx.media.MediaCtrl(self,style=wx.SIMPLE_BORDER, 
            #szBackend=wx.media.MEDIABACKEND_DIRECTSHOW 
            #szBackend=wx.media.MEDIABACKEND_QUICKTIME 
            #szBackend=wx.media.MEDIABACKEND_WMP10 
            ); 
   self.media.Bind(wx.media.EVT_MEDIA_LOADED,self.mediaLoaded); 
   self.media.Bind(wx.media.EVT_MEDIA_STATECHANGED,self.mediaStateChange) 
  except NotImplementedError: 
   self.Destroy() 
   raise; 
  self.listView = ''; 
  self.item = MediaItem.MediaItem(); 
  self.sb = mediaStateBar.MediaStateBar(self); 
  self.SetStatusBar(self.sb); 
  self.log = SaveLog.SaveLog(); 
  #self.log.test(); 
  self.menu = wx.Menu() 
  self.openMenu = self.menu.Append(wx.NewId(),u"打开文件"); 
  self.Bind(wx.EVT_MENU,self._openFile,self.openMenu); 
  self.quitMenu = self.menu.Append(wx.NewId(),u"退出"); 
  self.Bind(wx.EVT_MENU,self.quit,self.quitMenu); 
  self.viewMenu = wx.Menu(); 
  self.listMenu = self.viewMenu.Append(wx.NewId(),u"打开播放列表"); 
  self.Bind(wx.EVT_MENU,self.openList,self.listMenu); 
  self.loadListMenu = self.viewMenu.Append(wx.NewId(),u"加载播放列表"); 
  self.Bind(wx.EVT_MENU,self.loadMediaList,self.loadListMenu); 
  self.saveListMenu = self.viewMenu.Append(wx.NewId(),u"保存播放列表") 
  self.Bind(wx.EVT_MENU,self.saveMediaList,self.saveListMenu); 
  menuBar = wx.MenuBar(); 
  menuBar.Append(self.menu,u"文件") 
  menuBar.Append(self.viewMenu,u"播放列表") 
  self.SetMenuBar(menuBar); 
  self.slider = wx.Slider(self,wx.NewId(), 0,0,100); 
  self.Bind(wx.EVT_SLIDER,self.onSeek,self.slider); 
  self.btnGroupSizer = wx.BoxSizer(wx.HORIZONTAL); 
  self.btnGroupSizer.Add((5,5),0) 
  self.playBtn = self.createButton("./pic/play.png",self.playMp3,u"播放"); 
  self.playBtn.Disable() 
  self.btnGroupSizer.Add(self.playBtn); 
  self.btnGroupSizer.Add((5,5),0) 
  self.pauseBtn = self.createButton("./pic/pause.png",self.pauseMp3,u"暂停"); 
  self.pauseBtn.Disable() 
  self.btnGroupSizer.Add(self.pauseBtn); 
  self.btnGroupSizer.Add((5,5),0) 
  self.stopBtn = self.createButton("./pic/stop.png",self.stopMp3,u"停止"); 
  self.stopBtn.Disable() 
  self.btnGroupSizer.Add(self.stopBtn); 
  self.btnGroupSizer.Add((5,5),0) 
  self.preBtn = self.createButton("./pic/pre.png",self.preMp3,u"前一个"); 
  self.preBtn.Disable() 
  self.btnGroupSizer.Add(self.preBtn); 
  self.btnGroupSizer.Add((5,5),0) 
  self.nextBtn = self.createButton("./pic/next.png",self.nextMp3,u"下一个"); 
  self.nextBtn.Disable() 
  self.btnGroupSizer.Add(self.nextBtn); 
  self.btnGroupSizer.Add((5,5),0) 
  self.openBtn = self.createButton("./pic/media.png",self._openFile,u"打开文件"); 
  #self.openBtn.Disable() 
  self.btnGroupSizer.Add(self.openBtn); 
  self.btnGroupSizer.Add((5,5),0) 
  self.listBtn = self.createButton("./pic/list.png",self.openList,u"打开列表"); 
  #self.listBtn.Disable() 
  self.btnGroupSizer.Add(self.listBtn) 
  self.btnGroupSizer.Add((5,5),0) 
  self.volSlider = wx.Slider(self,wx.NewId(), 0,0,100); 
  self.Bind(wx.EVT_SLIDER,self.volumeSeek,self.volSlider); 
  self.btnGroupSizer.Add(self.volSlider) 
  self.btnGroupSizer.Add((5,5),0) 
  self.volTxt = wx.StaticText(self,wx.NewId(),"") 
  self.btnGroupSizer.Add(self.volTxt) 
  self.btnGroupSizer.Add((5,5),0) 
  mainSizer = wx.BoxSizer(wx.VERTICAL); 
  mainSizer.Add(self.media,1,wx.EXPAND|wx.ALL,5); 
  #mainSizer.Add((5,5),0) 
  mainSizer.Add(self.slider,0,wx.EXPAND|wx.ALL,5); 
  #mainSizer.Add((2,2),0) 
  mainSizer.Add(self.btnGroupSizer,0,wx.EXPAND|wx.ALL,5); 
  #mainSizer.Add((2,2),0) 
  self.SetSizer(mainSizer); 
  self.Layout(); 
  self.timer = wx.Timer(self) 
  self.Bind(wx.EVT_TIMER,self.onTimer); 
  self.timer.Start(200); 
 def createButton(self,img,clickFun,tip=""): 
  bmp = wx.Bitmap(img, wx.BITMAP_TYPE_PNG); 
  btn = wx.BitmapButton(self,wx.NewId(),bmp); 
  if tip != '' : btn.SetToolTipString(tip); 
  btn.Bind(wx.EVT_BUTTON,clickFun); 
  return btn; 
 def quit(self,event): 
  self.Destroy(); 
 def playMp3(self,e): 
  if self.item != '': 
   self.media.Play() 
  # self.media.SetInitialSize() 
 def pauseMp3(self,e=''): 
  self.media.Pause() 
 def stopMp3(self,e=''): 
  self.media.Stop() 
 def preMp3(self,e): 
  if self.item!='': 
   self.item = self.log.getPreItem(self.item.index) 
   if self.item != '': 
    self.playMedia(self.item.url,self.item.fileName); 
 def nextMp3(self,e): 
  if self.item!='': 
   self.item = self.log.getNextItem(self.item.index) 
   if self.item != '': 
    self.playMedia(self.item.url,self.item.fileName); 
 def onSeek(self,event): 
  self.media.Seek(self.slider.GetValue()) 
 def callAfterPlayMedia(self,url): 
  #self.media.SetInitialSize(); 
  self.media.Play(); 
 def playMedia(self,url,filename): 
  #print SPrint.encodeFromSystem( u"播放文件: " ) , SPrint.encodeFromSystem(url) ; 
  if self.media.Load(url) != True: 
   return ; 
  self.item = MediaItem.MediaItem(); 
  self.item.fileName = filename; 
  self.item.url = url; 
  self.SetTitle(filename) 
  self.SetStatusText(filename,1) 
  #wx.FutureCall(2000,self.callAfterPlayMedia,url); 
 def _openFile(self,event): 
  dialog = wx.FileDialog(self,u"打开文件",'F://movie//',"",woldcart,style=wx.OPEN|wx.CHANGE_DIR) 
  if dialog.ShowModal() == wx.ID_OK: 
   self.playMedia(dialog.GetPath(),dialog.GetFilename()) 
  dialog.Destroy(); 
 def cellRemove(self,url,all=False): 
  if all == True: 
   self.stopMp3() 
   self.item = ''; 
   return ; 
  if self.item.url == url: 
   self.stopMp3(); 
   self.item = ''; 
 def volumeSeek(self,evt): 
  #print float(self.volSlider.GetValue()/100); 
  self.media.SetVolume(self.volSlider.GetValue()/100.0) 
  self.volTxt.SetLabel(str(self.volSlider.GetValue())+'%') 
 def mediaLoaded(self,e): 
  print "media loaded!" 
  self.media.Play(); 
  self.slider.SetRange(0,self.media.Length()); 
  self.item.length = self.getAccurateTime(self.media.Length()/1000); 
  if self.log.addItem(self.item): 
   if self.listView != '': 
    self.listView.reflash(self.log); 
  if self.log.haveNextItem(self.item.index): 
   self.nextBtn.Enable(); 
  else: 
   self.nextBtn.Disable() 
  if self.log.havePreItem(self.item.index): 
   self.preBtn.Enable() 
  else: 
   self.preBtn.Disable(); 
  self.volSlider.SetValue(self.media.GetVolume()*100); 
  self.volTxt.SetLabel(str(self.volSlider.GetValue())+'%') 
 def mediaStateChange(self,evt): 
  if self.media.GetState() == wx.media.MEDIASTATE_PAUSED: 
   self.playBtn.Enable() 
   self.pauseBtn.Disable() 
   self.stopBtn.Enable(); 
   self.SetStatusText(u"暂停") 
  elif self.media.GetState() == wx.media.MEDIASTATE_PLAYING: 
   self.playBtn.Disable() 
   self.pauseBtn.Enable() 
   self.stopBtn.Enable(); 
   self.SetStatusText(u"播放") 
  elif self.media.GetState() == wx.media.MEDIASTATE_STOPPED: 
   self.playBtn.Enable() 
   self.pauseBtn.Enable() 
   self.stopBtn.Disable(); 
   self.SetStatusText(u"停止") 
 def onTimer(self,evt): 
  self.slider.SetValue(self.media.Tell()); 
  self.SetStatusText(self.creatTimeStatusTEXT(),1) 
 def creatTimeStatusTEXT(self): 
  current = self.getAccurateTime(self.media.Tell()/1000); 
  total = self.getAccurateTime(self.media.Length()/1000); 
  return ' ' + current + '/' + total; 
 def openList(self,evt): 
  if self.listView == '': 
   self.listView = mediaList.MediaListView(self,self.log); 
   self.listView.CenterOnParent(wx.BOTH); 
   self.listView.Show(); 
   self.listView.Bind(wx.EVT_CLOSE,self.closeOpenList); 
  else: 
   self.closeOpenList(evt); 
 def closeOpenList(self,evt): 
  self.listView.Destroy(); 
  self.listView = ''; 
 def loadMediaList(self,evt): 
  woldcart1 = "txt|*.txt"; 
  dialog = wx.FileDialog(self,u"加载播放列表",'C:',"",woldcart1,style=wx.OPEN|wx.CHANGE_DIR) 
  if dialog.ShowModal() == wx.ID_OK: 
   self._loadMediaList(dialog.GetPath()) 
  dialog.Destroy(); 
 def _loadMediaList(self,url): 
  f = file(url) 
  lines = f.readlines() 
  for line in lines: 
   if line: 
    self.log.loadLog(line); 
 def saveMediaList(self,evt): 
  woldcart1 = "txt|*.txt"; 
  dialog = wx.FileDialog(self,u"保存播放列表",'C:',"",woldcart1,style=wx.SAVE|wx.CHANGE_DIR) 
  if dialog.ShowModal() == wx.ID_OK: 
   self._saveMediaList(dialog.GetPath()) 
  dialog.Destroy(); 
 def _saveMediaList(self,url): 
  f = file(url,"w+") 
  f.writelines(self.log.createLog()); 
  f.close(); 
  self.showMessage(u"保存成功!"); 
 def showMessage(self,str): 
  dlg = wx.MessageDialog(self, str,u"消息", wx.OK | wx.ICON_INFORMATION) 
  dlg.ShowModal() 
  dlg.Destroy() 
 def getAccurateTime(self,s): 
  h = 0 
  m = 0 
  if s/60 !=0: 
   m = s/60 
   s = s%60 
  if m/60 !=0: 
   h = m/60 
   m = m%60 
  return str(h)+':'+str(m)+':'+str(s) 
 
if __name__ == "__main__": 
 app = wx.PySimpleApp() 
 frame = MediaFrame(); 
 bmp = wx.Icon("./pic/media.png",wx.BITMAP_TYPE_PNG) 
 frame.SetIcon(bmp) 
 frame.Show(); 
 app.MainLoop();

mediaList.py

# -*- coding: utf-8 -*- 
import wx; 
import sys; 
import os; 
import SaveLog; 
import subprocess; 
class MediaListView(wx.MiniFrame): 
 def __init__(self,parent,log): 
  wx.MiniFrame.__init__(self,parent,wx.NewId(),u"文件列表",style=wx.DEFAULT_FRAME_STYLE); 
  self.SetAutoLayout(True) 
  self.mediaMain = parent; 
  self.menu = ''; 
  self.selectIndex = -1 
  self.listDataLog = SaveLog.SaveLog(); 
  self.listDataLog = log; 
  self.listct = wx.ListCtrl(self,wx.NewId(),style=wx.LC_REPORT| 
               wx.LC_VRULES| 
               wx.LC_SORT_ASCENDING); 
  self.listct.InsertColumn(0,u"编号") 
  self.listct.SetColumnWidth(0,38) 
  self.listct.InsertColumn(1,u"名字") 
  self.listct.SetColumnWidth(1,235) 
  self.listct.InsertColumn(2,u"时间"); 
  self.listct.SetColumnWidth(2,105) 
  frSizer = wx.BoxSizer(wx.VERTICAL) 
  frSizer.Add(self.listct,1,wx.EXPAND|wx.ALL,5) 
  self.listct.Bind(wx.EVT_CONTEXT_MENU,self.onRightClick) 
  self.listct.Bind(wx.EVT_LEFT_DCLICK,self.doubleClick) 
  self.listct.Bind(wx.EVT_LIST_ITEM_SELECTED,self.itemSelected) 
  self.SetSizer(frSizer) 
  self.Layout(); 
  wx.CallAfter(self.reflash,self.listDataLog) 
 def reflash(self,log=''): 
  self.listct.DeleteAllItems(); 
  if log != '': 
   self.listDataLog = log; 
  i = 0; 
  ls = log.getList(); 
  for it in ls: 
   if it: 
    index = self.listct.InsertStringItem(i,str(it.index)); 
    self.listct.SetStringItem(index,0,str(it.index)) 
    self.listct.SetStringItem(index,1,it.fileName) 
    self.listct.SetStringItem(index,2,it.length) 
    i = i+1; 
 def onRightClick(self,evt): 
  self.menu = wx.Menu(); 
  self.playMenu = self.menu.Append(wx.NewId(),u"播放") 
  self.Bind(wx.EVT_MENU,self.cellPlay,self.playMenu) 
  self.delMenu = self.menu.Append(wx.NewId(),u"删除") 
  self.Bind(wx.EVT_MENU,self.cellRomve,self.delMenu); 
  self.dirMenu = self.menu.Append(wx.NewId(),u"打开文件目录") 
  self.Bind(wx.EVT_MENU,self.openDir,self.dirMenu); 
  self.delAllMenu = self.menu.Append(wx.NewId(),u"清除播放列表") 
  self.Bind(wx.EVT_MENU,self.clearAll,self.delAllMenu); 
  self.PopupMenu(self.menu); 
  self.menu.Destroy(); 
 def cellPlay(self,evt): 
  self.selectIndex = self.listct.GetFirstSelected(); 
  item = self.listDataLog.getItemByIndex(self.selectIndex); 
  self.mediaMain.playMedia(item.url,item.fileName); 
 def cellRomve(self,evt): 
  #self.selectIndex = self.listct.GetFirstSelected(); 
  url = self.listDataLog.delItemByIndex(self.listct.GetFirstSelected()); 
  self.listDataLog.reflashDataByIndex(); 
  self.reflash(self.listDataLog); 
  self.mediaMain.cellRemove(url) 
 def doubleClick(self,evt): 
  item = self.listDataLog.getItemByIndex(self.selectIndex); 
  self.mediaMain.playMedia(item.url,item.fileName); 
  evt.Skip(); 
 def itemSelected(self,evt): 
  self.selectIndex = evt.GetIndex(); 
  evt.Skip(); 
 def openDir(self,evt): 
  self.selectIndex = self.listct.GetFirstSelected(); 
  item = self.listDataLog.getItemByIndex(self.selectIndex); 
  subprocess.Popen("explorer " + os.path.split(item.url)[0]); 
 def clearAll(self,evt): 
  self.listDataLog.clearAll(); 
  self.reflash(self.listDataLog); 
  self.mediaMain.cellRemove('',True)

SaveLog.py

import MediaItem; 
class SaveLog(): 
 list = []; 
 def addItem(self,it=MediaItem.MediaItem): 
  if self.getItemByURL(it.url) == '' : 
   it.index = len(self.list) + 1; 
   self.list.append(it); 
   return True; 
  return False; 
 def clearAll(self): 
  self.list = []; 
 def getItemByURL(self,url): 
  for it in self.list: 
   if it and it.url == url: 
    return it; 
  return ''; 
 def getItemByIndex(self,ind): 
  return self.list[ind]; 
 def delItemByIndex(self,ind): 
  it = self.getItemByIndex(ind); 
  if it : 
   url = it.url; 
   self.list.remove(it); 
   return url; 
  return ''; 
 def reflashDataByIndex(self): 
  i = 1; 
  for it in self.list: 
   if it: 
    it.index = i; 
    i = i+ 1; 
 def havePreItem(self,ind): 
  if ind-1>0: 
   return True; 
  return False; 
 def haveNextItem(self,ind): 
  if ind < len(self.list)-1: 
   return True; 
  return False; 
 def getPreItem(self,ind): 
  if ind>0: 
   ind = ind -1; 
   return self.list[ind]; 
  return ''; 
 def getNextItem(self,ind): 
  if ind < len(self.list)-1: 
   ind = ind + 1; 
   return self.list[ind]; 
  return ''; 
 def getList(self): 
  return self.list; 
 def createLog(self): 
  out = []; 
  for it in self.list: 
   if it: 
    out.append(it.createLog()) 
  return out; 
 def loadLog(self,s=''): 
  ls = s.split("|"); 
  item = MediaItem.MediaItem(); 
  item.index = ls[0] 
  item.fileName = ls[1] 
  item.url = ls[2] 
  item.length = ls[3]; 
  self.addItem(item); 
 
 def test(self): 
  item = MediaItem.MediaItem(); 
  item.index = 0; 
  item.fileName = '123' 
  item.length = '123' 
  item.url = '123' 
  self.list.append(item) 
  item2 = MediaItem.MediaItem(); 
  item2.index = 1; 
  item2.fileName = '1234' 
  item2.length = '1234' 
  item2.url = '1234' 
  self.list.append(item2) 
  item3 = MediaItem.MediaItem(); 
  item3.index = 2; 
  item3.fileName = '12345' 
  item3.length = '12345' 
  item3.url = '12345' 
  self.list.append(item3)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中几种操作字符串的方法的介绍
Apr 09 Python
python使用nntp读取新闻组内容的方法
May 08 Python
python实现红包裂变算法
Feb 16 Python
Python编程使用NLTK进行自然语言处理详解
Nov 16 Python
django小技巧之html模板中调用对象属性或对象的方法
Nov 30 Python
分享Python切分字符串的一个不错方法
Dec 14 Python
详解Python下载图片并保存本地的两种方式
May 15 Python
解决python中导入win32com.client出错的问题
Jul 26 Python
python matplotlib画盒图、子图解决坐标轴标签重叠的问题
Jan 19 Python
Python3.7实现验证码登录方式代码实例
Feb 14 Python
怎么解决pycharm license Acti的方法
Oct 28 Python
python 获取谷歌浏览器保存的密码
Jan 06 Python
python使用pycharm环境调用opencv库
Feb 11 #Python
Python元组及文件核心对象类型详解
Feb 11 #Python
详解Python核心对象类型字符串
Feb 11 #Python
python使用json序列化datetime类型实例解析
Feb 11 #Python
Python中pow()和math.pow()函数用法示例
Feb 11 #Python
Python反射的用法实例分析
Feb 11 #Python
python互斥锁、加锁、同步机制、异步通信知识总结
Feb 11 #Python
You might like
php面向对象全攻略 (一) 面向对象基础知识
2009/09/30 PHP
PHP模板引擎Smarty的缓存使用总结
2014/04/24 PHP
PHP学习笔记(二):变量详解
2015/04/17 PHP
php支付宝APP支付功能
2020/07/29 PHP
用javascript动态调整iframe高度的代码
2007/04/10 Javascript
jQuery EasyUI API 中文文档 DateTimeBox日期时间框
2011/10/16 Javascript
jquery 插件学习(五)
2012/08/06 Javascript
jquery validate在ie8下的bug解决方法
2013/11/13 Javascript
javascript读取xml实现javascript分页
2013/12/13 Javascript
JavaScript 学习笔记之基础中的基础
2015/01/13 Javascript
JavaScript省市级联下拉菜单实例
2017/02/14 Javascript
微信小程序页面传值实例分析
2017/04/19 Javascript
ionic App问题总结系列之ionic点击系统返回键退出App
2017/08/19 Javascript
JS实现的简单折叠展开动画效果示例
2018/04/28 Javascript
vue弹窗组件的实现示例代码
2018/09/10 Javascript
详解多页应用 Webpack4 配置优化与踩坑记录
2018/10/16 Javascript
JS遍历JSON数组及获取JSON数组长度操作示例【测试可用】
2018/12/12 Javascript
[02:57]DOTA2亚洲邀请赛小组赛第四日 赛事回顾
2015/02/02 DOTA
Python中不同进制互相转换(二进制、八进制、十进制和十六进制)
2015/04/05 Python
在Python中操作字符串之rstrip()方法的使用
2015/05/19 Python
Python递归函数定义与用法示例
2017/06/02 Python
Python 爬虫之超链接 url中含有中文出错及解决办法
2017/08/03 Python
基于Python的文件类型和字符串详解
2017/12/21 Python
50行Python代码实现人脸检测功能
2018/01/23 Python
numpy中loadtxt 的用法详解
2018/08/03 Python
Python基于plotly模块实现的画图操作示例
2019/01/23 Python
Python3中的bytes和str类型详解
2019/05/02 Python
详解Python是如何实现issubclass的
2019/07/24 Python
Python函数装饰器原理与用法详解
2019/08/16 Python
tensorflow多维张量计算实例
2020/02/11 Python
h5使用canvas画布实现手势解锁
2019/01/04 HTML / CSS
Puritan’s Pride(普丽普莱)官方网站:美国最大最全的保健品公司之一
2016/10/23 全球购物
Hotels.com韩国:海外国内旅行所需的酒店和住宿预订网站
2020/05/08 全球购物
军校本科大学生自我评价
2014/01/14 职场文书
幼儿园元旦亲子活动方案
2014/02/17 职场文书
社区艾滋病宣传活动总结
2015/05/07 职场文书