I am trying to capture screen using MediaProjections screen capture api with AudioPlaybackCapture in android 10. but i am unable to do that. please help me here. AudioPlaybackCapture api repository i am following this AudioPlaybackCapture api code with Screen capture. video is getting record but there is not audio.
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}")
}
}
}