Most mobile devices contain audio recording equipment, either as an independent microphone or as part of a camcorder setup. Provided that our target devices contain this hardware, we can use the android.media.MediaRecorder class to record audio samples from within our applications. In this recipe, we will create a basic audio recorder that stores captured sound on the SD card.
Audio recording support does not currently work with an emulator, so you will need to test this recipe with a handset.
Start up a new Android project and open up the
res/layout/main.xml
file.Using either the Graphical Layout or the main.xml tab, add two buttons and give them the IDs
start_button
andstop_button
.Declare the following class-wide fields in the Java activity class:
private MediaRecorder recorder = new MediaRecorder(); private Button playButton; private Button stopButton;
Next, associate these with the views defined in
main.xml
:playButton = (Button) findViewById(R...