audioblackberryjava-memmapi

Blackberry Audio Recording Sample Code


Does anyone know of a good repository to get sample code for the BlackBerry? Specifically, samples that will help me learn the mechanics of recording audio, possibly even sampling it and doing some on the fly signal processing on it?

I'd like to read incoming audio, sample by sample if need be, then process it to produce a desired result, in this case a visualizer.


Solution

  • RIM API contains JSR 135 Java Mobile Media API for handling audio & video content.
    You correct about mess on BB Knowledge Base. The only way is browse it, hoping they'll not going to change site map again.
    It's Developers->Resources->Knowledge Base->Java API's&Samples->Audio&Video

    Audio Recording

    Basically it's simple to record audio:

    Links:
    RIM 4.6.0 API ref: Package javax.microedition.media
    How To - Record Audio on a BlackBerry smartphone
    How To - Play audio in an application
    How To - Support streaming audio to the media application
    How To - Specify Audio Path Routing
    How To - Obtain the media playback time from a media application
    What Is - Supported audio formats
    What Is - Media application error codes

    Audio Record Sample

    Thread with Player, RecordControl and resources is declared:

    final class VoiceNotesRecorderThread extends Thread{
       private Player _player;
       private RecordControl _rcontrol;
       private ByteArrayOutputStream _output;
       private byte _data[];
    
       VoiceNotesRecorderThread() {}
    
       private int getSize(){
           return (_output != null ? _output.size() : 0);
       }
    
       private byte[] getVoiceNote(){
          return _data;
       }
    }
    

    On Thread.run() audio recording is started:

       public void run() {
          try {
              // Create a Player that captures live audio.
              _player = Manager.createPlayer("capture://audio");
              _player.realize();    
              // Get the RecordControl, set the record stream,
              _rcontrol = (RecordControl)_player.getControl("RecordControl");    
              //Create a ByteArrayOutputStream to capture the audio stream.
              _output = new ByteArrayOutputStream();
              _rcontrol.setRecordStream(_output);
              _rcontrol.startRecord();
              _player.start();    
          } catch (final Exception e) {
             UiApplication.getUiApplication().invokeAndWait(new Runnable() {
                public void run() {
                   Dialog.inform(e.toString());
                }
             });
          }
       }
    

    And on thread.stop() recording is stopped:

       public void stop() {
          try {
               //Stop recording, capture data from the OutputStream,
               //close the OutputStream and player.
               _rcontrol.commit();
               _data = _output.toByteArray();
               _output.close();
               _player.close();    
          } catch (Exception e) {
             synchronized (UiApplication.getEventLock()) {
                Dialog.inform(e.toString());
             }
          }
       }
    

    Processing and sampling audio stream

    In the end of recording you will have output stream filled with data in specific audio format. So to process or sample it you will have to decode this audio stream.

    Talking about on the fly processing, that will be more complex. You will have to read output stream during recording without record commiting. So there will be several problems to solve:

    Also may be useful:
    java.net: Experiments in Streaming Content in Java ME by Vikram Goyal