Header Sep

Java Tips, Tricks & Code

My Rating Score
Login to rate page

January 2008

JSR 135, recording PCM (.wav) sound in JP-8

Sound can be recorded and coded in different formats using JSR 135, the Mobile Media API. In Sony Ericsson's Java Platform 8 (JP-8) phones, sound can be encoded to either AMR (.amr) or PCM (.wav) format. Sound can also be recorded to a stream or directly to a file. This example shows how to record sound in either of the two formats, using a stream or writing directly to a file.

Download example MIDlet>>

JSR 135 has the support of configuring recording options when creating a player using standard URI, augmented BNF syntax. Parameters can be passed that specifies which audio format sound should be encoded with.

To record sound encoded in AMR (.amr) sound format do the following when creating a player:

Player p = Manager.createPlayer("capture://audio?encoding=amr");
String fileName = "MyRecording.amr";

Or simply:

Player p = Manager.createPlayer("capture://audio");
String fileName = "MyRecording.amr";

To record sound encoded in PCM (.wav) sound format do the following when creating a player:

Player p = Manager.createPlayer("capture://audio?encoding=pcm");
String fileName = "MyRecording.wav";

To record sound using a stream the following has to be done after the record control has been retrieved from the player.

ByteArrayOutputStream output = new ByteArrayOutputStream();
recordControl.setRecordStream(output);
recordControl.startRecord();
//…record sound…
recordControl.stopRecord();
recordControl.commit();

To record straight to a file this has to be done instead:

recordControl.setRecordLocation(fileURL);
recordControl.startRecord();
//…record sound…
recordControl.stopRecord();
recordControl.commit();

Attached is an example of a sound recorder that can record to AMR or PCM using either a stream or directly writing to a file.

More information:

My Rating Score
Login to rate page