我正在尝试在 android 10 中使用带有 AudioPlaybackCapture 的 MediaProjections 屏幕捕获 api 来捕获屏幕。但我无法做到这一点。请在这里帮助我。 AudioPlaybackCapture api 存储库我正在使用屏幕捕获跟踪此AudioPlaybackCapture api代码。视频正在录制,但没有音频。
fun initRecorder() {
// if (mMediaRecorder == null) {
mMediaRecorder = MediaRecorder().apply {
setVideoSource(SURFACE)
try {
// setAudioSource(MIC)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
//startAudioCapture()
val config = AudioPlaybackCaptureConfiguration.Builder(this)
.addMatchingUsage(AudioAttributes.USAGE_MEDIA) // TODO provide UI options for inclusion/exclusion
.addMatchingUsage(AudioAttributes.USAGE_UNKNOWN)
.addMatchingUsage(AudioAttributes.USAGE_GAME)
.build()
/**
* Using hardcoded values for the audio format, Mono PCM samples with a sample rate of 8000Hz
* These can be changed according to your application's needs
*/
val audioFormat = AudioFormat.Builder()
.setEncoding(AudioFormat.ENCODING_PCM_16BIT)
.setSampleRate(8000)
.setChannelMask(AudioFormat.CHANNEL_IN_MONO)
.build()
audioRecord = AudioRecord.Builder()
.setAudioFormat(audioFormat)
// For optimal performance, the buffer size
// can be optionally specified to store audio samples.
// If the value is not specified,
// uses a single frame and lets the
// native code figure out the minimum buffer size.
.setBufferSizeInBytes(BUFFER_SIZE_IN_BYTES)
.setAudioPlaybackCaptureConfig(config)
.build()
mMediaRecorder!!.setAudioSource(audioRecord!!.audioSource)
// setAudioEncoder(AAC)
// val rate = 44100
// setAudioSamplingRate(rate);
/* if (audioRecord != null) {
}*/
} else {
setAudioSource(MediaRecorder.AudioSource.MIC)
setAudioEncoder(AAC)
val rate = 44100
setAudioSamplingRate(rate);
}
} catch (t: Throwable) {
}
setOutputFormat(MPEG_4)
val frameRate = 30
setVideoFrameRate(frameRate)
setVideoEncoder(H264)
//
setVideoSize(DISPLAY_WIDTH, DISPLAY_HEIGHT)
val videoBitRate = 8000000
setVideoEncodingBitRate(videoBitRate)
val bitDepth = 16;
val sampleRate = 44100
val bitRate = sampleRate * bitDepth;
setAudioEncodingBitRate(bitRate)
// setAudioSamplingRate(sampleRate);
val filePath = getFilePath()
setOutputFile(filePath)
try {
prepare()
} catch (fe: FileNotFoundException) {
print("Failed to prepare the media recorder. ${fe.message}")
} catch (t: Throwable) {
print("Failed to prepare the media recorder. ${t.message}")
}
}
}