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: 573
Registered: ‎10-17-2010
My Device: (BlackBerry Z10)-> Q10/Passport Dual Use
Accepted Solution

Playing Sounds in Sequence using MediaPlayer

Hello. How do I play sounds in sequence?

 

At the moment, here's my code.

 

I have a MediaPlayer Component as an attached object

http://developer.blackberry.com/native/reference/cascades/bb__multimedia__mediaplayer.html

 

attachedObjects :[
    MediaPlayer{
       id: sound
       sourceUrl: "asset:///sounds/sound1.mp3"
    }
]

 

Then I have a button. Here's what my code for that

 

Button{
   id: play
   text: "Play Sequence"

   onClicked :{
      

      sound.sourceUrl = "asset:///sounds/sound1.mp3"
      sound.play();
      sound.sourceUrl = "asset:///sounds/sound2.mp3"
      sound.play();
      sound.sourceUrl = "asset:///sounds/sound3.mp3"
      sound.play();

   }
}

 

 

What's going wrong.

 

The code is executing after about 1 second cutting off the next sound. I'm trying to make the next sound play after the previous sound has finished playing.

 

:Beta1:

Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: Playing Sounds in Sequence using MediaPlayer

Either set up a playlist or tie in to the playbackcompleted signal...

 

http://developer.blackberry.com/native/reference/cascades/bb__multimedia__mediaplayer.html#function-...


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 573
Registered: ‎10-17-2010
My Device: (BlackBerry Z10)-> Q10/Passport Dual Use

Re: Playing Sounds in Sequence using MediaPlayer

And how do I do play the sound one after the other?  I did notice a playSequence hint pop up as an argument to the .play() method but there is NO documentation on how this works.

 

 

Developer
Posts: 573
Registered: ‎10-17-2010
My Device: (BlackBerry Z10)-> Q10/Passport Dual Use

Re: Playing Sounds in Sequence using MediaPlayer

var thetracks = new Array("asset:///sounds/n1.mp3","asset:///sounds/followedby.mp3","asset:///sounds/n2.mp3","asset:///sounds/followedby.mp3","asset:///sounds/n3.mp3")
selectedSound.playlist(thetracks)

 

Playlist Function

 

  function playlist(tracksArray){
                    for(var i=1;i<tracksArray.length;i++){
                        selectedSound.sourceUrl = tracksArray[i];
                        selectedSound.play();
                    } 
                }

 

Having the same issue. The sounds are playing in sequence but they are not playing for the full duration. Each sound is being cut off by the next sound...

 

:Helpsmilie:

 

Developer
Posts: 573
Registered: ‎10-17-2010
My Device: (BlackBerry Z10)-> Q10/Passport Dual Use

Re: Playing Sounds in Sequence using MediaPlayer

Still stuck. If anyone would please chime in here with a solution, I would greatly appreciate it. Trying to release my app before the Dec 18th deadline.

 

 

Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: Playing Sounds in Sequence using MediaPlayer

[ Edited ]

The reason the sounds are not being finished is that play() is not a blocking call. Once you have called the function play() it will start playing the sound and return immediately to the following code line in the QML. If the next line is a call to play() again this will take precedence over the previous sound, which if you think about it logically is what you need in an app, you wouldn't want to have the sound of a doorbell play 2 seconds later just because you are already playing a sound, you would want it to interrupt, as it does.

 

My suggestion to use a playlist meant to use the playlist function of MediaPlayer, what you have implemented will obviously suffer from the same problem because of the reason given above. A playlist is a seperate file that contains a list of the media you wish to play and can be in several different formats.

 

My other suggestion of using onPlaybackCompleted doesn't seem to have been followed up by you but is probably the easiest to implement.

 

MediaPlayer {
    id: mediaPlayer
		            
    onPlaybackCompleted: {
count = count + 1; var s = count; mediaPlayer.setSourceUrl("asset:///sounds/sound" + s + ".mp3"); mediaPlayer.play(); } }

 Where count is a global property, set to 1, in your QML file and you have some trigger (i.e. onCreationCompleted or Button click) to start the first play().

 

 


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 573
Registered: ‎10-17-2010
My Device: (BlackBerry Z10)-> Q10/Passport Dual Use

Re: Playing Sounds in Sequence using MediaPlayer

I will accept your answer as the solution, even though when I tried implementing it did not work for me.

I decided to code this the hacky way using a QTimer, and a strings denoting the duration for each sound file.