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 QML 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
                }
            }
        }
    }
}

Last modified: 2013-12-20

comments powered by Disqus