MediaRecorder
AudioRecord
- 需要的權限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
/**
* 開始錄音
*/
class startRecordListener implements OnClickListener {
@Override public void onClick(View v) {
try {
MediaRecorder mRecorder = new MediaRecorder();
// 設定聲音源為麥克風
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
// 設定輸出格式為3gp
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
// 設定輸出檔案路徑,mFileName為錄音音頻輸出路徑
mRecorder.setOutputFile(mFileName);
// 設定聲音解碼AMR_NB
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
// 媒體錄制器準備
mRecorder.prepare();
// 開始錄制
mRecorder.start();
} catch (IOException e) {
}
}
}
/**
* 停止錄音
*/
class stopRecordListener implements OnClickListener {
@Override public void onClick(View v) {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
}
2.AudioRecord
AudioRecord錄音步驟:
1. 建立一個資料流。
2. 構造一個AudioRecord對象,其中需要的最小錄音緩存buffer大小可以通過getMinBufferSize方法得到。如果buffer容量過小,将導緻對象構造的失敗。
3. 初始化一個buffer,該buffer大于等于AudioRecord對象用于寫聲音資料的buffer大小。
4. 開始錄音。
5. 從AudioRecord中讀取聲音資料到初始化buffer,将buffer中資料導入資料流。
6. 停止錄音。
7. 關閉資料流。
錄音代碼:
/**
* 錄音,需要在新的線程中執行,防止主線程阻塞
*/
private void record() {
int frequency = 11025;
int channelConfiguration = AudioFormat.CHANNEL_IN_STEREO;
int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
try {
// 建立一個資料輸入流往檔案裡寫資料
OutputStream os = new FileOutputStream(mFileName);
BufferedOutputStream bos = new BufferedOutputStream(os);
DataOutputStream dos = new DataOutputStream(bos);
// 建立一個AudioRecord對象來錄音
int bufferSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
AudioRecord mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, frequency, channelConfiguration,
audioEncoding, bufferSize);
short[] buffer = new short[bufferSize];
// 開始錄音
mAudioRecord.startRecording();
isRecording = true;
Log.e(TAG, "record: 錄音開始");
while (isRecording) {
int bufferReadResult = mAudioRecord.read(buffer, 0, bufferSize);
for (int i = 0; i < bufferReadResult; i++) {
dos.writeShort(buffer[i]);
Log.e(TAG, "record: 錄音中...");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 停止錄音
*/
class stopRecordListener implements OnClickListener {
@Override public void onClick(View v) {
mAudioRecord.stop();
Log.e(TAG, "record: 錄音結束");
}
}
播放代碼:
/**
* 播放錄音
*/
class startPlayListener implements OnClickListener {
@Override public void onClick(View v) {
try {
// Get the file we want to playback.
File file = new File(mFileName);
// Get the length of the audio stored in the file (16 bit so 2 bytes per short)
// and create a short array to store the recorded audio.
int musicLength = (int) (file.length() / 2);
short[] music = new short[musicLength];
// Create a DataInputStream to read the audio data back from the saved file
InputStream is = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
DataInputStream dis = new DataInputStream(bis);
// Read the file into the music array.
int i = 0;
while (dis.available() > 0) {
music[i++] = dis.readShort();
}
// close the input streams
dis.close();
// Create a new AudioTrack object using the same parameters as the AudioRecord
// object used to create the file.
AudioTrack audioTrack =
new AudioTrack(AudioManager.STREAM_MUSIC, 11025, AudioFormat.CHANNEL_IN_STEREO,
AudioFormat.ENCODING_PCM_16BIT, musicLength * 2, AudioTrack.MODE_STREAM);
Log.e(TAG, "onClick: 開始播放");
// Start playback
audioTrack.play();
// Write the music buffer to the AudioTrack object
audioTrack.write(music, 0, musicLength);
Log.e(TAG, "onClick: 播放結束");
audioTrack.stop();
} catch (Exception e) {
e.printStackTrace();
}
}
}