Two audio recording methods for Android

Source: Internet
Author: User

Two audio recording methods for Android

In mobile APP development, every time an APP is designed for multimedia development, many programmers will suffer, and the project development progress will slow down

And the APP testing will increase. Multimedia Development in Android includes audio playback, audio recording, video playback, and video recording.

Although the Android SDK provides some basic development APIs, for example, audio recording provides two methods: AudioRecord recording audio and MediaRecorder recording

Audio. Compared with MediaRecorder, The AudioRecord class is closer to the underlying layer and has fewer encapsulated methods. However, an AudioRecord audio recording is implemented.

Programming is also very simple.

I. AudioRecord:

Package com. hb56.MyAndroidUtil;
Import java. io. BufferedInputStream;
Import java. io. BufferedOutputStream;
Import java. io. DataInputStream;
Import java. io. DataOutputStream;
Import java. io. File;
Import java. io. FileInputStream;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import android. app. Activity;
Import android. content. ContentValues;
Import android. content. Intent;
Import android. hardware. Camera. AutoFocusCallback;
Import android. media. AudioFormat;
Import android. media. AudioManager;
Import android. media. AudioRecord;
Import android. media. AudioTrack;
Import android. media. MediaPlayer;
Import android. media. MediaRecorder;
Import android.net. Uri;
Import android. OS. AsyncTask;
Import android. OS. Bundle;
Import android. OS. Environment;
Import android. provider. MediaStore;
Import android. util. Log;
Import android. view. View;
Import android. widget. Button;
Import android. widget. TextView;


/**
* In this instance, we use the AudioRecord class to complete our audio recording program.
* For the AudioRecord class, we can use three different read methods to complete recording,
* Each method is used in actual scenarios.
* 1. to instantiate an AudioRecord class, we need to input several parameters.
* 1. AudioSource: MediaRecorder. AudioSource. MIC
* 2. SampleRateInHz: recording frequency, which can be 8000hz or 11025hz. Different hardware devices have different values.
* 3. ChannelConfig: Specifies the recording channel, which can be AudioFormat. CHANNEL_CONFIGURATION_MONO and AudioFormat. CHANNEL_CONFIGURATION_STEREO.
* 4. AudioFormat: the recording encoding format, which can be AudioFormat. ENCODING_16BIT and 8 bit. The simulation of 16 bit is better than that of 8 bit, but it consumes more power and storage space.
* 5. BufferSize: Specifies the buffer size for recording. You can use getMinBufferSize to obtain the buffer size.
* In this way, we can instantiate an AudioRecord object.
* 2. Create a file to save the recorded content.
* Same article
* 3. Open an output stream and point to the created file.
* DataOutputStream dos = new DataOutputStream (new BufferedOutputStream (new FileOutputStream (file )))
* 4. Now we can start recording. We need to create a byte array to store the audio data returned from AudioRecorder,
* Note: The defined array must be smaller than the BufferSize specified when AudioRecord is defined.
* Short [] buffer = new short [BufferSize/4];
* StartRecording ();
* Then, in a loop, call the read method of AudioRecord to read data.
* In addition, MediaPlayer cannot be used to play audio recorded using AudioRecord. To achieve playback, we need
* Implemented using the AudioTrack class
* The AudioTrack class allows us to play original audio data.
*
*
* 1. to instantiate an AudioTrack, you also need to input several parameters.
* 1. StreamType: There are several constants in AudioManager, one of which is STREAM_MUSIC;
* 2. SampleRateInHz: it is best to use the same value as AudioRecord.
* 3. ChannelConfig: Same as above
* 4. AudioFormat: Same as above
* 5. BufferSize: obtained through the static method getMinBufferSize of AudioTrack
* 6. Mode: It can be AudioTrack. MODE_STREAM and MODE_STATIC. For the two differences, refer to the documentation.
* 2. Open an input stream, point to the file saved in the recorded content, and start playing.
*
* Two AsyncTask methods are used for audio recording and playback.
*/
/**
* Use the AudioRecord class to implement your own audio recording program
* Com. hb56.MyAndroidUtil. AudioRecord
*
* @ Author Admin-zhangyx
*
* Create at 2:03:13
*/
Public class AudioRecordActivity extends Activity {
Private TextView stateView;
Private Button btnStart, btnStop, btnPlay, btnFinish;
Private RecordTask recorder;
Private PlayTask player;
Private File audioFile;
Private boolean isRecording = true, isPlaying = false; // mark
Private int frequence = 8000; // recording frequency, in hz. The value here is noted. If the write is poor, errors may occur when the AudioRecord object is instantiated. It won't work if I start writing 11025. This depends on the hardware.
Private int channelConfig = AudioFormat. CHANNEL_CONFIGURATION_MONO;
Private int audioEncoding = AudioFormat. ENCODING_PCM_16BIT;


Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. my_audio_record );
StateView = (TextView) this. findViewById (R. id. view_state );
StateView. setText ("Getting started ");
BtnStart = (Button) this. findViewById (R. id. btn_start );
BtnStop = (Button) this. findViewById (R. id. btn_stop );
BtnPlay = (Button) this. findViewById (R. id. btn_play );
BtnFinish = (Button) this. findViewById (R. id. btn_finish );
BtnFinish. setText ("Stop playing ");
BtnStop. setEnabled (false );
BtnPlay. setEnabled (false );
BtnFinish. setEnabled (false );


