Help Center

Local Navigation

Record audio in a BlackBerry device application

A BlackBerry® device can record audio in four formats: Adaptive Multi-Rate (AMR), 8 kHz mono-16-bit pulse code modulation (PCM), GSM with BlackBerry devices operating on GSM networks, and QCP with BlackBerry devices operating on CDMA networks. The default audio recording format is AMR.
  1. Import the required classes.
    import java.lang.Thread;
    import javax.microedition.media.Manager;
    import java.io.ByteArrayOutputStream;
    import javax.microedition.media.Player;
    import javax.microedition.media.control.RecordControl;
    
  2. In a class that extends Thread, create a variable of type Player, a variable of type RecordControl for recording media from a Player, a variable of type ByteArrayOutputStream for the audio stream, and a byte array variable to store the OutputStream data. Note that you are not required to record audio in a separate thread because recording operations are threaded by design.
    final class AudioRecorderThread extends Thread 
    {
        private Player _player;
        private RecordControl _rcontrol;
        private ByteArrayOutputStream _output;
        private byte _data[];
  3. Create a class constructor.
    AudioRecorderThread(){}
  4. In a try block, in your implementation of the run() method, invoke Manager.createPlayer(String locator) using as a parameter a value that specifies the encoding to use to record audio. You can use the following supported locator strings.
    • AMR: capture://audio or capture://audio?encoding=amr or capture://audio?encoding=audio/amr
    • PCM: capture://audio?encoding=pcm or capture://audio?encoding=audio/basic
    • GSM: capture://audio?encoding=gsm or capture://audio?encoding=audio/x-gsm
    • QCP: capture://audio?encoding=audio/qcelp
  5. Create a Player object by invoking createPlayer() to capture audio.
    public void run() {
        try {
            _player = Manager.createPlayer("capture://audio");
  6. Invoke Player.realize().
    _player.realize();
  7. Invoke Player.getControl() to obtain the controls for recording media from a Player.
    _rcontrol = (RecordControl)_player.getControl("RecordControl");
  8. Create a ByteArrayOutputStream to record the audio stream. Note that you can also record directly to a file specified by a URL.
    _output = new ByteArrayOutputStream();
  9. Invoke RecordControl.setRecordStream() to set the output stream to which the BlackBerry device application records data.
    _rcontrol.setRecordStream(_output);
  10. Invoke RecordStore.startRecord() to start recording the audio and start playing the media from the Player.
    _rcontrol.startRecord(); 
    _player.start();
  11. In a catch block, specify actions to perform if an exception occurs.
    } catch (final Exception e){
        //Perform actions 
    }
  12. Create a try block in your implementation of the stop method, and then invoke RecordControl.commit() to stop recording audio.
    public void stop() {
         try {
            _rcontrol.commit()
  13. Invoke ByteArrayOutputStream.toByteArray() to write the audio data from the OutputStream to a byte array.
    _data = _output.toByteArray();
  14. Invoke ByteArrayOutputStream.close() and Player.close() to close the OutputStream and Player.
    _output.close(); 
    _player.close();
  15. In a catch block, specify actions to perform if an exception occurs.
    } catch (Exception e) {
        //Perform actions
    }

Was this information helpful? Send us your comments.