MediaRecord錄像聲音小問題

由於MediaRecord提供接口較少,沒有增加音量的接口。顧採取分開錄製音視頻再合成的方式實現.
1、通過MediaRecord和AudioRecord同時分別錄製出無聲MP4視頻和raw音頻。
2、將raw音量增益並封裝成wav
3、將wav轉碼成AAC,並與MP4視頻合成爲目標錄像。(此處參考http://blog.csdn.net/smile3670/article/details/41279749)

錄音增益封裝成WAV音頻部分代碼如下

public class AudioMessageRecord {
	private static final String TAG = "MessageRecord";
	// 音頻獲取源
	private int audioSource = MediaRecorder.AudioSource.MIC;
	// 設置音頻採樣率,44100是目前的標準,但是某些設備仍然支持22050,16000,11025
	private static int sampleRateInHz = 8000;
	// 設置音頻的錄製的聲道CHANNEL_IN_STEREO爲雙聲道,CHANNEL_CONFIGURATION_MONO爲單聲道(CHANNEL_IN_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 AudioRecord audioRecord;
	private boolean isRecord = false;// 設置正在錄製的狀態
	// AudioName裸音頻數據文件
	private static final String AudioName = "/sdcard/audio.raw";
	// NewAudioName可播放的音頻文件
	private String outFileName = "/sdcard/audio.wav";

	public AudioMessageRecord(){
		creatAudioRecord();
	}
	
	private void creatAudioRecord() {
		// 獲得緩衝區字節大小
		bufferSizeInBytes = AudioRecord.getMinBufferSize(sampleRateInHz,
				channelConfig, audioFormat);
		Log.e(TAG, "bufferSizeInBytes = " + bufferSizeInBytes);
		// 創建AudioRecord對象
		audioRecord = new AudioRecord(audioSource, sampleRateInHz,
				channelConfig, audioFormat, bufferSizeInBytes);
	}

	public void startRecord(String fileName) {
		outFileName = fileName;
		audioRecord.startRecording();
		// 讓錄製狀態爲true
		isRecord = true;
		// 開啓音頻文件寫入線程
		new Thread(new AudioRecordThread()).start();
	}

	public 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, outFileName);// 給裸數據加上頭文件\WAV格式
			
			if (mAudioMessageRecordCallback != null) {
				mAudioMessageRecordCallback.audioRecordEnd();
			}
		}
	}

	/**
	 * 這裏將數據寫入文件,但是並不能播放,因爲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);
			/*for(int i=0;i<audiodata.length;i++)   
			{
				//音量大小,此種方法放大聲音會有底噪聲
				audiodata[i]= (byte) (audiodata[i] * 5);//數字決定大小
			}*/
			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;
		
		//JNI增益處理必須是320
		byte[] data = new byte[320];
		try {
			in = new FileInputStream(inFilename);
			out = new FileOutputStream(outFilename);
			totalAudioLen = in.getChannel().size();
			totalDataLen = totalAudioLen + 36;
			WriteWaveFileHeader(out, totalAudioLen, totalDataLen,
					longSampleRate, channels, byteRate);
			//音頻會比視頻晚一秒結束,經過試驗,減去最後一秒的數據
			Log.i("count", "in.available() = " + in.available());
			int count = in.available() / 320;
			Log.i("count", "all count = " + count);
			while (in.read(data) != -1) {
				if (count --> 90) {
					//通過JNI接口增益
					SettingsJni.retrieveSettings().kotiNsAndAgc(data);
					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);
	}
	
	AudioMessageRecordCallback mAudioMessageRecordCallback;
	public void setAudioCallback(AudioMessageRecordCallback audioMessageRecordCallback){
		mAudioMessageRecordCallback = audioMessageRecordCallback;
	}
	public interface AudioMessageRecordCallback{
		/**
		 * 錄音結束
		 */
		void audioRecordEnd();
	}

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章