AudioCaptureSample icon indicating copy to clipboard operation
AudioCaptureSample copied to clipboard

No audio recorded, only zeros in file

Open darwusch opened this issue 4 years ago • 4 comments

When I run this code in the emulator when playing music with another app, nothing is recorded. I tried several music apps. All permissions are given. I do get the popup message about the recording and I see the icon on top about the recoding. The recoding file is saved, but when I open it, it only contains zeros. I tried it on android 10 and 11. Android 10: Automotive emulator and Polestar 2 Android 11: Automotive emulator (both with headset on and off)

darwusch avatar Feb 21 '22 07:02 darwusch

Hi julioz: I use MediaPlayer in my app and play a streaming song url. I encounter all values are zero of capturedAudioSamples array event though I use real device.

But, I can record audio when I use Youtube App that play audio.

Do you know what happen?

Cheng-Yi-Nie avatar Feb 25 '22 02:02 Cheng-Yi-Nie

Hi julioz: I use MediaPlayer in my app and play a streaming song url. I encounter all values are zero of capturedAudioSamples array event though I use real device.

But, I can record audio when I use Youtube App that play audio.

Do you know what happen?

Somewhere I read that capturing audio only works on playing apps that are written with minimalSDK 29. Might be that this is one of the reasons.

darwusch avatar Feb 25 '22 12:02 darwusch

I found the infomation in Android Developer Document: Pass an AUDIO_USAGE to AudioPlaybackCaptureConfiguration.addMatchingUsage() to permit capturing a specific usage. Call the method multiple times to specify more than one usage.

I try to set the AudioPlaybackCaptureConfiguration. I set two MatchingUsage with AudioAttributes.USAGE_MEDIA and AudioAttributes.USAGE_UNKNOWN.

val config = AudioPlaybackCaptureConfiguration.Builder(mediaProjection!!) .addMatchingUsage(AudioAttributes.USAGE_MEDIA) .addMatchingUsage(AudioAttributes.USAGE_UNKNOWN) .build()

So that I can record both of my app audio from MediaPlayer and Youtube App audio.

polokoio456 avatar Mar 03 '22 05:03 polokoio456

Any idea what config will I have to use to capture audio from chrome browser app ?

navinkumarr avatar Sep 23 '22 17:09 navinkumarr