Android中實現錄製內置聲音

Android中實現對內置聲源的錄製有兩種方案java

1、將應用打包爲系統應用

AudioSource輸入源
android

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能夠實現內錄功能,有兩點比較麻煩:
git

(1)須要系統權限 github

(2)會截走揚聲器和耳機的聲音,也就是說再錄音時本地沒法播放聲音
bash

  • 對於系統權限,須要在在AndroidManifest.xml添加 android:sharedUserId="android.uid.system",而後使用系統簽名來打包應用,這樣就第三方應用打包爲系統應用,就能夠使用系統權限了。 
對於系統簽名,若是系統使用的是原生的系統,就能夠直接在AOSP發佈的Android源碼獲取簽名證書

https://github.com/aosp-mirror/platform_build/tree/master/target/product/security
網絡


用signapk.jar對應用進行簽名  
app

java -jar signapk.jar platform.x509.pem platform.pk8 app-unsigned.apk signed.apk
複製代碼

signapk獲取地址:
ui

https://github.com/techexpertize/SignApkspa

https://github.com/maks/aosp-signapk
.net

  • 對於截走揚聲器和耳機的聲音問題 

修改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

2、使用Root權限修改底層驅動

第二種方法來自開源項目:

https://github.com/iwo/SCR-Screen-Recorder-app

相關文章
相關標籤/搜索