03 กรกฎาคม 2562
Posted by Don Turner, Developer Advocate for Android Media
In Android Q there's a new API which allows applications to capture the audio of other applications. It's called the AudioPlaybackCapture API and it enables some important use cases for easier content sharing and accessibility.
Some examples include:
There may be some situations where a developer wishes to disallow the capture of their app's audio. This article explains how audio capture works for users and how developers can disallow their app's audio from being captured if they need to.
In order to capture the audio of other apps the user must grant the record audio permission to the app doing the capturing.
AUDIO_RECORD permissions dialog
Additionally, before a capture session can be started the capturing app must call MediaProjectionManager.createScreenCaptureIntent()
. This will display the following dialog to the user:
Screen capture intent dialog
The user must tap "Start now" in order for a capturing session to be started. This will allow both video and audio to be captured.
Cast icon showing red in status bar
During a capture session the cast icon is shown in red in the status bar.
Whether your app's audio can be captured by default depends on your target API. Here's a table which summarizes the default behaviour:
Target API | Third party apps can capture your app's audio by default? | System apps and components can capture your app's audio by default? |
28 and below | No, the app needs to explicitly opt-in | Yes for audio with usage type MEDIA, GAME and UNKNOWN |
29 | Yes for audio with usage type MEDIA, GAME and UNKNOWN | Yes for audio with usage type MEDIA, GAME and UNKNOWN |
There may be situations where an app wishes to disallow its audio from being captured by other apps. This could be because the audio contains:
An app's audio capturing policy can be set either for all audio or for each individual audio player.
To disallow capture of all audio by third party apps you can do either of the following:
Add the following to your AndroidManifest.xml
|
<application
|
Programmatically disable capture by running the following code prior to playing audio | AudioManager.setAllowedCapturePolicy(ALLOW_CAPTURE_BY_SYSTEM)
|
To disallow capture for an individual player you can set the capture policy for the audio player when it is built by calling:
AudioAttributes.Builder.setAllowedCapturePolicy(ALLOW_CAPTURE_BY_SYSTEM)
This approach may be useful if your app plays content with differing licenses. For example, both copyrighted and royalty-free content.
By default system apps and components are allowed to capture an app's audio if its usage is MEDIA, GAME and UNKNOWN, as this enables important accessibility use cases, such as live captioning.
In rare cases where a developer wishes to disallow audio capture for system apps as well they can do so in a similar way to the approach for third party apps. Note that this will also disallow capture by third party apps.
This can only be done programmatically by running the following code before any audio is played:
AudioManager.setAllowedCapturePolicy(ALLOW_CAPTURE_BY_NONE)
To disallow capture for an individual player you can set the capture policy for the audio player when it is built:
AudioAttributes.Builder.setAllowedCapturePolicy(ALLOW_CAPTURE_BY_NONE)
If your app is targeting API 28 or below and you would like to enable audio capture add android:allowAudioPlaybackCapture="true"
to your app's manifest.xml
.
If you would like to disallow some or all of your audio from being captured then update your app according to the instructions above.
For more information check out the Audio Playback Capture API documentation.