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

Play any audio media file with Phonon, via command line

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

I find it useful because it doesn't conflict with amarok or any application that uses Phonon (the other command-line utils I saw tried to use alsa directly)

Hope you like it. I called it 'phonon-play' and made an alias named 'pplay' to execute the script.

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:              # -1 stands for invalid file
            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
anda_skoa
KDE Developer
Posts
783
Karma
4
OS
Just out of curiosity: does it need the QApplication from QtGUI or could it also just use QCoreApplication from QtCore?

Cheers,
_


anda_skoa, proud to be a member of KDE forums since 2008-Oct.
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
By looking at the Phonon documentation, on doc.qt.nokia.com, it seems that it should be possible in theory to use QCoreApplication as none of the used classes are GUI classes.


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
eordano
Registered Member
Posts
6
Karma
0
OS
I'm a real noob here, you are probably absolutely right. Since it worked well, I left it as it is...


User avatar
astromme
KDE Developer
Posts
6
Karma
0
OS
Thanks, this is really useful!
User avatar
ivan
KDE Developer
Posts
918
Karma
14
OS
Fixed the Pnonom -> Phonon for the sakes of searching.


Image
yogieza
Registered Member
Posts
6
Karma
0
OS
i prefer using xmms2 for playing audio files via command line
StephanRay
Registered Member
Posts
1
Karma
0
Aweome tip mate, thanks.

Just signed up and already seeing the value here.
bobthedog3114
Registered Member
Posts
2
Karma
0
Just signed up and already seeing the value here.
I am a bit overwhelmed with the great info but will catch up and hopefully contribute more soon.
mtok
Registered Member
Posts
1
Karma
0
anda_skoa wrote:Just out of curiosity: does it need the QApplication from QtGUI or could it also just use QCoreApplication from QtCore?

Cheers,
_


Indeed, replacing the occurrence of QtGUI with QtCore and changing all QApplication to QCoreApplication still made my computer play the soundfile I passed to phonon-play.py
blie
Registered Member
Posts
2
Karma
0
OS
I was looking for this thank :D


Bookmarks



Who is online

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