PDA

View Full Version : Phonon: Play multiple files



alenn.masic
14th September 2012, 15:35
I have three sound files (.wav) which I need to play on button pressed. I tried changing source for those three files and to invoke mediaObject->play() but I get choppy sound. When I play only one sound everything is ok.

Can anyone help how to resolve this.

Added after 30 minutes:


pzvuk = new Phonon::MediaObject(this);

.
.
.
void MainWindow::zvuk1()
{

pzvuk->setCurrentSource(Phonon::MediaSource("/home/alen/QtSDK/writer/suma.wav"));
ozvuk = new Phonon::AudioOutput(Phonon::MusicCategory, this);
path = Phonon::createPath(pzvuk, ozvuk);
connect(pzvuk, SIGNAL(aboutToFinish()), this, SLOT(ponoviZ()));
pzvuk->play();
}
void MainWindow::zvuk2(){

pzvuk->setCurrentSource(Phonon::MediaSource("/home/alen/QtSDK/writer/oluja.wav"));
ozvuk = new Phonon::AudioOutput(Phonon::MusicCategory, this);
path = Phonon::createPath(pzvuk, ozvuk);
connect(pzvuk, SIGNAL(aboutToFinish()), this, SLOT(ponoviZ()));
pzvuk->play();
}
void MainWindow::zvuk3(){

pzvuk->setCurrentSource(Phonon::MediaSource("/home/alen/QtSDK/writer/ocean.wav"));
ozvuk = new Phonon::AudioOutput(Phonon::MusicCategory, this);
path = Phonon::createPath(pzvuk, ozvuk);
connect(pzvuk, SIGNAL(aboutToFinish()), this, SLOT(ponoviZ()));
pzvuk->play();
}

this is my code, what am I doing wrong

EDIT: Fixed it. I just declared mediaObject, AudioOutput and Path in constructor, and now it's working ok.

d_stranz
14th September 2012, 19:51
EDIT: Fixed it. I just declared mediaObject, AudioOutput and Path in constructor, and now it's working ok.

And so, do you understand why your original code was wrong and why your fix worked?

alenn.masic
14th September 2012, 20:22
no, haha, I just made it work :)

d_stranz
15th September 2012, 03:33
no, haha, I just made it work

So, OK, I'm going to assume that maybe you aren't joking here, and maybe you might not really understand what was wrong. But even if you do know, someone else reading this might not.

In each one of your cleverly named slots (zvuk1(), zvuk2(), and zvuk3() - the purpose of those functions will be obvious to anyone who comes along later to read your code... including you next year, I'll bet) you are creating a new instance of the Phonon::AudioOutput and connecting it up. Whatever the original ozvuk pointer was, it is now lost because you have reassigned the variable to a new pointer. So, every time you push a button, you create a new AudioOutput instance without getting rid of the previous one. Depending on how many times you clicked the button, you could have created dozens of them, all trying to play the same thing at the same time.

If you were doing the same thing with the Phonon::MediaObject and path instances, then you probably had multiple copies of them hanging around like zombies too, looking for necks to bite.

By moving it all into the constructor, now you have only one of each thing, and you simply reassign the media source in each slot.

By the way, if you didn't move the "connect" to the constructor also, you're still creating problems because a new connection will be added for each click, and every one of those aboutToFinish() signals will be getting processed multiple times. Qt doesn't care if you make multiple connections between the same pair of objects and signal/slot; it will just line them all up and call it over and over until it reaches the end of the list. Every click just makes the list longer by one.

alenn.masic
15th September 2012, 17:41
those names for functions and variables are on my native language, so it is understandable for anyone comming from my region. Thank you for explanation, didn't know that :)

d_stranz
15th September 2012, 19:21
those names for functions and variables are on my native language

Yes, I know that, but even in English short names for functions usually don't describe very well what the function is doing. If I were to call my slots "sound1", "sound2", and "sound3", it would have no real meaning to me later. If instead, I changed the name "sound3" to "playOceanSound" then I wouldn't have to look at the code to know exactly what the function will do.

Calling my variables pMediaObj and pAudioOut makes it very clear what these variables are, instead of "pSound" and "oSound".

No one has the ability to remember all the code they write, so using names for things that help you understand what the code is doing can save you hours of time later when you have to change something. Much worse if you go back in a few months and find that you named all of your methods and variables "soundThis" and "soundThat" and "sound1, 2, or 3". "sound3"? Now what does that mean?