1. 程式人生 > >Android 支援多個APK同時錄音

Android 支援多個APK同時錄音

Android4.4 修改方法

原始碼路徑: sdk\hardware\libhardware_legacy\audio\AudioPolicyManagerBase.cpp
audio_io_handle_t AudioPolicyManagerBase::getInput()
在mpClientInterface->openInput前新增:

#ifdef ANDROID4_4_SUPPORT_MULIT_CHANNEL_RECORDING
	// Modify Tower 20181103: check wether have an AudioInputDescriptor Use the same profile
	for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
		sp<AudioInputDescriptor> desc;
		desc = mInputs.valueAt(input_index);
		if (desc->mProfile == profile) {
			desc->mRefCount++;
			desc->mSessions.add(session);
			return desc->mIoHandle;
		} 
	}
#endif

Android6.0 修改方法

原始碼路徑: sdk\frameworks\av\services\audiopolicy\managerdefault\AudioPolicyManager.cpp
status_t AudioPolicyManager::getInputForAttr()
在mpClientInterface->openInput前新增:

#ifdef ANDROID6_0_SUPPORT_MULIT_CHANNEL_RECORDING
	// Modify Tower 20181118: check wether have an AudioInputDescriptor Use the same profile
	for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
		sp<AudioInputDescriptor> desc;
		desc = mInputs.valueAt(input_index);
	    if (desc->mProfile == profile) {
			desc->mRefCount++;             // reference count add
			desc->mSessions.add(session);
		    *input = desc->mIoHandle;
			return NO_ERROR;
		} 
	}
#endif

說明

在4.4和6.0上, 對於該修改最變化的就是返回值. 無論是getInput 還是 getInputForAttr 函式主要的作用就是找到一個需求匹配的profile檔案, 然後建立一個RecordThread執行緒. 最後返回一個audio_io_handle_t input 一個隨機的整數值.

原型AudioRecord框架

修改後AudioRecord框架
[適用環境]: 後臺存在一個錄音執行緒, 例如北京智慧管家, 圖靈等時, 在啟動第三方錄音apk, 例如微信, QQ物聯等. 可以規避 AudioRecord: start() status -38 報錯. 正常錄音.

[補充]: 在 ATM7051H_Android4.4 和 RK3128_Android6.0 上測試驗證過. 請放心使用.