[PyQt] multiple sound files in queue in phonon

Jan Haag haag498 at googlemail.com
Sat Jan 2 22:39:47 GMT 2010


On Jan 2, 2010, at 10:57 PM, Haarman wrote:

> Hi,
>
> I am trying to figure out how I can add wav files to phonon so it will
> just play them all in the order added. Somehow phonon just plays one
> file and then just a very small part of another. Anyone knows what I
> am doing wrong here?
>
> # -*- coding: utf-8 -*-
>
> import sys, time
>
> from PyQt4.QtGui import QApplication, QMainWindow
> from PyQt4.QtGui import QFrame
> from PyQt4.QtCore import SIGNAL, QObject
> from PyQt4.phonon import Phonon
>
> class MainWindow(QMainWindow):
>
>
>    def __init__(self):
>        QMainWindow.__init__(self)
>        self.sounds = [
>            #' /usr/share/kde4/apps/korganizer/sounds/onscreen.wav',
>            #' /usr/share/kde4/apps/korganizer/sounds/onscreen.wav',
>            #' /usr/share/kde4/apps/korganizer/sounds/onscreen.wav',
>            '/usr/share/psi/sound/ft_complete.wav',
>            '/usr/share/psi/sound/online.wav',
>            '/usr/share/psi/sound/ft_incoming.wav',
>            ]
>
>        self.m_media = Phonon.MediaObject(self)
>        audioOutput = Phonon.AudioOutput(Phonon.MusicCategory, self)
>        Phonon.createPath(self.m_media, audioOutput)
>
>        QObject.connect(self.m_media, SIGNAL('aboutToFinish()'),
> self.enqueueNextSource)
>        QObject.connect(self.m_media,
> SIGNAL('stateChanged(Phonon::State, Phonon::State)'),
> self.stateChanged)
>
>        self.m_media.setCurrentSource(Phonon.MediaSource 
> (Phonon.MediaSource(self.sounds.pop())))
>
>        self.m_media.play()

This is what I did in a similar situation:

self.player = Phonon.createPlayer(4, Phonon.MediaSource(self.sounds.pop 
()))
self.player.tick.connect(lambda a, b: pass)
self.player.aboutToFinish.connect(self.enqueueNextSource)
self.player.stateChanged.connect(self.stateChanged)
self.player.play()

This uses the new-style connect API, which I personally found more  
"readable"...
The only difference is the connection to the "tick" signal of the  
player. It takes a callable,
in this case one that returns immediately.
That fixed the problem for me.

Jan

>
>
>    def stateChanged(self, newState, oldState):
>         print 'stateChanged from %s to %s' % (oldState, newState)
>         if oldState == Phonon.LoadingState and newState ==  
> Phonon.StoppedState:
>             print 'play'
>             self.m_media.play()
>             pass
>
>         if newState == Phonon.ErrorState:
>             print self.m_media.errorType()
>
>
>    def enqueueNextSource(self):
>        print self.m_media.currentSource
>
>        if len(self.sounds) > 0:
>            self.m_media.enqueue(Phonon.MediaSource(Phonon.MediaSource 
> (self.sounds.pop())))
>
>
> def main():
>    app = QApplication(sys.argv)
>    QApplication.setApplicationName("Phonon test")
>    mw = MainWindow()
>    mw.show()
>    sys.exit(app.exec_())
>
>
> if __name__ == '__main__':
>    main()
>
> thx!



More information about the PyQt mailing list