// Here we create a file to save the recording content
File fpath = new File (Environment. getExternalStorageDirectory ()
. GetAbsolutePath () + "/data/files /");
Fpath. mkdirs (); // create a folder
Try {
// Create a temporary file. Note that the format is. pcm.
AudioFile = File. createTempFile ("recording", ". pcm", fpath );
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}


Public void onClick (View v ){
Int id = v. getId ();
Switch (id ){
Case R. id. btn_start:
// Start recording


// Start the recording task here
Recorder = new RecordTask ();
Recorder.exe cute ();


Break;
Case R. id. btn_stop:
// Stop recording
This. isRecording = false;
// Update the status
// Set when recording is complete and completed in onPostExecute of RecordTask
Break;
Case R. id. btn_play:


Player = new PlayTask ();
Player.exe cute ();
Break;
Case R. id. btn_finish:
// Complete playback
This. isPlaying = false;
Break;


}
}


Class RecordTask extends AsyncTask {
@ Override
Protected Void doInBackground (Void... arg0 ){
IsRecording = true;
Try {
// Activate the output stream to the specified file
DataOutputStream dos = new DataOutputStream (
New BufferedOutputStream (
New FileOutputStream (audioFile )));
// Obtain the appropriate buffer size based on several defined configurations
Int bufferSize = AudioRecord. getMinBufferSize (frequence,
ChannelConfig, audioEncoding );
// Instantiate AudioRecord
AudioRecord record = new AudioRecord (
MediaRecorder. AudioSource. MIC, frequence,
ChannelConfig, audioEncoding, bufferSize );
// Define the buffer
Short [] buffer = new short [bufferSize];


// Start recording
Record. startRecording ();


Int r = 0; // store the recording progress
// Define a loop and determine whether to continue recording Based on the isRecording Value
While (isRecording ){
// Read bytes from bufferSize and return the number of short reads
// Buffer overflow always appears here. I don't know why. I tried several values and it didn't work. TODO: to be solved.
Int bufferReadResult = record
. Read (buffer, 0, buffer. length );
// Cyclically write the audio data in the buffer into OutputStream
For (int I = 0; I <bufferReadResult; I ++ ){
Dos. writeShort (buffer [I]);
}
PublishProgress (new Integer (r); // report the current progress to the UI thread
R ++; // progress value of auto Increment
}
// Recording ends
Record. stop ();
Log. v ("The DOS available:", ":" + audioFile. length ());
Dos. close ();
} Catch (Exception e ){
// TODO: handle exception
}
Return null;
}


// This method is triggered when publishProgress is called in the preceding method. This method is executed in the UI thread.
Protected void onProgressUpdate (Integer... progress ){
StateView. setText (progress [0]. toString ());
}


Protected void onPostExecute (Void result ){
BtnStop. setEnabled (false );
BtnStart. setEnabled (true );
BtnPlay. setEnabled (true );
BtnFinish. setEnabled (false );
}


Protected void onPreExecute (){
// StateView. setText ("Recording ");
BtnStart. setEnabled (false );
BtnPlay. setEnabled (false );
BtnFinish. setEnabled (false );
BtnStop. setEnabled (true );
}


}


Class PlayTask extends AsyncTask {
@ Override
Protected Void doInBackground (Void... arg0 ){
IsPlaying = true;
Int bufferSize = AudioTrack. getMinBufferSize (frequence,
ChannelConfig, audioEncoding );
Short [] buffer = new short [bufferSize/4];
Try {
// Define the input stream and write the audio into the AudioTrack class for playback.
DataInputStream dis = new DataInputStream (
New BufferedInputStream (new FileInputStream (audioFile )));
// Instance AudioTrack
AudioTrack = new AudioTrack (AudioManager. STREAM_MUSIC,
Frequence, channelConfig, audioEncoding, bufferSize,
AudioTrack. MODE_STREAM );
// Start playing
Track. play ();
// Because AudioTrack plays a stream, We need to read the stream while playing it.
While (isPlaying & dis. available ()> 0 ){
Int I = 0;
While (dis. available ()> 0 & I <buffer. length ){
Buffer [I] = dis. readShort ();
I ++;
}
// Write the data to AudioTrack
Track. write (buffer, 0, buffer. length );


}


// The playback ends.
Track. stop ();
Dis. close ();
} Catch (Exception e ){
// TODO: handle exception
}
Return null;
}


Protected void onPostExecute (Void result ){
BtnPlay. setEnabled (true );
BtnFinish. setEnabled (false );
BtnStart. setEnabled (true );
BtnStop. setEnabled (false );
}


Protected void onPreExecute (){


// StateView. setText ("playing ");
BtnStart. setEnabled (false );
BtnStop. setEnabled (false );
BtnPlay. setEnabled (false );
BtnFinish. setEnabled (true );
}


}
}

 

