问题描述
我正在尝试使用MediaProjections屏幕捕获api和Android 10中的AudioPlaybackCapture捕获屏幕,但是我无法做到这一点。请在这里帮助我。 AudioPlaybackCapture api repository我正在使用此 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}")
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)