¿Cómo tocar una canción simple usando Phonon de PyQt?

I'm trying to play a single mp3 file on my software when a button is pressed. Here's how i did it:

QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), playsong)

y la función:

def playsong():            
        m_media = Phonon.MediaObject()
        m_media.setCurrentSource(Phonon.MediaSource("files/song.mp3"))
        m_media.play()

This doesnt raise any error. But the song wont play. I've seen a lot of exemples in C++ and some in python which has a lot of songs, and playlist, etc. I just want to play a single song, am i missing something?

preguntado el 08 de noviembre de 11 a las 12:11

2 Respuestas

I use phonon on pyqt and my code has a few more lines:

output = Phonon.AudioOutput(Phonon.MusicCategory)
m_media = Phonon.MediaObject()
Phonon.createPath(m_media, output)
m_media.setCurrentSource(Phonon.MediaSource("files/song.mp3"))
m_media.play()

But honestly, I've found out that phonon is not able to play some specific songs with weird ID3 tags, so I've switched to pyaudiere (https://pypi.python.org/pypi/pyaudiere), which is much more stable (but yes, its less integrated with Qt).

contestado el 22 de mayo de 13 a las 11:05

Here's a simple music player example:

from PyQt4 import QtGui, QtCore
from PyQt4.phonon import Phonon

class Window(QtGui.QPushButton):
    def __init__(self):
        QtGui.QPushButton.__init__(self, 'Choose File')
        self.mediaObject = Phonon.MediaObject(self)
        self.audioOutput = Phonon.AudioOutput(Phonon.MusicCategory, self)
        Phonon.createPath(self.mediaObject, self.audioOutput)
        self.mediaObject.stateChanged.connect(self.handleStateChanged)
        self.clicked.connect(self.handleButton)

    def handleButton(self):
        if self.mediaObject.state() == Phonon.PlayingState:
            self.mediaObject.stop()
        else:
            path = QtGui.QFileDialog.getOpenFileName(self, self.text())
            if path:
                self.mediaObject.setCurrentSource(Phonon.MediaSource(path))
                self.mediaObject.play()

    def handleStateChanged(self, newstate, oldstate):
        if newstate == Phonon.PlayingState:
            self.setText('Stop')
        elif newstate == Phonon.StoppedState:
            self.setText('Choose File')
        elif newstate == Phonon.ErrorState:
            source = self.mediaObject.currentSource().fileName()
            print 'ERROR: could not play:', source.toLocal8Bit().data()

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    app.setApplicationName('Phonon')
    win = Window()
    win.resize(200, 100)
    win.show()
    sys.exit(app.exec_())

respondido 08 nov., 11:21

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.