Python trainer-Media Player

Source: Internet
Author: User

 

You can play most of the audio and video.

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 "Media Player", Pos = wx. DEFA Ultposition, 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_statechan Ged, self. mediastatechange) <br/> constraint t 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 "Open File"); <br/> Self. BIND (wx. evt_menu, self. _ openfile, self. openmenu); <br/> self. quitmenu = self. menu. append (wx. newid (), u "quit"); <br/> self. BIND (wx. evt_menu, self. quit, self. quitmenu); <br/> self. viewmenu = wx. menu (); <br/> self. listmenu = self. viewmenu. append (wx. newid (), u "Open playlist"); <br/> self. BIND (wx. evt_menu, self. openlist, self. listmenu); <br/> self. loadlistmenu = self. viewmenu. append (wx. newid (), u "loading playlist"); <br/> S Elf. BIND (wx. evt_menu, self. loadmedialist, self. loadlistmenu); <br/> self. savelistmenu = self. viewmenu. append (wx. newid (), u "Save playlist") <br/> self. BIND (wx. evt_menu, self. savemedialist, self. savelistmenu); <br/> menubar = wx. menubar (); <br/> menubar. append (self. menu, U "file") <br/> menubar. append (self. viewmenu, U "playlist") <br/> self. setmenubar (menubar); <br/> self. slider = wx. slider (self, wx. newid (), 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 "play"); <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. pau Semp3, U "paused"); <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 "stop"); <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 "previous"); <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 "Next"); <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 "Open File"); <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 "Open List"); <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 (), 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. bitmapbut Ton (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 "playback file:"), 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 "Open File", 'f: // movie // ', "", woldcart, style = wx. open | wx. change_dir) <br/> If dialog. show Modal () = 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. medi A. 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 "pause") <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 "play Put ") <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 "stop") <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. me Dia. 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. c Loseopenlist (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 "load playlist", '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 T (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 "Save the playlist", '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 "saved successfully! "); <Br/> def showmessage (self, STR): <br/> DLG = wx. messagedialog (self, STR, U "message", 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 "file list", 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 "no.") <br/> self. listct. setcolumnwidth (0, 38) <br/> self. listct. insertcolumn (1, U "name") <br/> self. listct. setcolumnwidth (1,235) <br/> self. listct. insertcolumn (2, U "time"); <br/> self. li Stct. 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. lis Tdatalog) <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 "play") <br/> self. BIND (wx. evt_menu, self. cellplay, self. playmenu) <br/> self. delmenu = self. menu. append (wx. newid (), u "delete") <br/> self. BIND (wx. evt_menu, self. cellromve, self. delmenu); <br/> self. dirmenu = self. menu. append (wx. newid (), u "open file directory") <br/> self. BIND (wx. evt_menu, self. opendir, self. dirmenu); <br/> self. delallmenu = self. menu. append (wx. newid (), u "Clear playlist") <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 = '000000' <br/> item. length = '000000' <br/> item. url = '000000' <br/> self. list. append (item) <br/> item2 = MediaItem. mediaItem (); <br/> item2.index = 1; <br/> item2.fileName = '000000' <br/> item2.length = '000000' <br/> item2.url = '000000' <br/> self. list. append (item2) <br/> item3 = MediaItem. mediaItem (); <br/> item3.index = 2; <br/> item3.fileName = '000000' <br/> item3.length = '000000' <br/> item3.url = '000000' <br/> self. list. append (item3) <br/>

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.