pyqt播放多媒体,使用到的是 phonon.Phonon 类的VideoPlayer对象
首先创建一个VideoPlayer对象, 然后创建一个MediaSource对象
最后执行vidioPlayer对象的play方法,即可播放音乐
暂停方法,pause,seek调转到某一帧,可做快进快退功能
继续播放,再次执行play,play方法不要穿参数
# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui, Qt
class Player(QtGui.QMainWindow):
style = 'background:url(images/{0}.png) no-repeat center;border:none;'
currentMovie = None
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setWindowTitle("ADAM Player") #设置窗体的标题
self.resize(800, 500) #设置窗体大小
self.setWindowIcon(QtGui.QIcon('gita.ico')) #设置icon
self.setStyleSheet('background:white')
# self.setWindowFlags(Qt.Qt.FramelessWindowHint)
self.setMask(QtGui.QPixmap('images/bg.png').mask())
self.setMouseTracking(True)
menu = self.menuBar()
menu.setStyleSheet('background:#000;')
self.menu = menu.addMenu(u'&文件')
open = self.menu.addAction(u'打开')
self.connect(open, QtCore.SIGNAL('triggered()'), self.openMovie)
self.menu.setStyleSheet('background:none;')
#播放器对象
self.videoPlayer = phonon.Phonon.VideoPlayer(self)
self.videoPlayer.resize(QtCore.QSize(800, 450))
#播放按钮
self.playBtn = QtGui.QPushButton(self)
self.playBtn.setObjectName('playBtn')
self.playBtn.setStyleSheet(self.style.format('play'))
self.playBtn.setMask(QtGui.QPixmap('images/play.png').mask())
self.playBtn.resize(48, 48)
self.connect(self.playBtn, QtCore.SIGNAL('clicked()'), self.play) #绑定点击事件
#重新播放按钮
self.resetBtn = QtGui.QPushButton(self)
self.resetBtn.setObjectName('resetBtn')
self.resetBtn.setStyleSheet(self.style.format('reset'))
self.resetBtn.setMask(QtGui.QPixmap('images/reset.png').mask())
self.resetBtn.resize(48, 48)
self.connect(self.resetBtn, QtCore.SIGNAL('clicked()'), self.restart) #绑定点击事件
self.videoPlayer.move(0, 20)
self.playBtn.move(20, 415)
self.resetBtn.move(80, 415)
def play(self):
if not self.currentMovie:
QtGui.QMessageBox.warning(self, u'提示', u'请选择影片')
return False
if self.videoPlayer.isPlaying():
self.videoPlayer.pause()
self.position = self.videoPlayer.currentTime()
self.playBtn.setStyleSheet(self.style.format('play'))
else:
if self.videoPlayer.isPaused():
self.videoPlayer.play()
self.playBtn.setStyleSheet(self.style.format('pause'))
else:
# mediaSource = phonon.Phonon.MediaSource("./source/122195353176400128.mp3")
# mediaSource = phonon.Phonon.MediaSource(u"./source/爱她的枪(预告片).mpeg")
mediaSource = phonon.Phonon.MediaSource(self.currentMovie)
self.videoPlayer.load(mediaSource)
self.videoPlayer.play()
self.playBtn.setStyleSheet(self.style.format('pause'))
self.setWindowTitle(self.currentMovie)
def restart(self):
self.videoPlayer.seek(0)
def openMovie(self):
self.currentMovie = unicode(QtGui.QFileDialog.getOpenFileName(self))
if self.currentMovie:
self.videoPlayer.load(phonon.Phonon.MediaSource(self.currentMovie))
self.play()
from PyQt4 import phonon
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
palyer = Player()
palyer.show()
sys.exit(app.exec_())