[PyQt] Phonon hasVIdeo problem
Gionata Boccalini
gion at ngi.it
Wed Mar 9 16:47:10 GMT 2011
Hi everyone,
I need to put a video widget in my application, which is made with
Phonon framework, and has a button to load a file into the viewer. The
class is simply:
class VideoWidget(QWidget):
def __init__(self, parent = None):
super(VideoWidget, self).__init__(parent)
self.__fileName = ""
openButton = QPushButton(self.tr("Open..."))
self.__playButton = QPushButton()
self.__playButton.setEnabled(False)
self.__playButton.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
self.__positionSlider = Phonon.SeekSlider(self)
self.__positionSlider.setIconVisible(False)
self.__positionSlider.show()
controlLayout = QHBoxLayout()
controlLayout.setMargin(0)
controlLayout.addWidget(openButton)
controlLayout.addWidget(self.__playButton)
controlLayout.addWidget(self.__positionSlider)
self.__player = Phonon.VideoWidget()
self.__player.setSizePolicy(QSizePolicy.Expanding,
QSizePolicy.Expanding)
audioOutput = Phonon.AudioOutput(Phonon.VideoCategory, self)
self.__mediaObj = Phonon.MediaObject()
Phonon.createPath(self.__mediaObj, self.__player)
Phonon.createPath(self.__mediaObj, audioOutput);
self.__positionSlider.setMediaObject(self.__mediaObj)
layout = QVBoxLayout()
layout.addWidget(self.__player)
layout.addLayout(controlLayout)
self.setLayout(layout)
# Signals
self.__playButton.clicked.connect(self.play)
openButton.clicked.connect(self.open)
self.__mediaObj.hasVideoChanged.connect(self.hasVideoChanged)
I have two slots and a callable used from the outside of this class:
@QtCore.pyqtSlot("bool")
def hasVideoChanged(self, res):
if self.__mediaObj.hasVideo():
self.__playButton.setEnabled(True)
@QtCore.pyqtSlot()
def play(self):
if not self.__mediaObj.state() == Phonon.PlayingState:
self.__mediaObj.play()
self.__playButton.setIcon(self.style().standardIcon(QStyle.SP_MediaPause))
else:
self.__mediaObj.pause()
self.__playButton.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
def openFile(self, filename):
if not fileName.isEmpty():
self.__playButton.setEnabled(False)
self.__fileName = fileName
self.__mediaObj.setCurrentSource(Phonon.MediaSource(self.__fileName))
With the "openFile" method one can load a file (of any type, even a
non-video file) and try to play it with the widget. I set the play
button disabled by default, then, if the MediaObject has a video frame,
the slot "hasVideoChanged" is ran and the play button is set enabled.
So the playButton is enabled only when a correct video file is ready to
be played. But when the file isn't a video file, let's say a text file,
the slot hasVideoChanged is called again and the method
self.__mediaObj.hasVideo() returns TRUE!! Since this time the source
file is not a video, that method should return FALSE! Maybe I have
misunderstood the documentation but hasVideo() should return true only
when there is a real video to be played in the file that was set in the
media source...
As an alternative I could check the MIME type of the file before
loading, but I prefered the solution integrated with Phonon!
Thanks in advance for your help!
Gionata Boccalini
More information about the PyQt
mailing list