Android中實現對內建聲源的錄製有兩種方案
一、將應用打包為系統應用
AudioSource輸入源
public final class AudioSource {
private AudioSource() {}
/** Default audio source **/
public static final int DEFAULT = 0;
/** Microphone audio source */
public static final int MIC = 1;
/** Voice call uplink (Tx) audio source */
public static final int VOICE_UPLINK = 2;
/** Voice call downlink (Rx) audio source */
public static final int VOICE_DOWNLINK = 3;
/** Voice call uplink + downlink audio source */
public static final int VOICE_CALL = 4;
/** Microphone audio source with same orientation as camera if available, the main
* device microphone otherwise */
public static final int CAMCORDER = 5;
public static final int VOICE_RECOGNITION = 6;
public static final int VOICE_COMMUNICATION = 7;
public static final int REMOTE_SUBMIX = 8;
}
複製程式碼
- DEFAULT:預設為MIC,android.permission.RECORD_AUDIO
- MIC:麥克風,android.permission.RECORD_AUDIO
- VOICE_UPLINK:電話錄音上行線路,android.permission.CAPTURE_AUDIO_OUTPUT,系統許可權不允許第三方app使用
- VOICE_DOWNLINK:電話錄音下行線路,android.permission.CAPTURE_AUDIO_OUTPUT,系統許可權不允許第三方app使用
- VOICE_CALL:電話錄音上下線路,android.permission.CAPTURE_AUDIO_OUTPUT,系統許可權不允許第三方app使用
- CAMCORDER:攝像頭的麥克風,android.permission.RECORD_AUDIO
- VOICE_RECOGNITION:語音識別,android.permission.RECORD_AUDIO
- VOICE_COMMUNICATION:網路電話,android.permission.RECORD_AUDIO
- REMOTE_SUBMIX:傳輸到遠端的音訊混合流,預設情況下如果用該項錄音,本地揚聲器或者耳機的聲音將會被截走,android.permission.CAPTURE_AUDIO_OUTPUT,系統許可權不允許第三方app使用
REMOTE_SUBMIX可以實現內錄功能,有兩點比較麻煩:
(1)需要系統許可權
(2)會截走揚聲器和耳機的聲音,也就是說再錄音時本地無法播放聲音
- 對於系統許可權,需要在在AndroidManifest.xml新增
android:sharedUserId="android.uid.system",然後使用系統簽名來打包應用,這樣就第三方應用打包為系統應用,就可以使用系統許可權了。
https://github.com/aosp-mirror/platform_build/tree/master/target/product/security
用signapk.jar對應用進行簽名
java -jar signapk.jar platform.x509.pem platform.pk8 app-unsigned.apk signed.apk
複製程式碼
signapk獲取地址:
https://github.com/techexpertize/SignApk
https://github.com/maks/aosp-signapk
- 對於截走揚聲器和耳機的聲音問題
修改framework下av/services/audiopolicy/AudioPolicyManager.cpp
if (mAvailableOutputDevices.getDevice(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, String8("0")) != 0) {
device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
}
複製程式碼
修改為:
if (mAvailableOutputDevices.getDevice(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, String8("0")) != 0) {
device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
device2 |= (availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADPHONE);
device2 |= (availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER);
}複製程式碼
現在就可以使用MediaRecorder.AudioSource.REMOTE_SUBMIX實現內錄功能了。
該方案只適用於知道系統簽名或者自己編譯系統原始碼的情況
參考文章:
https://www.jianshu.com/p/916ede1d1f48
https://blog.csdn.net/liuhaitao_share/article/details/41249175
https://blog.csdn.net/jinzhuojun/article/details/33748031
二、使用Root許可權修改底層驅動
第二種方法來自開源專案:
https://github.com/iwo/SCR-Screen-Recorder-app