Playing custom sounds

Custom sounds are saved in audio files, and you can use MediaPlayer to play audio files by following these steps:

  • Add the multimedia library by using the import bb.multimedia 1.2 import statement.
  • Create a MediaPlayer object.
  • Set the path as the source URL to the audio file that you want to play. This source path can be any URI, and can point to either a local and remote file.
  • Call the player's play() function to play the audio file.

When using BlackBerry 10 OS version 10.1 to perform the concurrent playback of multiple media items, such as local files or remote streams, you’re only guaranteed to have a maximum of 8 items playing at the same time.

Here's a code sample that shows you how to play and stop a custom audio file.

import bb.cascades 1.2
import bb.multimedia 1.2

Page {
    Container {
        layout: AbsoluteLayout {
        }
        
        attachedObjects:[
            MediaPlayer {
                id: audioPlayer
                sourceUrl: "/path/to/audio.wav"
            }
        ]
        
        Button {
            text: "Play Audio"
            onClicked: {
                if(audioPlayer.play() != MediaError.None) {
                    // Your error handling code here
                }
            }
        }
        
        Button {
            text: "Stop Audio"
            onClicked: {
                if(audioPlayer.stop() != MediaError.None) {
                    // Your error handling code here
                }
            }
        }
    }
}

Not applicable

Not applicable

Last modified: 2015-03-31



Got questions about leaving a comment? Get answers from our Disqus FAQ.

comments powered by Disqus