PyQt phonon implement music player

Source: Internet
Author: User

Now online about PYQT's article is not much, say that official document is also rotten mess, first use phonon module encountered many questions,

Foreign sites related to more information, see some good examples recorded, mainly comments, give some hints to the later.

#-*-Coding:utf-8-*-import sysfrom PyQt4 import qtcore, qtguifrom pyqt4.phonon import phononclass audioplayer (QtGui.QWi Dget): Def __init__ (self, url, parent = None): #url参数就是你的MP3地址 self.url = URL Qtgui.qwidget.__init __ (self, parent) #设置一些窗口尺寸的策略, without setting there will also be a default policy, simply commented, no effect # Self.setsizepolicy (Qtgui.qsizepolicy.expanding,qtgui. qsizepolicy.preferred) #创建一个音乐播放器 This is a simple method, the function is also very single, seemingly can only achieve a simple single song playback (I hope I have not guessed wrong,), more flexible is the use of audiooutput, Mediaobject and other methods to implement Self.player = Phonon.createplayer (Phonon.musiccategory,phonon.mediasource (URL)) #下面这句话真没搞懂, Google a few times try not to respond to simply put his comments. Do not affect program # Self.player.setTickInterval (self.player.tick.connect) (self.tock) #播放进度改变触发事件 Self.play_ Pause = Qtgui.qpushbutton (self) #播放按钮 Self.play_pause.setIcon (Qtgui.qicon (' icons/49heart.svg ')) #设置播放按钮图标, Jpg,png Can be Self.play_pause.clicked.connect (self.playclicked) #播放按钮单击事件 Self.player.stateChanged.connect (self.state Changed) #播放状态改变触发事件       Self.slider = Phonon.seekslider (Self.player, self) #进度条 self.status = Qtgui.qlabel (self) #Label组件用来显示播放的当 Former Time self.status.setAlignment (QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter) #设置Label的对齐方式 left-aligned or centered layout = Qtgui.qhboxlayout (self) #水平布局 the Layout.addwidget (self). Play_pause) #添加播放按钮 layout.addwidget (self.slider) #添加滑动条 layout.addwidget (self.status) #添加播放状态 def Pl        Ayclicked (self): if self.player.state () = = Phonon.playingstate: #如果为播放状态 self.player.pause () #暂停    Else:self.player.play () #开始播放 def statechanged (self, new, old): if new = = Phonon.playingstate: #根据改变后的状态更换播放图标 Self.play_pause.setIcon (Qtgui.qicon (' icons/49heart.svg ')) Else:self.play_ Pause.seticon (Qtgui.qicon (' Icons/49heart.svg ')) def tock (Self, Time): #        The current playback time of the above label component is updated when the playback progress changes the time parameter is the current tick of the song, and the current position of the media object in the stream is given in milliseconds by the time parameter time/1000 #除以1000得到秒单位 H = time/36XX #小时 m = (time-3600*h)/#分钟 s = (time-3600*h-m*60) #秒 self.status.setText ('%02d:%02d:%02d '%  (h,m,s)) #更新Label显示的播放时间进度def Main (): App = Qtgui.qapplication (SYS.ARGV) Window=audioplayer (sys.argv[1]) #在命令行下的第二个参数 Your Song Path Diameter window.show () #显示主窗口 sys.exit (APP.EXEC_ ()) If __name__ = = "__main__": Main () # test: Python mp3player.py mymusic . mp3

This code to achieve a single first on-demand, embedded in the program to achieve simple sound playback is good, if you want to do more professional, more closely to the modern player of the function,

I'll cover some of the more flexible and complex features in the next few articles.


PyQt phonon implement music player

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.