:: Research Group Embedded Interaction :: Media Informatics :: Ludwig-Maximilians-University Munich


Recording and playing sounds with J2ME

This tutorials shows, how to use the MMAPI of J2ME to record and play recorded sounds on mobile phones.

Used Software

  • J2ME (MIDP 2.0, CLDC 1.0)
  • Mobile Media API (MMAPI)

Used Hardware

  • Nokia 6600 (Software version 4.09.1)
  • Siemens S65

Source Code

The source code for this tutorial can be found at recorddemo.zip.

Needed steps for recording sounds

The Mobile Media API provides support for capturing audio or video from the onboard hardware (microphone or camera) with the “capture” locator syntax. To record audio, you have to get acces to the mobile phones microphone.

Create a Player

First you have to create a player object. The player gets input from the onboard microphone.

	Player player;
	...
	player=Manager.createPlayer(“capture://audio?encoding=amr”);
	player.realize();

In this tutorial we are using capture://audio?encoding=amr because this is the most simple way to avoid the Mickey Mouse Effect on the Nokia 6600 mobile phones. On other phones you can try to use capture://audio or other audio encodings.

Prepare and start recording

To start recording you need to create a RecordControl Object.

       RecordControl rc = (RecordControl)player.getControl("RecordControl");
       ByteArrayOutputStream output = new ByteArrayOutputStream();
       rc.setRecordStream(output);
       rc.startRecord();
       player.start();
    

It is important to create a ByteArayOutputStream on which the RecordControl will write the recorded data.

Recording audio for a specific time

After starting the record you need to specify the time for which your application will record audio data. To stop recording you can use any event (like key pressed) or you can just let it record for a declared time.

       Thread.currentThread().sleep(5000);
       rc.commit();
    

This example will record audio for 5 seconds. rc.commit() stops the recording.

Saving the recorded data

The recorded data can be saved to a record store. If the data is small enough and you just want to listen to it, it can easily be saved in a variable as well.

       byte[] recordedSoundArray = output.toByteArray();
    

Calling this, will save the recorded data to a byte array.

Playing the recorded data

The following code will tell the mobile phone to play the recorded audio data.

       ByteArrayInputStream recordedInputStream = new ByteArrayInputStream(recordedSoundArray);
       Player p2 = Manager.createPlayer(recordedInputStream,"audio/x-wav");
       p2.prefetch();
       p2.start();
    

Other audio encodings


     audio_encodings = audio_enc_param *( "&" audio_param )
     audio_enc_param = "encoding=" audio_enc
     audio_enc       = "pcm" / "ulaw" / "gsm" / content_type
     audio_param     = "rate=" rate /
                       "bits=" bits /
                       "channels=" channels /
                       "endian=" endian /
                       "signed=" signed /
                       "type=" audio_type
     rate            = "96000" / "48000" / "44100" /
                       "22050" / "16000" / "11025" /
                       "8000" / other_rate
     other_rate      = pos_integer
     bits            = "8" / "16" / "24" / other_bits
     other_bits      = pos_integer
     channels        = pos_integer
     endian          = "little" / "big"
     signed          = "signed" / "unsigned"
     audio_type      = bitrate_variable / other_type
     other_type      = alphanumeric
     pos_integer     = 1*DIGIT
 

examples:

  • capture://audio?rate=8000&bits=16
  • capture://audio?encoding=pcm&signed=unsigned
  • ...

Finding out the supported capture types for your mobile phone

If you want to find out the supported capture formats for your cell phone, this could help you.
It works pretty good on many cell phones but not on all.

       String[] types= Manager.getSupportedContentTypes("capture");
       for( int i= 0; i < types.length; i++ )
       someStringItem.setText(errorItem.getText() + " "+types[i]);

In this example, someStringItem is a StringItem object.

If you have any comments or questions, please feel free to ask us!