在Android中錄音可以用MediaRecord錄音,操作比較簡單。但是不夠專業,就是不能對音頻進行處理。如果要進行音頻的實時的處理或者音頻的一些封裝
就可以用AudioRecord來進行錄音了。
這裏給出一段代碼。實現了AudioRecord的錄音和WAV格式音頻的封裝。
用AudioTrack和AudioTrack類可以進行邊錄邊播,可以參考:http://blog.sina.com.cn/s/blog_6309e1ed0100j1rw.html
我們這裏的代碼沒有播放。但是有封裝和詳解,如下:
- package com.ppmeet;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import android.app.Activity;
- import android.graphics.PixelFormat;
- import android.media.AudioFormat;
- import android.media.AudioRecord;
- import android.media.MediaRecorder;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.Window;
- import android.view.WindowManager;
- import android.widget.Button;
- /**
- * class name:TestAudioRecord<BR>
- * class description:用AudioRecord來進行錄音<BR>
- * PS: <BR>
- *
- * @version 1.00 2011/09/21
- * @author CODYY)peijiangping
- */
- public class TestAudioRecord extends Activity {
- // 音頻獲取源
- private int audioSource = MediaRecorder.AudioSource.MIC;
- // 設置音頻採樣率,44100是目前的標準,但是某些設備仍然支持22050,16000,11025
- private static int sampleRateInHz = 44100;
- // 設置音頻的錄製的聲道CHANNEL_IN_STEREO爲雙聲道,CHANNEL_CONFIGURATION_MONO爲單聲道
- private static int channelConfig = AudioFormat.CHANNEL_IN_STEREO;
- // 音頻數據格式:PCM 16位每個樣本。保證設備支持。PCM 8位每個樣本。不一定能得到設備支持。
- private static int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
- // 緩衝區字節大小
- private int bufferSizeInBytes = 0;
- private Button Start;
- private Button Stop;
- private AudioRecord audioRecord;
- private boolean isRecord = false;// 設置正在錄製的狀態
- //AudioName裸音頻數據文件
- private static final String AudioName = "/sdcard/love.raw";
- //NewAudioName可播放的音頻文件
- private static final String NewAudioName = "/sdcard/new.wav";
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- getWindow().setFormat(PixelFormat.TRANSLUCENT);// 讓界面橫屏
- requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉界面標題
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
- WindowManager.LayoutParams.FLAG_FULLSCREEN);
- // 重新設置界面大小
- setContentView(R.layout.main);
- init();
- }
- private void init() {
- Start = (Button) this.findViewById(R.id.start);
- Stop = (Button) this.findViewById(R.id.stop);
- Start.setOnClickListener(new TestAudioListener());
- Stop.setOnClickListener(new TestAudioListener());
- creatAudioRecord();
- }
- private void creatAudioRecord() {
- // 獲得緩衝區字節大小
- bufferSizeInBytes = AudioRecord.getMinBufferSize(sampleRateInHz,
- channelConfig, audioFormat);
- // 創建AudioRecord對象
- audioRecord = new AudioRecord(audioSource, sampleRateInHz,
- channelConfig, audioFormat, bufferSizeInBytes);
- }
- class TestAudioListener implements OnClickListener {
- @Override
- public void onClick(View v) {
- if (v == Start) {
- startRecord();
- }
- if (v == Stop) {
- stopRecord();
- }
- }
- }
- private void startRecord() {
- audioRecord.startRecording();
- // 讓錄製狀態爲true
- isRecord = true;
- // 開啓音頻文件寫入線程
- new Thread(new AudioRecordThread()).start();
- }
- private void stopRecord() {
- close();
- }
- private void close() {
- if (audioRecord != null) {
- System.out.println("stopRecord");
- isRecord = false;//停止文件寫入
- audioRecord.stop();
- audioRecord.release();//釋放資源
- audioRecord = null;
- }
- }
- class AudioRecordThread implements Runnable {
- @Override
- public void run() {
- writeDateTOFile();//往文件中寫入裸數據
- copyWaveFile(AudioName, NewAudioName);//給裸數據加上頭文件
- }
- }
- /**
- * 這裏將數據寫入文件,但是並不能播放,因爲AudioRecord獲得的音頻是原始的裸音頻,
- * 如果需要播放就必須加入一些格式或者編碼的頭信息。但是這樣的好處就是你可以對音頻的 裸數據進行處理,比如你要做一個愛說話的TOM
- * 貓在這裏就進行音頻的處理,然後重新封裝 所以說這樣得到的音頻比較容易做一些音頻的處理。
- */
- private void writeDateTOFile() {
- // new一個byte數組用來存一些字節數據,大小爲緩衝區大小
- byte[] audiodata = new byte[bufferSizeInBytes];
- FileOutputStream fos = null;
- int readsize = 0;
- try {
- File file = new File(AudioName);
- if (file.exists()) {
- file.delete();
- }
- fos = new FileOutputStream(file);// 建立一個可存取字節的文件
- } catch (Exception e) {
- e.printStackTrace();
- }
- while (isRecord == true) {
- readsize = audioRecord.read(audiodata, 0, bufferSizeInBytes);
- if (AudioRecord.ERROR_INVALID_OPERATION != readsize) {
- try {
- fos.write(audiodata);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- try {
- fos.close();// 關閉寫入流
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- // 這裏得到可播放的音頻文件
- private void copyWaveFile(String inFilename, String outFilename) {
- FileInputStream in = null;
- FileOutputStream out = null;
- long totalAudioLen = 0;
- long totalDataLen = totalAudioLen + 36;
- long longSampleRate = sampleRateInHz;
- int channels = 2;
- long byteRate = 16 * sampleRateInHz * channels / 8;
- byte[] data = new byte[bufferSizeInBytes];
- try {
- in = new FileInputStream(inFilename);
- out = new FileOutputStream(outFilename);
- totalAudioLen = in.getChannel().size();
- totalDataLen = totalAudioLen + 36;
- WriteWaveFileHeader(out, totalAudioLen, totalDataLen,
- longSampleRate, channels, byteRate);
- while (in.read(data) != -1) {
- out.write(data);
- }
- in.close();
- out.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- * 這裏提供一個頭信息。插入這些信息就可以得到可以播放的文件。
- * 爲我爲啥插入這44個字節,這個還真沒深入研究,不過你隨便打開一個wav
- * 音頻的文件,可以發現前面的頭文件可以說基本一樣哦。每種格式的文件都有
- * 自己特有的頭文件。
- */
- private void WriteWaveFileHeader(FileOutputStream out, long totalAudioLen,
- long totalDataLen, long longSampleRate, int channels, long byteRate)
- throws IOException {
- byte[] header = new byte[44];
- header[0] = 'R'; // RIFF/WAVE header
- header[1] = 'I';
- header[2] = 'F';
- header[3] = 'F';
- header[4] = (byte) (totalDataLen & 0xff);
- header[5] = (byte) ((totalDataLen >> 8) & 0xff);
- header[6] = (byte) ((totalDataLen >> 16) & 0xff);
- header[7] = (byte) ((totalDataLen >> 24) & 0xff);
- header[8] = 'W';
- header[9] = 'A';
- header[10] = 'V';
- header[11] = 'E';
- header[12] = 'f'; // 'fmt ' chunk
- header[13] = 'm';
- header[14] = 't';
- header[15] = ' ';
- header[16] = 16; // 4 bytes: size of 'fmt ' chunk
- header[17] = 0;
- header[18] = 0;
- header[19] = 0;
- header[20] = 1; // format = 1
- header[21] = 0;
- header[22] = (byte) channels;
- header[23] = 0;
- header[24] = (byte) (longSampleRate & 0xff);
- header[25] = (byte) ((longSampleRate >> 8) & 0xff);
- header[26] = (byte) ((longSampleRate >> 16) & 0xff);
- header[27] = (byte) ((longSampleRate >> 24) & 0xff);
- header[28] = (byte) (byteRate & 0xff);
- header[29] = (byte) ((byteRate >> 8) & 0xff);
- header[30] = (byte) ((byteRate >> 16) & 0xff);
- header[31] = (byte) ((byteRate >> 24) & 0xff);
- header[32] = (byte) (2 * 16 / 8); // block align
- header[33] = 0;
- header[34] = 16; // bits per sample
- header[35] = 0;
- header[36] = 'd';
- header[37] = 'a';
- header[38] = 't';
- header[39] = 'a';
- header[40] = (byte) (totalAudioLen & 0xff);
- header[41] = (byte) ((totalAudioLen >> 8) & 0xff);
- header[42] = (byte) ((totalAudioLen >> 16) & 0xff);
- header[43] = (byte) ((totalAudioLen >> 24) & 0xff);
- out.write(header, 0, 44);
- }
- @Override
- protected void onDestroy() {
- close();
- super.onDestroy();
- }
- }