Help Center
Local Navigation
BlackBerry Manuals & Help
>
Developer Documentation
>
Java Development Guides and API Reference
>
Development Guide
Multimedia - BlackBerry Java SDK - 6.0
Play audio in a BlackBerry device application
-
Import the required classes and interfaces.
import net.rim.device.api.ui.*; import net.rim.device.api.ui.container.*; import net.rim.device.api.ui.component.*; import javax.microedition.media.*; import javax.microedition.media.control.*; import java.io.*
-
Create the application framework by extending the
UiApplication class. In
main(), create an instance of the new class and
invoke
enterEventDispatcher() to enable the application to
receive events. In the application constructor, invoke
pushScreen() to display the custom screen for the
application. The
AudioPlaybackDemoScreen class, described in step 3,
represents the custom screen.
public class AudioPlaybackDemo extends UiApplication { public static void main(String[] args) { AudioPlaybackDemo app = new AudioPlaybackDemo(); app.enterEventDispatcher(); } public AudioPlaybackDemo() { pushScreen(new AudioPlaybackDemoScreen()); } } -
Create the framework for the custom screen by extending the
MainScreen class.
class AudioPlaybackDemoScreen extends MainScreen { public AudioPlaybackDemoScreen() { } } -
In the screen constructor, in a try/catch block, create an
instance of the
Player class by invoking
Manager.createPlayer(String), and passing in the
location of the audio file to play.
try { Player p = javax.microedition.media.Manager.createPlayer("http://abc.com/sounds/abc.wav"); } catch(MediaException me) { Dialog.alert(me.toString()); } catch(IOException ioe) { Dialog.alert(ioe.toString()); } -
To control an aspect of playback, retrieve the appropriate
Control object. Invoke the
Player object's
realize() method to access its associated
Control object. The following code sample
demonstrates how to retrieve the
VolumeControl object and set the volume level of
playback.
try { Player p = javax.microedition.media.Manager.createPlayer("http://abc.com/sounds/abc.wav"); p.realize(); VolumeControl volume = (VolumeControl) p.getControl("VolumeControl"); volume.setLevel(30); } catch(MediaException me) { Dialog.alert(me.toString()); } catch(IOException ioe) { Dialog.alert(ioe.toString()); } -
Invoke
Player.start() to start playback. Invoking
Player.start() implicitly performs all the necessary
state transitions. In the following code sample, both
realize() and
prefetch() are invoked to demonstrate how to
explicitly initialize the
Player object before starting playback.
try { Player p = javax.microedition.media.Manager.createPlayer("http://abc.com/sounds/abc.wav"); p.realize(); VolumeControl volume = (VolumeControl)p.getControl("VolumeControl"); volume.setLevel(30); p.prefetch(); p.start(); } catch(MediaException me) { Dialog.alert(me.toString()); } catch(IOException ioe) { Dialog.alert(ioe.toString()); }
Previous topic: Playing audio