Play audio in a BlackBerry device application

  1. 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.*
  2. 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());
         }
    }
  3. Create the framework for the custom screen by extending the MainScreen class.
    class AudioPlaybackDemoScreen extends MainScreen
    {	
         public AudioPlaybackDemoScreen()
         {
         }
    }
  4. 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());
    }
  5. 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());
    }
  6. 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

Was this information helpful? Send us your comments.