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

Posts: 29
Registered: ‎05-22-2012
My Device: any BB10 released
My Carrier: Airtel

How to play sounds simultaneously with single ImageButton?

i'm building up an instrument app which is completed overall but needs some improvements for perfection. I created an ImageButton linking to a sound & it works well. But when i press the button again while the first invoked sound is still playing, it behaves abnormally. is there anyway to merge sounds of same button when hitting the key several times?



Posts: 127
Registered: ‎02-27-2012
My Device: Bold
My Carrier: Rogers

Re: How to play sounds simultaneously with single ImageButton?


Hi shahriyarali,


By "merge sounds of same button when hitting the key several times" <-- are you implying that :


1) you want to simply *reset* the playback on the 2nd or any consecutive key presses, if the sound is still playing.




2) you just want to ignore the consecutive key presses while the playback is still going on?


Either way, for both #1 and #2, it sounds like you will have to handle that logic of stopping, pausing or resetting your playback from your button signals - while keeping track if your MediaPlayer instance is still playing. That is, you would have to make one property of your MediaPlayer instance to keep track of it's playback state -> then use it to control the state from the Button component's signal by pausing or resetting the media player.


For example, if you try to implement #1 scenario (resetting the playback), it would be something like the following:

Button { id: playButton text: "Play" onClicked: { if (! myPlayer.isPlaying) { var result = myPlayer.play(); if (result != MediaError.MediaErrorNone) {
//TODO: Error handling } } else { var result = myPlayer.seekTime(0,0); // SEEKING TO THE BEGINNING POSITION
if (result != MediaError.MediaErrorNone) { //TODO: Error handling } } } .......
attachedObjects: [ MediaPlayer { id: myPlayer // URL with no spaces sourceUrl: "/accounts/1000/shared/music/sound.mp3"
property bool isPlaying: false // <---- creating the property here to keep track of the playback state
onMediaStateChanged: { if (myPlayer.mediaState == MediaState.Started) {
playButton.text = "Pause" isPlaying = true; // <-------- updating the playback state here
} else if (myPlayer.mediaState == MediaState.Paused) {
playButtonUrl.text = "Play" isPlaying = false; // <--------- updating the playback state here } // onMediaStateChanged } } // MediaPlayer


Note that for the #2 scenario, it should be even simpler - you just ignore the button signal if the MediaPlayer instance is still playing


Let us know if that helped (or if I totally misunderstood your question & rambled on for no reason Smiley Happy).