Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Posts: 67
Registered: ‎09-01-2012
My Device: Dev Alpha
My Carrier: Bell
Accepted Solution

How to play 2 sounds sequentially from one button using MediaPlayer?

Hi All,

 

This might be pretty simple and I'm just been staring at this long, hence I'm not getting it.  Basically I want to have one button and when I click on it, depending on the settings, can play one sound or two sounds (or 3 or 4..) sequentially.

 

I've declared my MediaPlayer:

       

MediaPlayer {

id: mPlayer       

},

 

Then in my code, I would dynamically set the SourceUrl:

       

if (SomeApp.getValueFor("Option1", "false") == "true") {

            mPlayer.setSourceUrl(item.someSound)

            mPlayer.play()

if (SomeApp.getValueFor("Option2", "false") == "true") {

mPlayer.setSourceUrl(item.anotherSound)

mPlayer.play()

}

.......

 

What happens right now is that the 2nd sound overrides the first sound.  I need some sort of pause or signal to have the first sound finish before the 2nd sound comes in.......

 

Thanks,

Developer
Posts: 1,008
Registered: ‎12-12-2010
My Device: Passport (Red Limited Edition)
My Carrier: Mobile Vikings

Re: How to play 2 sounds sequentially from one button using MediaPlayer?

Have you tried using the playbackCompleted() (doc) signal of the MediaPlayer class? Just connect to this signal, play the first sound and when that playback is completed play the second sound.

-------------------------------------------
BlackBerry Certified Builder for Native Application Development -- Proud member of the Belgian BlackBerry Developer group
Samples: Park in Ghent
Feeling generous? Nominate me for BB Elite member!
Developer
Posts: 67
Registered: ‎09-01-2012
My Device: Dev Alpha
My Carrier: Bell

Re: How to play 2 sounds sequentially from one button using MediaPlayer?

I had tried to do something with that signal, but since the sourceurl is dynamically being set in my playback function, I'm not sure how I can set it for onPlaybackCompleted.
Developer
Posts: 67
Registered: ‎09-01-2012
My Device: Dev Alpha
My Carrier: Bell

Re: How to play 2 sounds sequentially from one button using MediaPlayer?

Furthermore, if I have 4 options, option 1 doesn't necessarily have to play, nor 2, etc....
It seems simple.....I'm probably just missing something
Developer
Posts: 1,523
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: How to play 2 sounds sequentially from one button using MediaPlayer?

Pseudocode to show the idea:

 

Create QList<QString> mSoundsToPlay;

 

then when adding the sound to play do:

 

if (mSoundsToPlay.isEmpty())
{
  mSoundsToPlay.push_back(soundName);
  ...start playing soundName...
}
else
{
  mSoundsToPlay.push_back(soundName);
}

 

in playbackCompleted() handler:

 

mSoundsToPlay.pop_front();
if (!mSoundsToPlay.isEmpty())
{
  ...play next sound from mSoundsToPlay.front()...
}

 


Andrey Fidrya, @zmeyc on twitter
Developer
Posts: 67
Registered: ‎09-01-2012
My Device: Dev Alpha
My Carrier: Bell

Re: How to play 2 sounds sequentially from one button using MediaPlayer?

[ Edited ]

Excellent!  Thank you for the kickstarting my head....

 

I decided to create a separate class with QStringList as my storage variable to handle my sound play list and made the necessary read/write/status functions invokable in QML.

 

Then depending on my conditions, I add the location of the sound file to my QStringList and finally play it in the end.  Then using your logic in my onPlaybackCompleted, I would play all the sounds sequentially until it's done.

 

:Clap: