This forum has been archived. All content is frozen. Please use KDE Discuss instead.

How to play sound from commandline?

Tags: None
(comma "," separated)
eordano
Registered Member
Posts
6
Karma
0
OS
I have written this little script to play a sound using Phonon.

Hope you like it.

Code: Select all
#! /usr/bin/env python
from PyQt4.phonon import Phonon
from PyQt4.QtGui import QApplication
from PyQt4.QtCore import SIGNAL, SLOT
from PyQt4.QtCore import QFile
import sys
import signal


def main():
    signal.signal(signal.SIGINT, signal.SIG_DFL)
    app = QApplication(sys.argv)
    QApplication.setApplicationName('phonon-play')

    media = Phonon.MediaObject()
    audio = Phonon.AudioOutput(Phonon.MusicCategory)
    Phonon.createPath(media, audio)
    f = QFile(sys.argv[1])
    if f.exists():
        source = Phonon.MediaSource(sys.argv[1])
        if source.type() != -1:
            media.setCurrentSource(source)
            app.connect(media, SIGNAL("finished()"), app, SLOT("quit()"))
            media.play()
            return app.exec_()
        else:
            return -2
    else:
        return -1

if __name__ == '__main__':
    sys.exit(main())


User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
@eordano: Some users may definitely find this helpful. You are welcome to post it in the Tutorials & Tips section of the Forum.


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
eordano
Registered Member
Posts
6
Karma
0
OS
Okey, will do. I'm glad you found it interesting.




Bookmarks



Who is online

Registered users: bartoloni, Bing [Bot], Google [Bot], Sogou [Bot]