Ii. MediaRecorder:

Package com. hb56.MyAndroidUtil;
Import java. io. File;
Import java. io. IOException;
Import android. app. Activity;
Import android. content. ContentValues;
Import android. content. Intent;
Import android. media. MediaPlayer;
Import android. media. MediaRecorder;
Import android.net. Uri;
Import android. OS. Bundle;
Import android. OS. Environment;
Import android. provider. MediaStore;
Import android. view. View;
Import android. widget. Button;
Import android. widget. TextView;
/**
* This uses the MediaRecorder class to implement your own audio recording program.
*
* To enable recording of audio, we need the RECORD_AUDIO permission.
* To write data to SDCard, we need the WRITE_EXTERNAL_STORAGE permission.
*/
/**
* Use the MediaRecorder class to implement your own audio recording program
* Com. hb56.MyAndroidUtil. MediaRecorderActivity
* @ Author Admin-zhangyx
*
* Create at 2:13:38
*/
Public class MediaRecorderActivity {
Private TextView stateView;
Private Button btnStart, btnStop, btnPlay, btnFinish;
Private MediaRecorder recorder;
Private MediaPlayer player;
Private File audioFile;
Private Uri fileUri;

Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. my_audio_record );
StateView = (TextView) this. findViewById (R. id. view_state );
StateView. setText ("Getting started ");
BtnStart = (Button) this. findViewById (R. id. btn_start );
BtnStop = (Button) this. findViewById (R. id. btn_stop );
BtnPlay = (Button) this. findViewById (R. id. btn_play );
BtnFinish = (Button) this. findViewById (R. id. btn_finish );
BtnStop. setEnabled (false );
BtnPlay. setEnabled (false );

}

