Sound recording in j2me

In this article I decided to note for myself how to record and play sound in J2ME

First of all you need to clreate an object of the Player which will get a stream of a data from microphone

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


The string
capture://audio?encoding=amr
is the simpliest

You can try to use another parameters with capture://audio to get better quality

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

For example:
capture://audio?rate=8000&bits=16
capture://audio?encoding=pcm&signed=unsigned

To find what parameters support your mobile device try to perform this code:

String[] types= Manager.getSupportedContentTypes("capture");

for( int i= 0; i <>
someStringItem.setText(errorItem.getText() + " "+types[i]);


To start recording you need to have an object of a RecordControl type.

Then you will be able to write your data into a ByteArayOutputStream stream.

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


After that you need to set the time of recording. For stop you can use an event of any type or simply wait till end of the time.
For example like that:

Thread.currentThread().sleep(5000);

rc.commit();


Then you can save your sound or to listen, firstly convert it to byte array.

byte[] recordedSoundArray = output.toByteArray();

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


NOTE: All the said above is true for devices which allow MMAPI to work with audio/video streaming

There I decided to share a class example which successfully records sound on most nowadays devices. Here is a code:
package maxsmile;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.lcdui.StringItem;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.control.RecordControl;

/**
 *
 * @author maxim.vasilkov@gmail.com
 */
public class FewSecondsRecord extends Thread {
    public StringItem stringItem;
    //Constructor
    public FewSecondsRecord(StringItem si) {
        // We will show our status here
        stringItem = si;
    }
   
    public void run() {
        try {
            Player player;
            //We could use ?encoding=audio/wav
            //But let the system use defaults
            player = Manager.createPlayer("capture://audio");
            player.realize();
            RecordControl rc = (RecordControl)player.getControl("RecordControl");
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            rc.setRecordStream(output);
           
            rc.startRecord();
            player.start();
            // Show user what happening now
            // This string item variable we got
            // as a parameter in the constructor
            stringItem.setText("Start record...");
           
            //Time of record
            //change this with your logic
            Thread.sleep(5000);

            rc.stopRecord();
            rc.commit();
            player.stop();
           
            player.close();
            // Show user what happening now
            stringItem.setText("Processing...");
            byte[] recordedSoundArray = output.toByteArray();
           
            ByteArrayInputStream recordedInputStream =
                    new ByteArrayInputStream(recordedSoundArray);
            Player player2 = Manager.createPlayer(recordedInputStream,
                                    "audio/x-wav");
            player2.realize();
            player2.prefetch();
            player2.start();
            // Show user what happening now
            stringItem.setText("Play sound!");
            //Time of record
            //change this with your logic
            Thread.sleep(5000);
            // Show user what happening now
            stringItem.setText("Please press exit");
        } catch (Exception ex) {
            ex.printStackTrace();
            stringItem.setText("Error :( - "+ex.getMessage());
        }
    }

}

Comments

Popular posts from this blog

FarmVille Clicker

Brief history of my life (Part I - from birth 'till university)