使用操作系统:Linux Ubuntu 18.04
我正在尝试运行包含QtMultimedia模块的不同PyQt示例。 不幸的是,他们给了我同样的错误:
defaultServiceProvider :: requestService():找不到以下服务: “ org.qt-project.qt.mediaplayer”
我听说许多使用类似Unix的操作系统的人都遇到了这个问题,但是我仍然找不到任何解决方案。
这是一个代码示例:`
from PyQt5 import QtCore, QtWidgets, QtMultimedia
import sys
class MyWindow(QtWidgets.QWidget):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent, flags=
QtCore.Qt.Window | QtCore.Qt.MSWindowsFixedSizeDialogHint)
self.setWindowTitle("Music PLayer")
# Player
self.mpPlayer = QtMultimedia.QMediaPlayer()
self.mpPlayer.setVolume(50)
self.mpPlayer.mediaStatusChanged.connect(self.initPlayer)
self.mpPlayer.stateChanged.connect(self.setPlayerState)
vbox = QtWidgets.QVBoxLayout()
# File open button
btnOpen = QtWidgets.QPushButton("File &open")
btnOpen.clicked.connect(self.openFile)
vbox.addWidget(btnOpen)
# Make components disable at start
self.sldPosition = QtWidgets.QSlider(QtCore.Qt.Horizontal)
self.sldPosition.setMinimum(0)
self.sldPosition.valueChanged.connect(self.mpPlayer.setPosition)
self.mpPlayer.positionChanged.connect(self.sldPosition.setValue)
self.sldPosition.setEnabled(False)
vbox.addWidget(self.sldPosition)
hbox = QtWidgets.QHBoxLayout()
self.btnPlay = QtWidgets.QPushButton("&Play")
self.btnPlay.clicked.connect(self.mpPlayer.play)
self.btnPlay.setEnabled(False)
hbox.addWidget(self.btnPlay)
self.btnPause = QtWidgets.QPushButton("P&ause")
self.btnPause.clicked.connect(self.mpPlayer.pause)
self.btnPause.setEnabled(False)
hbox.addWidget(self.btnPause)
self.btnStop = QtWidgets.QPushButton("&Stop")
self.btnStop.clicked.connect(self.mpPlayer.stop)
self.btnStop.setEnabled(False)
hbox.addWidget(self.btnStop)
vbox.addLayout(hbox)
# Volume control
hbox = QtWidgets.QHBoxLayout()
lblVolume = QtWidgets.QLabel("&Volume")
hbox.addWidget(lblVolume)
sldVolume = QtWidgets.QSlider(QtCore.Qt.Horizontal)
sldVolume.setRange(0, 100)
sldVolume.setTickPosition(QtWidgets.QSlider.TicksAbove)
sldVolume.setTickInterval(10)
sldVolume.setValue(50)
lblVolume.setBuddy(sldVolume)
sldVolume.valueChanged.connect(self.mpPlayer.setVolume)
hbox.addWidget(sldVolume)
btnMute = QtWidgets.QPushButton("&Mute")
btnMute.setCheckable(True)
btnMute.toggled.connect(self.mpPlayer.setMuted)
hbox.addWidget(btnMute)
vbox.addLayout(hbox)
self.setLayout(vbox)
self.resize(300, 100)
def openFile(self):
file = QtWidgets.QFileDialog.getOpenFileUrl(parent=self,
caption="Choose track file",
filter="Medial files (*.mp3 *.ac3)")
self.mpPlayer.setMedia(QtMultimedia.QMediaContent(file[0]))
def initPlayer(self, state):
if state == QtMultimedia.QMediaPlayer.LoadedMedia:
self.mpPlayer.stop()
self.btnPlay.setEnabled(True)
self.sldPosition.setEnabled(True)
self.slfPosition.setMaximum(self.mpPlayer.duration())
elif state == QtMultimedia.QMediaPlayer.EndOfMedia:
self.mpPlayer.stop()
elif state == QtMultimedia.QMediaPlayer.NoMedia or state == QtMultimedia.QMediaPlayer.InvalidMedia:
self.sldPosition.setValue(0)
self.sldPosition.setEnabled(False)
self.btnPlay.setEnabled(False)
self.btnPause.setEnabled(False)
self.btnStop.setEnabled(False)
def setPlayerState(self, state):
if state == QtMultimedia.QMediaPlayer.StoppedState:
self.sldPosition.setValue(0)
self.btnPause.setEnabled(False)
self.btnStop.setEnabled(False)
elif state == QtMultimedia.QMediaPlayer.PlayingState:
self.btnPause.setEnabled(True)
self.btnStop.setEnabled(True)
elif state == QtMultimedia.QMediaPlayer.PausedState:
self.btnPause.setEnabled(False)
self.btnPlay.setEnabled(True)
def main():
app = QtWidgets.QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())
main()
`