在使用pyqt的videoplayer开发视频播放器的时候,发现不能播放MP4,结果发现没有解码器,在电脑上安装MP4解码器就可以了。K-Lite Codec Pack支持大多数格式的视频解码
下面是我写的视频播放器,比较简单
# -*- 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.videoPlayer.show()
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_())