Public void onClick (View v ){
Int id = v. getId ();
Switch (id ){
Case R. id. btn_start:
// Start recording
// We Need to instantiate a MediaRecorder object and set it accordingly.
Recorder = new MediaRecorder ();
// Specify AudioSource as MIC (Microphone audio source), which is the longest
Recorder. setAudioSource (MediaRecorder. AudioSource. MIC );
// Specify OutputFormat. We select 3gp format.
// Other format, MPEG-4: This will specify the recording file as MPEG-4 format, can protect Audio and Video
// RAW_AMR: records the original file. This operation only supports audio recording and requires the audio encoding to be AMR_NB.
// THREE_GPP: The recorded file is a 3gp file that supports audio and video recording.
Recorder. setOutputFormat (MediaRecorder. OutputFormat. THREE_GPP );
// Specifies the Audio encoding method. Currently, only the AMR_NB format is used.
Recorder. setAudioEncoder (MediaRecorder. AudioEncoder. AMR_NB );
// Next we need to specify the storage path of the recorded file
File fpath = new File (Environment. getExternalStorageDirectory (). getAbsolutePath () + "/data/files /");
Fpath. mkdirs (); // create a folder
Try {
// Create a temporary file
AudioFile = File. createTempFile ("recording", ". 3gp", fpath );
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}

Recorder. setOutputFile (audioFile. getAbsolutePath ());

// The recording starts below
Try {
Recorder. prepare ();
} Catch (IllegalStateException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}

Recorder. start ();

StateView. setText ("Recording ");
BtnStart. setEnabled (false );
BtnPlay. setEnabled (false );
BtnStop. setEnabled (true );

Break;
Case R. id. btn_stop:
Recorder. stop ();
Recorder. release ();

// Then we can store our recording files in MediaStore.
ContentValues values = new ContentValues ();
Values. put (MediaStore. Audio. Media. TITLE, "this is my first record-audio ");
Values. put (MediaStore. Audio. Media. DATE_ADDED, System. currentTimeMillis ());
Values. put (MediaStore. Audio. Media. DATA, audioFile. getAbsolutePath ());
FileUri = this. getContentResolver (). insert (MediaStore. Audio. Media. EXTERNAL_CONTENT_URI, values );
// After recording, we instantiate a MediaPlayer object and prepare to play the video.
Player = new MediaPlayer ();
Player. setOnCompletionListener (new MediaPlayer. OnCompletionListener (){

@ Override
Public void onCompletion (MediaPlayer arg0 ){
// Update the status
StateView. setText ("prepare for recording ");
BtnPlay. setEnabled (true );
BtnStart. setEnabled (true );
BtnStop. setEnabled (false );
}
});

// Prepare for playing
Try {
Player. setDataSource (audioFile. getAbsolutePath ());
Player. prepare ();
} Catch (IllegalArgumentException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (IllegalStateException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}

// Update the status
StateView. setText ("prepare for playback ");
BtnPlay. setEnabled (true );
BtnStart. setEnabled (true );
BtnStop. setEnabled (false );
Break;
Case R. id. btn_play:
// Play the recording
// Note that the MediaPlayer has been instantiated at the end of the recording and is ready for playing.
Player. start ();
// Update the status
StateView. setText ("playing ");
BtnStart. setEnabled (false );
BtnStop. setEnabled (false );
BtnPlay. setEnabled (false );

// The status must be updated when the playback ends.
Break;
Case R. id. btn_finish:
// After recording is completed, the Uri of the recorded audio is returned.
Intent intent = new Intent ();
Intent. setData (fileUri );
This. setResult (RESULT_ OK, intent );
This. finish ();
Break;

}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.