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

How to play sound from commandline?

Tags: None
(comma "," separated)
L_V
Banned
Posts
104
Karma
-3
OS
Ok thank you.
I understand there is no replacement for artsplay in KDE4.
It is more by curiosity to understand how Dolphin is able to preview sound files without any basic player, just directly talking to libraries.
As I am not a developer, aplay command from alsa-utils will do the trick.
Thanks.

But very last question....
Thunderbird is able to play new coming mail sounds.
Thunderbird is not a KDE4 program, and nothing special has been develop to integrate Thunderbird in KDE4.
How Thunderbird can guess how KDE4 is now playing sounds ?
User avatar
Ignacio Serantes
Registered Member
Posts
453
Karma
1
OS
Thunderbird probably play sound using ALSA.

Phonon uses an engine (currently Xine or GStreamer) and this engine uses diferent methods to play sounds: ALSA, OSS, PulseAudio, etc....


Ignacio Serantes, proud to be a member of KDE forums since 2008-Nov.
L_V
Banned
Posts
104
Karma
-3
OS
"Thunderbird probably play sound using ALSA."

aplay is part of alsa-utils package which is optional.
Thunderbird plays sounds without this optional package.
Then, same mystery as kde4 sound preview command.

How programs play sounds directly talking to librairies ?
One needs to be a developer to understand the mechanism.

I will live without understanding, but the most important: it works fine !

Thanks.
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
Just an extra piece of information: "aplay" likely uses the very same libraries that Phonon and Thunderbird use to play sound. A library is just a unit of precompiled code with standard interfaces ( header files ).


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
User avatar
anda_skoa
KDE Developer
Posts
783
Karma
4
OS
L_V wrote:How programs play sounds directly talking to librairies ?
One needs to be a developer to understand the mechanism.


It is actually not that difficult to understand. I'll try to explain:

To play a sound file, an application has to read the sound data from the file.
The sound data is most likely encoded, e.g. mp3 or ogg vorbis, so it has to be passed to a decoder.
After that it can be passed to a sound device, which will then pass it to the audio hardware.

A player program like aplay does all this on its own, a KDE application like Dolphin just passes the file name to the KDE libraries (in this case Phonon) and it will then do the reading and forwarding of the data.

So all sound playing applications to some of these steps on their own, at some point their data ending up in the same destination.

It is not important if one application knows how another application does any of these steps, they do it independently from each other.

Cheers,
_


anda_skoa, proud to be a member of KDE forums since 2008-Oct.
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: bancha, Bing [Bot], Evergrowing, Google [Bot], lockheed, mesutakcan