我从我的朋友那里得到了这段代码,我没有更改代码中的任何内容。部分代码适用于 mp3 播放器,它可以工作,但 mp4 播放器无法工作。
这是视频播放器的代码,是的,我安装了所有软件包并且没有错误。上传视频时,屏幕上没有显示该视频,并且播放按钮不起作用。
import pygame
from tkinter import filedialog
import time
from mutagen.mp3 import MP3
from tkinter import *
import tkinter.ttk as ttk
############## imports below so we can run the video player from here ##############
from PyQt5.QtWidgets import (QWidget, QApplication, QPushButton, QStyle, QFileDialog,
QSlider, QLabel, QSizePolicy, QVBoxLayout, QHBoxLayout)
from PyQt5.QtGui import QIcon, QPalette
from PyQt5.QtCore import Qt, QUrl
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer
from PyQt5.QtMultimediaWidgets import QVideoWidget
import sys
import os
####################################################################################
class window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Rami Video Player 1.0")
self.setGeometry(400, 200, 1000, 600)
self.setWindowIcon(QIcon('imgs/python.jpg'))
p = self.palette()
p.setColor(QPalette.Window, Qt.black)
self.setPalette(p)
self.init_ui()
self.show()
def init_ui(self):
self.mediaPlayer = QMediaPlayer(None, QMediaPlayer.VideoSurface)
videowidget = QVideoWidget()
openBtn = QPushButton('Open Video')
openBtn.clicked.connect(self.open_file)
self.playBtn = QPushButton()
self.playBtn.setEnabled(False)
self.playBtn.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
self.playBtn.clicked.connect(self.play_video)
self.slider = QSlider(Qt.Horizontal)
self.slider.setRange(0, 0)
self.slider.sliderMoved.connect(self.set_position)
self.label = QLabel()
self.label.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Maximum)
hboxlayout = QHBoxLayout()
hboxlayout.setContentsMargins(0, 0, 0, 0)
hboxlayout.addWidget(openBtn)
hboxlayout.addWidget(self.playBtn)
hboxlayout.addWidget(self.slider)
vboxlayout = QVBoxLayout()
vboxlayout.addWidget(videowidget)
vboxlayout.addLayout(hboxlayout)
vboxlayout.addWidget(self.label)
self.setLayout(vboxlayout)
self.mediaPlayer.setVideoOutput(videowidget)
self.mediaPlayer.stateChanged.connect(self.mediastate_changed)
self.mediaPlayer.positionChanged.connect(self.position_changed)
self.mediaPlayer.durationChanged.connect(self.duration_changed)
def open_file(self):
filename, _ = QFileDialog.getOpenFileName(self, "Open Video",
os.getcwd(),
("Video Files (*.avi *.mp4 *.mov)"))
if filename != '':
self.mediaPlayer.setMedia(QMediaContent(QUrl.fromLocalFile(filename)))
self.playBtn.setEnabled(True)
def play_video(self):
if self.mediaPlayer.state() == QMediaPlayer.PlayingState:
self.mediaPlayer.pause()
else:
self.mediaPlayer.play()
def mediastate_changed(self, state):
if self.mediaPlayer.state() == QMediaPlayer.PlayingState:
self.playBtn.setIcon(
self.style().standardIcon(QStyle.SP_MediaPause)
)
else:
self.playBtn.setIcon(
self.style().standardIcon(QStyle.SP_MediaPlay)
)
def position_changed(self, position):
self.slider.setValue(position)
def duration_changed(self, duration):
self.slider.setRange(0, duration)
def set_position(self, position):
self.mediaPlayer.setPosition(position)
这是为什么?
代码在我这边工作得很好,但你可能错过了
libqt5multimedia5-plugins
,这必须另外安装到qt5
。
在 Debian 上,可以通过
sudo apt-get install libqt5multimedia5-plugins
安装来解决此问题。