createMediaProjectionVideoTrack
abstract fun createMediaProjectionVideoTrack(intent: Intent, callback: MediaProjection.Callback): LocalVideoTrack
Creates a LocalVideoTrack backed by MediaProjection.
The intent can be obtained after completing the MediaProjection permission request flow.
The calling app must validate that the result code is Activity.RESULT_OK before calling this method.
override fun onCreate(savedInstanceState: Bundle?) {
val manager = getSystemService(MEDIA_PROJECTION_SERVICE) as MediaProjectionManager
val intent = manager.createScreenCaptureIntent()
startActivityForResult(intent, REQUEST_CODE_MEDIA_PROJECTION)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode != REQUEST_CODE_MEDIA_PROJECTION) return
if (resultCode != Activity.RESULT_OK) return
if (data == null) return
val track = factory.createMediaProjectionVideoTrack(data, callback)
}
Content copied to clipboard
Starting with Android 10, you must also create a foreground service with mediaProjection
type:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<application
<service
android:enabled="true"
android:name=".MediaProjectionService"
android:foregroundServiceType="mediaProjection" />
</application>
Content copied to clipboard
Return
a MediaProjection-backed LocalVideoTrack
Parameters
intent
a data parameter obtained from Activity.onActivityResult
callback
a callback to indicate when the session is no longer valid
Throws
if MediaConnectionFactory has been disposed