Question

I'm relatively new to Qt so I'll explain this question and what I hope to achieve as clear as possible.

Currently I have buttons that, when clicked, play a sound file located in the applications directory. I did a little research and found many users say using Phonon is better than QSound. This application is just for the Windows platform so nothing special is required.

Here's the code I have in mainwindow.cpp:

void MainWindow::on_obj_button_gandalf_clicked()
{
        Phonon::MediaObject *music =
        Phonon::createPlayer(Phonon::MusicCategory,Phonon::MediaSource("sound_file.mp3"));
        music->play();
}

The sound plays perfectly. However, if the user had to click the button a second time whilst the sound is still playing from the first click it plays one over the other.

Is there some sort of isplaying() function or something to determine if the same sound is indeed playing already? If so it shouldn't play it again, if not it should then play the song as requested.

I'm using Qt 4.7.0 32bit

Was it helpful?

Solution

Reading the documentation, it appears it's as simple as calling music->state() and ... checking the state.

if (music->state() == Phonon::PlayingState) {
    ...

You'll also note there's a number of Signals that would let you manage this in your application, specifically the stateChanged signal.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top