[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-27 (世界標準時間)。"],[],[],null,["An app can record the video or audio that is being played from another app. Such\napps must handle the\n[`MediaProjection`](/reference/android/media/projection/MediaProjection) token\ncorrectly. This page explains how. It also shows how a device admin can disable\nthe ability to record any screen snapshots, and how an audio app can prevent\nother apps from recording the content it plays.\n\nHow to handle a `MediaProjection` token\n\nThe [`MediaProjection`](/reference/android/media/projection/MediaProjection) API\nallows apps to acquire a `MediaProjection` token that gives them one time access\nto capture screen contents or audio. The Android OS asks the user for their\npermission before granting the token to your app.\n\nThe OS displays the active `MediaProjection` tokens in the Quick Settings UI and\nallows users to withdraw access to a token at any time. When this happens the\nvirtual displays or audio streams associated with the session stop receiving\nmedia streams. Your app must respond appropriately, otherwise it will continue\nto record audio silence or a black video stream.\n\nTo handle the loss of a token, register a callback on the `MediaProjection`\ninstance using the\n[`registerCallback`](/reference/android/media/projection/MediaProjection#registerCallback(android.media.projection.MediaProjection.Callback,%20android.os.Handler))\nmethod, and stop recording when the\n[`onStop`](/reference/android/media/projection/MediaProjection.Callback#onStop())\nmethod is called.\n\nFor more information, see [Media projection](/media/grow/media-projection).\n\nCapture video\n\nSee the\n[ScreenCapture sample app](https://github.com/android/media-samples/tree/main/ScreenCapture)\nto learn how to learn how to use Media Projection API to capture a device's\nscreen in real time and show it on a SurfaceView.\n\nYou can use the [`DevicePolicyManager`](/reference/android/app/admin/DevicePolicyManager)\nto prevent screen recording. For enterprise accounts (Android for Work), the\nadministrator can disable the collection of assistant data for the work profile\nby using the\n[setScreenCaptureDisabled](/reference/android/app/admin/DevicePolicyManager#setScreenCaptureDisabled(android.content.ComponentName,%20boolean))\nmethod.\n\nThe codelab\n[Managing Android Devices Without an App](https://developer.android.com/codelabs/android-management-api#8)\nshows how to forbid screenshots.\n\nCapture audio playback\n\nThe [AudioPlaybackCapture API](/guide/topics/media/playback-capture) was introduced in Android 10. This API\ngives apps the ability to copy the audio being played by other apps. This\nfeature is the analog of screen capture, but for audio. The primary use case is\nfor streaming apps that want to capture the audio being played by games.\n\nNote that the AudioPlaybackCapture API does not affect the latency of the app whose audio is\nbeing captured.\n\nBuilding a capture app\n\nFor security and privacy, playback capture imposes some limitations.\nTo be able to capture audio, an app must meet these requirements:\n\n- The app must have the [`RECORD_AUDIO`](/reference/android/Manifest.permission#RECORD_AUDIO) permission.\n- The app must bring up the prompt displayed by [`MediaProjectionManager.createScreenCaptureIntent()`](/reference/android/media/projection/MediaProjectionManager#createScreenCaptureIntent()), and the user must approve it.\n- The capturing and playing apps must be in the same user profile.\n\nTo capture audio from another app, your app must build an\n[`AudioRecord`](/reference/android/media/AudioRecord) object\nand add an\n[`AudioPlaybackCaptureConfiguration`](/reference/android/media/AudioPlaybackCaptureConfiguration)\nto it. Follow these steps:\n\n1. Call [`AudioPlaybackCaptureConfiguration.Builder.build()`](/reference/android/media/AudioPlaybackCaptureConfiguration.Builder#build()) to build an [`AudioPlaybackCaptureConfiguration`](/reference/android/media/AudioPlaybackCaptureConfiguration).\n2. Pass the configuration to the `AudioRecord` by calling [`setAudioPlaybackCaptureConfig`](/reference/android/media/AudioRecord.Builder#setAudioPlaybackCaptureConfig(AudioPlaybackCaptureConfiguration)).\n\nControlling audio capture\n\nYour app can control what types of content it can record, and what other\nkinds of app can record its own playback.\n\nConstraining capture by audio content\n\nAn app can limit which audio it can capture by using these\nmethods:\n\n- Pass an `AUDIO_USAGE` to [AudioPlaybackCaptureConfiguration.addMatchingUsage()](/reference/android/media/AudioPlaybackCaptureConfiguration.addMatchingUsage(int)) to permit capturing a specific usage. Call the method multiple times to specify more than one usage.\n- Pass an `AUDIO_USAGE` to [AudioPlaybackCaptureConfiguration.excludeUsage()](/reference/android/media/AudioPlaybackCaptureConfiguration.excludeUsage(int)) to forbid capturing that usage. Call the method multiple times to specify more than one usage.\n- Pass a UID to [AudioPlaybackCaptureConfiguration.addMatchingUid()](/reference/android/media/AudioPlaybackCaptureConfiguration.addMatchingUid(int)) to only capture apps with a specific UID. Call the method multiple times to specify more than one UID.\n- Pass a UID to [AudioPlaybackCaptureConfiguration.excludeUid()](/reference/android/media/AudioPlaybackCaptureConfiguration.excludeUid(int)) to forbid capturing that UID. Call the method multiple times to specify more than one UID.\n\nNote that you cannot use the `addMatchingUsage()` and `excludeUsage()` methods\ntogether. You must choose one or the other. Likewise, you cannot use `addMatchingUid()` and `excludeUid()`\nat the same time.\n\nConstraining capture by other apps\n\nYou can configure an app to prevent other apps from capturing its audio.\nThe audio coming from an app can be captured only if the app meets these requirements:\n\nUsage\n\nThe player producing the audio must [set its usage](/reference/android/media/AudioAttributes.Builder#setUsage(int))\nto [`USAGE_MEDIA`](/reference/android/media/AudioAttributes#USAGE_MEDIA),\n[`USAGE_GAME`](/reference/android/media/AudioAttributes#USAGE_GAME), or\n[`USAGE_UNKNOWN`](/reference/android/media/AudioAttributes#USAGE_UNKNOWN).\n\nCapture policy\n\nThe player's capture policy must be\n[`AudioAttributes.ALLOW_CAPTURE_BY_ALL`](/reference/android/media/AudioAttributes#ALLOW_CAPTURE_BY_ALL),\nwhich allows other apps to capture playback. This can be done in a number of ways:\n\n- To enable capture on all players, include `android:allowAudioPlaybackCapture=\"true\"` in the app's `manifest.xml` file.\n- You can also enable capture on all players by calling [`AudioManager.setAllowedCapturePolicy(AudioAttributes.ALLOW_CAPTURE_BY_ALL)`](/reference/android/media/AudioManager#setAllowedCapturePolicy(int)).\n- You can set the policy on an individual player when you build it using [`AudioAttributes.Builder.setAllowedCapturePolicy(AudioAttributes.ALLOW_CAPTURE_BY_ALL)`](/reference/android/media/AudioAttributes.Builder#setAllowedCapturePolicy(int)). (If you are using [`AAudio`](/ndk/guides/audio/aaudio/aaudio) call [`AAudioStreamBuilder_setAllowedCapturePolicy(AAUDIO_ALLOW_CAPTURE_BY_ALL)`](/ndk/reference/group/audio#aaudiostreambuilder_setAllowedCapturePolicy).)\n\nIf these prerequisites are met, any audio produced by the player can be captured.\n| **Note:** The ability for an app's audio to be captured also depends on the app's `targetSdkVersion`.\n|\n| - By default, apps that target versions up to and including to Android 9.0 do not permit playback capture. To enable it, include `android:allowAudioPlaybackCapture=\"true\"` in the app's `manifest.xml` file.\n| - By default, apps that target Android 10 (API level 29) or higher allow their audio to be captured. To disable playback capture, include `android:allowAudioPlaybackCapture=\"false\"` in the app's `manifest.xml` file.\n\nDisabling system capture\n\nThe protections permitting capture described above apply only to apps. Android\nsystem components can capture playback by default.\nMany of these components are customized by Android vendors and support features\nlike accessibility and captioning. For this reason it is recommended that apps\nallow the system to capture their playback. In the rare case when you do not\nwant the system to capture your app's playback, set the capture policy to\n[`ALLOW_CAPTURE_BY_NONE`](/reference/android/media/AudioAttributes#ALLOW_CAPTURE_BY_NONE).\n\nSetting policy at runtime\n\nYou can call `AudioManager.setAllowedCapturePolicy()` to change the capture\npolicy while an app is running. If a MediaPlayer or AudioTrack is playing\nwhen you call the method, the audio is not affected. You must close and reopen\nthe player or track for the policy change to take effect.\n\nPolicy = manifest + AudioManager + AudioAttributes\n\nSince the capture policy can be specified in several places, it's important\nto understand how the effective policy is determined.\nThe most restrictive capture policy is always applied. For example, an app whose\nmanifest includes `setAllowedCapturePolicy=\"false\"` will never permit non-system\napps to capture its audio, even if `AudioManager#setAllowedCapturePolicy` is set\nto `ALLOW_CAPTURE_BY_ALL`. Similarly, if the\n`AudioManager#setAllowedCapturePolicy` is set to `ALLOW_CAPTURE_BY_ALL` and the\nmanifest sets `setAllowedCapturePolicy=\"true\"`, but the media player's\n`AudioAttributes` were built with\n`AudioAttributes.Builder#setAllowedCapturePolicy(ALLOW_CAPTURE_BY_SYSTEM)`, then\nthis media player will not be capturable by non-system apps.\n\nThe table below summarizes the effect of the manifest attribute and the\neffective policy:\n\n| allowAudioPlaybackCapture | ALLOW_CAPTURE_BY_ALL | ALLOW_CAPTURE_BY_SYSTEM | ALLOW_CAPTURE_BY_NONE |\n|---------------------------|----------------------|-------------------------|-----------------------|\n| true | any app | system only | no capture |\n| false | system only | system only | no capture |"]]