Reputation: 129
I have raspberry pi3. I want to display any type of video using phonon+qt+python. I don't want to use c++ in QT. I had tried using following code, but it is giving error like: No module named phonon. I already install phonon using sudo apt-get install phonon, but still it is giving error like that. Any one know how to solve that or is there any other way to display video using python + qt or is there any other way to install phonon?
from PyQt4 import QtGui, QtCore
import PyQt4.phonon
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.media = phonon.MediaObject(self)
self.media.stateChanged.connect(self.handleStateChanged)
self.video = phonon.VideoWidget(self)
self.video.setMinimumSize(400, 400)
self.audio = phonon.AudioOutput(phonon.VideoCategory, self)
phonon.createPath(self.media, self.audio)
phonon.createPath(self.media, self.video)
self.button = QtGui.QPushButton('Choose File', self)
self.button.clicked.connect(self.handleButton)
self.list = QtGui.QListWidget(self)
self.list.addItems(phonon.BackendCapabilities.availableMimeTypes())
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.video, 1)
layout.addWidget(self.button)
layout.addWidget(self.list)
def handleButton(self):
if self.media.state() == phonon.PlayingState:
self.media.stop()
else:
path = QtGui.QFileDialog.getOpenFileName(self, self.button.text())
if path:
self.media.setCurrentSource(phonon.MediaSource(path))
self.media.play()
def handleStateChanged(self, newstate, oldstate):
if newstate == phonon.PlayingState:
self.button.setText('Stop')
elif (newstate != phonon.LoadingState and
newstate != phonon.BufferingState):
self.button.setText('Choose File')
if newstate == phonon.ErrorState:
source = self.media.currentSource().fileName()
print ('ERROR: could not play:', source.toLocal8Bit().data())
print (' %s' % self.media.errorString().toLocal8Bit().data())
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
app.setApplicationName('phonon Player')
window = Window()
window.show()
sys.exit(app.exec_())
Upvotes: 1
Views: 1170
Reputation: 244282
You must install phonon for pyqt with the following command:
sudo apt-get install python-qt4-phonon
In addition you must correct certain imports and part of your code:
from PyQt4 import QtGui
from PyQt4.phonon import Phonon
class Window(QtGui.QWidget):
def __init__(self, parent=None):
super(Window, self).__init__(parent=parent)
self.media = Phonon.MediaObject(self)
self.video = Phonon.VideoWidget(self)
self.video.setMinimumSize(400, 400)
self.audio = Phonon.AudioOutput(Phonon.VideoCategory, self)
Phonon.createPath(self.media, self.audio)
Phonon.createPath(self.media, self.video)
self.button = QtGui.QPushButton('Choose File', self)
self.list = QtGui.QListWidget(self)
self.list.addItems(Phonon.BackendCapabilities.availableMimeTypes())
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.video, 1)
layout.addWidget(self.button)
layout.addWidget(self.list)
self.media.stateChanged.connect(self.handleStateChanged)
self.button.clicked.connect(self.handleButton)
def handleButton(self):
if self.media.state() == Phonon.PlayingState:
self.media.stop()
else:
path = QtGui.QFileDialog.getOpenFileName(self, self.button.text())
if path:
self.media.setCurrentSource(Phonon.MediaSource(path))
self.media.play()
def handleStateChanged(self, newstate, oldstate):
if newstate == Phonon.PlayingState:
self.button.setText('Stop')
elif newstate != Phonon.LoadingState and newstate != Phonon.BufferingState:
self.button.setText('Choose File')
if newstate == Phonon.ErrorState:
source = self.media.currentSource().fileName()
print('ERROR: could not play:', source.toLocal8Bit().data())
print(' %s' % self.media.errorString().toLocal8Bit().data())
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
app.setApplicationName('phonon Player')
window = Window()
window.show()
sys.exit(app.exec_())
Screenshot:
Upvotes: 1