Results 1 to 6 of 6

Thread: Phonon: Play multiple files

  1. #1
    Join Date
    Aug 2012
    Posts
    55
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Phonon: Play multiple files

    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:


    Qt Code:
    1. pzvuk = new Phonon::MediaObject(this);
    2.  
    3. .
    4. .
    5. .
    6. void MainWindow::zvuk1()
    7. {
    8.  
    9. pzvuk->setCurrentSource(Phonon::MediaSource("/home/alen/QtSDK/writer/suma.wav"));
    10. ozvuk = new Phonon::AudioOutput(Phonon::MusicCategory, this);
    11. path = Phonon::createPath(pzvuk, ozvuk);
    12. connect(pzvuk, SIGNAL(aboutToFinish()), this, SLOT(ponoviZ()));
    13. pzvuk->play();
    14. }
    15. void MainWindow::zvuk2(){
    16.  
    17. pzvuk->setCurrentSource(Phonon::MediaSource("/home/alen/QtSDK/writer/oluja.wav"));
    18. ozvuk = new Phonon::AudioOutput(Phonon::MusicCategory, this);
    19. path = Phonon::createPath(pzvuk, ozvuk);
    20. connect(pzvuk, SIGNAL(aboutToFinish()), this, SLOT(ponoviZ()));
    21. pzvuk->play();
    22. }
    23. void MainWindow::zvuk3(){
    24.  
    25. pzvuk->setCurrentSource(Phonon::MediaSource("/home/alen/QtSDK/writer/ocean.wav"));
    26. ozvuk = new Phonon::AudioOutput(Phonon::MusicCategory, this);
    27. path = Phonon::createPath(pzvuk, ozvuk);
    28. connect(pzvuk, SIGNAL(aboutToFinish()), this, SLOT(ponoviZ()));
    29. pzvuk->play();
    30. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by alenn.masic; 14th September 2012 at 14:44.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Phonon: Play multiple files

    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?

  3. #3
    Join Date
    Aug 2012
    Posts
    55
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Phonon: Play multiple files

    no, haha, I just made it work

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Phonon: Play multiple files

    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.

  5. #5
    Join Date
    Aug 2012
    Posts
    55
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Phonon: Play multiple files

    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

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Phonon: Play multiple files

    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?

  7. The following user says thank you to d_stranz for this useful post:

    alenn.masic (15th September 2012)

Similar Threads

  1. Phonon can't play the video
    By leegoe in forum Qt Programming
    Replies: 1
    Last Post: 8th June 2012, 02:45
  2. Phonon videoplayer doesn't play some files
    By SepticInsect in forum Qt Programming
    Replies: 5
    Last Post: 25th January 2012, 11:56
  3. Playing multiple audio files with Phonon
    By MartinWalter in forum Qt Programming
    Replies: 0
    Last Post: 23rd December 2010, 11:19
  4. Cnan't play mp3 with phonon
    By Skepsor in forum Qt Programming
    Replies: 11
    Last Post: 11th June 2010, 14:54
  5. Multiple sound files in Phonon Audio
    By brent99 in forum Qt Programming
    Replies: 0
    Last Post: 8th March 2010, 21:26

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.