views:

3112

answers:

3

This is probably lazy of me, but as I've mentioned before, the RIM website is a mangled mess.

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?

Basically, I'd like to read incoming audio, sample by sample if need be, then process it to produce a desired result (think visualizer).

Thanks in advance.

+1  A: 

While not audio specific, this question does have some good "getting started" references.

Writing Blackberry Applications

Paul Alexander
While useful, I really am looking for specific examples on the Audio API. It's not too hard to find BlackBerry documentation for the GUI stuff, but this low level stuff is a bit harder to come by. Thanks for your answer.
Sam
Yeah...I'm playing around with BB development myself. It's frustrating how little support and docs there are for anything but "standard" apps.
Paul Alexander
Yes, it's frustrating! Seems that there are few people who truly love developing for BB, and BB themselves would really like to charge you $75/hour to talk to a representative who simply searches their own support forums...
Sam
+3  A: 

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:

  • create Player with correct audio encoding
  • get RecordControl
  • start recording
  • stop recording

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:

  • synch access to output stream for Recorder and Sampler - threading issue
  • read the correct amount of audio data - go deep into audio format decode to find out markup rules

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

Max Gontar
Thanks much. This is very, VERY helpful.
Sam
Youre welcome !
Max Gontar
+1  A: 

I spent ages trying to figure this out too. Once you've installed the BlackBerry Component Packs (available from their website), you can find the sample code inside the component pack.

In my case, once I had installed the Component Packs into Eclipse, I found the extracted sample code in this location:

C:\Program Files\Eclipse\eclipse3.4\plugins\net.rim.eide.componentpack4.5.0_4.5.0.16\components\samples

Unfortunately when I imported all that sample code I had a bunch of compile errors. To workaround that I just deleted the 20% of packages with compile errors.

My next problem was that launching the Simulator always launched the first sample code package (in my case activetextfieldsdemo), I couldn't get it to run just the package I am interested in. Workaround for that was to delete all the packages listed alphabetically before the one I wanted.

Other gotchas:
-Right click on the project in Eclipse and select Activate for BlackBerry
-Choose BlackBerry -> Build Configurations... -> Edit... and select your new project so it builds.
-Make sure you put your BlackBerry source code under a "src" folder in the Eclipse project, otherwise you might hit build issues.

Dan J
Wow, these woes are pretty demoralizing... Thanks for the input! ++you
Sam