Record audio in a BlackBerry device application

BlackBerry® devices that operate on CDMA networks can record audio in the following formats: AMR 8 kHz mono, 16-bit PCM, GSM 6.10, and QCELP. The default audio recording format is AMR.
  1. Import the required classes.
    import net.rim.device.api.ui.*;
    import net.rim.device.api.ui.container.*;
    import net.rim.device.api.ui.component.*;
    import java.io.*;
    import java.lang.*;
    import javax.microedition.media.*;
    import javax.microedition.media.control.*;
  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 AudioRecordingDemoScreen class, which is described in step 3, represents the custom screen.
    public class AudioRecordingDemo extends UiApplication
    {
         public static void main(String[] args)
         {
              AudioRecordingDemo app = new AudioRecordingDemo();
              app.enterEventDispatcher();
         }
        
         public AudioRecordingDemo()
         {
              pushScreen(new AudioRecordingDemoScreen());
         }
    }
  3. Create the framework for the custom screen by extending the MainScreen class. Declare an instance of the AudioRecorderThread class, which is described in step 5.
    private class AudioRecordingDemoScreen extends MainScreen
    {	
        private AudioRecorderThread _recorderThread;
    
        public AudioRecordingDemoScreen()
        {
        }
    }
  4. In the AudioRecordingDemoScreen constructor, invoke setTitle() to specify the title for the screen. Invoke addMenuItem() twice to add the menu items to start and stop the recording. which is described in step 5.
    public AudioRecordingDemoScreen()
    {
       setTitle("Audio recording demo");
    
       addMenuItem(new StartRecording());
       addMenuItem(new StopRecording());
                
    }
  5. In the AudioRecordingDemoScreen class, define the menu items to start the recording and to stop recording by invoking the start() and stop() methods of the AudioRecorderThread class respectively, which is described in step 6. in the following code sample, Player starts in its own thread to increase the responsiveness of the menu items.
    private class StartRecording extends MenuItem 
    {
        public StartRecording() 
        {
            super("Start recording", 0, 100);
        }
    
        public void run() 
        {
            try 
            {
                AudioRecorderThread newRecorderThread = new AudioRecorderThread();
                newRecorderThread.start();
                _recorderThread = newRecorderThread;
            } 
            catch (Exception e) 
            {
                Dialog.alert(e.toString());
            }
        }
    }
            
    private class StopRecording extends MenuItem 
    {
        public StopRecording() 
        {
            super("Stop recording", 0, 100);
        }
    
        public void run() 
        {
             try 
             {
                 if (_recorderThread != null) 
                 { 
                     _recorderThread.stop();
                 }
             } 
             catch (Exception e) 
             {
                 Dialog.alert(e.toString());
             }
         }
    }
  6. Inside the AudioRecordingDemo screen class, define an inner class that extends Thread and implements PlayerListener. In the inner class, create a variable of type Player and a variable of type RecordControl for recording media.
    Note: You are not required to record audio in a separate thread because recording operations are threaded by design.
    private class AudioRecorderThread extends Thread implements javax.microedition.media.PlayerListener
    {
        private Player _player;
        private RecordControl _recordControl;
    
        AudioRecorderThread()
        {
        
        }
    
    }
  7. In the AudioRecorderThread class, implement the Thread interface's run() method. In run() create a try/catch block and invoke Manager.createPlayer(String locator) to create a Player object to record audio, using as a parameter a value that specifies the encoding to use.
    public void run() 
    {
        try 
        {
            _player = javax.microedition.media.Manager.createPlayer("capture://audio?encoding=audio/amr");
        
        }
        catch( IOException e ) 
        {
            Dialog.alert(e.toString());
        }
        catch( MediaException e ) 
        {
            Dialog.alert(e.toString());
        }
    }
  8. Invoke Player.addPlayerListener() specifying this as a parameter because AudioRecorderThread implements PlayerListener.
    public void run() 
    {
        try 
        {
            _player = javax.microedition.media.Manager.createPlayer("capture://audio?encoding=audio/amr");
    
            _player.addPlayerListener(this);
        
        }
        catch( IOException e ) 
        {
            Dialog.alert(e.toString());
        }
        catch( MediaException e ) 
        {
            Dialog.alert(e.toString());
        }
    }
  9. To record, invoke Player.realize() method to access the RecordControl object. Then, invoke Player.getControl(), passing in the string, RecordControl, to retrieve the RecordControl object.
    public void run() 
    {
        try 
        {
            _player = javax.microedition.media.Manager.createPlayer("capture://audio?encoding=audio/amr");
    
            _player.addPlayerListener(this);
    
            _player.realize();
            _recordControl = (RecordControl) _player.getControl( "RecordControl" );
        
        }
        catch( IOException e ) 
        {
            Dialog.alert(e.toString());
        }
        catch( MediaException e ) 
        {
            Dialog.alert(e.toString());
        }
    }
  10. Invoke RecordControl.setRecordLocation() to set the location on the device to save the audio recording.
    public void run() 
    {
        try 
        {
            _player = javax.microedition.media.Manager.createPlayer("capture://audio?encoding=audio/amr");
    
            _player.addPlayerListener(this);
    
            _player.realize();
            _recordControl = (RecordControl) _player.getControl( "RecordControl" );
    
            _recordControl.setRecordLocation("file:///store/home/user/AudioRecordingTest.amr" );    
        }
        catch( IOException e ) 
        {
            Dialog.alert(e.toString());
        }
        catch( MediaException e ) 
        {
            Dialog.alert(e.toString());
        }
    }
  11. Invoke RecordControl.startRecord() to start recording the audio and start playing the media. Invoke Player.start() to start Player.
    public void run() 
    {
        try 
        {
            _player = javax.microedition.media.Manager.createPlayer("capture://audio?encoding=audio/amr");
    
            _player.addPlayerListener(this);
    
            _player.realize();
            _recordControl = (RecordControl) _player.getControl( "RecordControl" );
    
            _recordControl.setRecordLocation("file:///store/home/user/AudioRecordingTest.amr" );
    
            _recordControl.startRecord(); 
            _player.start();
        
        }
        catch( IOException e ) 
        {
            Dialog.alert(e.toString());
        }
        catch( MediaException e ) 
        {
            Dialog.alert(e.toString());
        }
    }
  12. In AudioRecorder, implement the Thread interface's stop() method. Check that Player is not null and invoke Player.close() to release the Player object's resources. Set Player to null.
    public void stop() 
    {
        if (_player != null) 
        {
            _player.close();
            _player = null;
        }
    }
  13. Check that RecordControl is not null and invoke RecordControl.stopRecord() to stop recording. In a try/catch block, invoke RecordControl.commit() to save the recording to a specified file. Set RecordControl to null.
    public void stop() 
    {
        if (_player != null) 
        {
            _player.close();
            _player = null;
        }
    
        if (_recordControl != null) 
        {
            _recordControl.stopRecord();
                        
            try 
            {
                _recordControl.commit();
            } 
            catch (Exception e) 
            {
                Dialog.alert(e.toString());
            }
            _recordControl = null;
        }
    }
  14. In AudioRecorderThread, implement the PlayerListener interface's playerUpdate() method which is invoked whenever the Player object generates an event. In the following code sample, the application outputs information about the event.
    public void playerUpdate(Player player, String event, Object eventData) 
    {
        Dialog.alert("Player " + player.hashCode() + " got event " + event + ": " + eventData);
    }
Previous topic: Recording audio

Was this information helpful? Send us your comments.