Android can use MediaRecorder and AudioRecord to implement recording. MediaRecorder directly stores the microphone data to a file and can directly encode the data (such as AMR and MP3 ), audioRecord is the audio stream that reads the microphone. This article uses AudioRecord to read audio streams and AudioTrack to play audio streams. It implements a simple hearing aid program by means of "reading and playing" and increasing the volume.
PS: because the current Android simulator does not support AudioRecord, this program needs to be compiled and run on the real machine.
First paste the program running this article:
PS: The program volume adjustment only adjusts the volume inside the program. To adjust the maximum volume, you need to manually set the system volume.
To use AudioRecord, you must apply for a license. In AndroidManifest. xml, add the following statement:
View plaincopy to clipboardprint?
<Uses-permission android: name = "android. permission. RECORD_AUDIO"> </uses-permission>
<Uses-permission android: name = "android. permission. RECORD_AUDIO"> </uses-permission>
The source code of main. xml is as follows:
View plaincopy to clipboardprint?
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical" android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<Button android: layout_height = "wrap_content" android: id = "@ + id/btnRecord"
Android: layout_width = "fill_parent" android: text = "Start recording and placing"> </Button>
<Button android: layout_height = "wrap_content"
Android: layout_width = "fill_parent" android: text = "Stop"Android: id =" @ + id/btnStop "> </Button>
<Button android: layout_height = "wrap_content" android: id = "@ + id/btnExit"
Android: layout_width = "fill_parent" android: text = "quit"> </Button>
<TextView android: id = "@ + id/TextView01" android: layout_height = "wrap_content"
Android: text = "program volume adjustment" android: layout_width = "fill_parent"> </TextView>
<SeekBar android: layout_height = "wrap_content" android: id = "@ + id/skbVolume"
Android: layout_width = "fill_parent"> </SeekBar>
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical" android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<Button android: layout_height = "wrap_content" android: id = "@ + id/btnRecord"
Android: layout_width = "fill_parent" android: text = "Start recording and placing"> </Button>
<Button android: layout_height = "wrap_content"
Android: layout_width = "fill_parent" android: text = "Stop"Android: id =" @ + id/btnStop "> </Button>
<Button android: layout_height = "wrap_content" android: id = "@ + id/btnExit"
Android: layout_width = "fill_parent" android: text = "quit"> </Button>
<TextView android: id = "@ + id/TextView01" android: layout_height = "wrap_content"
Android: text = "program volume adjustment" android: layout_width = "fill_parent"> </TextView>
<SeekBar android: layout_height = "wrap_content" android: id = "@ + id/skbVolume"
Android: layout_width = "fill_parent"> </SeekBar>
</LinearLayout>
The source code of testRecord. java is as follows:
View plaincopy to clipboardprint?
Package com. testRecord;
Import android. app. Activity;
Import android. media. AudioFormat;
Import android. media. AudioManager;
Import android. media. AudioRecord;
Import android. media. AudioTrack;
Import android. media. MediaRecorder;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. Button;
Import android. widget. SeekBar;
Import android. widget. Toast;
Public class testRecord extends Activity {
/** Called when the activity is first created .*/
Button btnRecord, btnStop, btnExit;
SeekBar skbVolume; // adjust the volume
Boolean isRecording = false; // specifies whether the mark is recorded.
Static final int frequency = 44100;
Static final int channelConfiguration = AudioFormat. CHANNEL_CONFIGURATION_MONO;
Static final int audioEncoding = AudioFormat. ENCODING_PCM_16BIT;
Int recBufSize, playBufSize;
AudioRecord audioRecord;
AudioTrack audioTrack;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
SetTitle ("Hearing Aid ");
RecBufSize = AudioRecord. getMinBufferSize (frequency,
ChannelConfiguration, audioEncoding );
PlayBufSize = AudioTrack. getMinBufferSize (frequency,
ChannelConfiguration, audioEncoding );
//-----------------------------------------
AudioRecord = new AudioRecord (MediaRecorder. AudioSource. MIC, frequency,
ChannelConfiguration, audioEncoding, recBufSize );
AudioTrack = new AudioTrack (AudioManager. STREAM_MUSIC, frequency,
ChannelConfiguration, audioEncoding,
PlayBufSize, AudioTrack. MODE_STREAM );
//------------------------------------------
BtnRecord = (Button) this. findViewById (R. id. btnRecord );
BtnRecord. setOnClickListener (new ClickEvent ());
BtnStop = (Button) this. findViewById (R. id. btnStop );
BtnStop. setOnClickListener (new ClickEvent ());
BtnExit = (Button) this. findViewById (R. id. btnExit );
BtnExit. setOnClickListener (new ClickEvent ());