Audio Playback
Classes from the javax.microedition.media package will be used to play audio on Blackberry devices. This package is compatible with the MMAPI.
Manager class
The Manager class is part of the javax.microedition.media package and is responsible from media handling. This class is needed to get access to the system resources such as the Player for multimedia processing. The Manager class can create a Player object and it can also query the player for specific details like supported media contents.
Content types
Content types identify the type of the media type and are registered as the mime types. Common audio types are audio/mpeg (for files with .mp3 extension) and audio/midi (for files with .midi extension).
We can use the method
public static String[] getSupportedProtocols(String content_type)
to find out which protocols specific content types support on the device we can use.
Playing Single Tone
To play a single tone, the following method can be used.
public static void playTone(int note, int duration, int volume) throws MediaException
where:
- SEMITONE_CONST = 17.31234049066755 = 1/(ln(2^(1/12)))
- note = ln(freq/8.176)*SEMITONE_CONST
- The musical note A = MIDI note 69 (0x45) = 440 Hz
- duration is note duration in milliseconds, and
- volume is in range from 0 to 100.
Creating a Player
To create a player that plays audio files, the following method can be used.
PlayerManager.createPlayer(String url);
where the url is the location of the media file.
Player class
The Player class has a life-cycle consisting of 5 states – unrealized, realized, prefetched, started and closed. Once a Player is created, it is in the unrealized state. When it is created, it tries to connect to the audio file. When the system finds the source, the Player moves to the realized state. When the Player is ready to play the audio, it moves to the prefetched state. If the Player is started in this state, it enters the started state. From this state, the Player can either be moved to prefetched state or can be closed.
Audio Sample Application
In this example, I will create an application and play audio files from the Blackberry simulator. I will create a new project and call it AudioVideo, and add a new mp3 file as a resource. How the file is imported and the code to run the application is shown in the screen shots below.
When the application is run, the audio file starts playing and the list of supported media types are displayed as shown below.
Video Playback
Video files can be played in the application using the Manager and Player classes. Videos are larger in size and should be played from the SD Card.
Mobile devices have smaller screen and smaller files are sufficient to be played in mobile devices.
I have added a new class called Video.java to play the video file. The code for the class is below.
Note that, I am calling the video file from the SD Card. To set the SD Card, click on the Simulate menu, select change card and add the directory that contains the video file as shown below.
When the application is run, the video starts playing in the simulator.
No comments:
Post a Comment