首先要做的是设置属性REMOTE_SUBMIX
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.REMOTE_SUBMIX);
Android 10及以下的录屏
目标 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);
}
Android 11
目标frameworks\av\services\audiopolicy\enginedefault\src\Engine.cpp
if ((remoteSubmix = availableOutputDevices.getDevice(
AUDIO_DEVICE_OUT_REMOTE_SUBMIX, String8("0"),
AUDIO_FORMAT_DEFAULT)) != nullptr) {
devices2.add(remoteSubmix);
}
修改为
if ((remoteSubmix = availableOutputDevices.getDevice(
AUDIO_DEVICE_OUT_REMOTE_SUBMIX, String8("0"),
AUDIO_FORMAT_DEFAULT)) != nullptr) {
devices2 = availableOutputDevices.getDevicesFromTypes({
AUDIO_DEVICE_OUT_REMOTE_SUBMIX, AUDIO_DEVICE_OUT_WIRED_HEADPHONE, AUDIO_DEVICE_OUT_SPEAKER});
}
总结
要实现安卓录制系统声音,正常情况下是获取不到的,本文采用的是修改源码的方式来实现。
相关资源: Android中实现录制内置声音
|