Code sample: Playing media in a BlackBerry device application

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.*

public class AudioPlaybackDemo extends UiApplication
{
    public static void main(String[] args)
    {
         AudioPlaybackDemo app = new AudioPlaybackDemo();
         app.enterEventDispatcher();
    }
    
    public AudioPlaybackDemo()
    {
         pushScreen(new AudioPlaybackDemoScreen());
    }

    private class AudioPlaybackDemoScreen extends MainScreen
    {	
        public AudioPlaybackDemoScreen()
        {
            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());
            }
         }
     }
}

Was this information helpful? Send us your comments.