अगर आपको अपनी PiP विंडो में कंट्रोल (चलाएं, रोकें वगैरह) जोड़ने हैं, तो आपको हर कंट्रोल के लिए एक RemoteAction बनाना होगा.
- ब्रॉडकास्ट कंट्रोल के लिए कॉन्स्टेंट जोड़ें:
// Constant for broadcast receiver const val ACTION_BROADCAST_CONTROL = "broadcast_control" // Intent extras for broadcast controls from Picture-in-Picture mode. const val EXTRA_CONTROL_TYPE = "control_type" const val EXTRA_CONTROL_PLAY = 1 const val EXTRA_CONTROL_PAUSE = 2
- PiP विंडो में कंट्रोल के लिए,
RemoteActionsकी सूची बनाएं. - इसके बाद, हर बटन की कार्रवाइयां सेट करने के लिए,
BroadcastReceiverजोड़ें औरonReceive()को बदलें. रिसीवर और रिमोट ऐक्शन को रजिस्टर करने के लिए,DisposableEffectका इस्तेमाल करें. जब प्लेयर को हटा दिया जाता है, तब रिसीवर को अनरजिस्टर करें.@RequiresApi(Build.VERSION_CODES.O) @Composable fun PlayerBroadcastReceiver(player: Player?) { val isInPipMode = rememberIsInPipMode() if (!isInPipMode || player == null) { // Broadcast receiver is only used if app is in PiP mode and player is non null return } val context = LocalContext.current DisposableEffect(player) { val broadcastReceiver: BroadcastReceiver = object : BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent?) { if ((intent == null) || (intent.action != ACTION_BROADCAST_CONTROL)) { return } when (intent.getIntExtra(EXTRA_CONTROL_TYPE, 0)) { EXTRA_CONTROL_PAUSE -> player.pause() EXTRA_CONTROL_PLAY -> player.play() } } } ContextCompat.registerReceiver( context, broadcastReceiver, IntentFilter(ACTION_BROADCAST_CONTROL), ContextCompat.RECEIVER_NOT_EXPORTED ) onDispose { context.unregisterReceiver(broadcastReceiver) } } }
- अपनी रिमोट कार्रवाइयों की सूची को
PictureInPictureParams.Builderमें पास करें:val context = LocalContext.current val pipModifier = modifier.onGloballyPositioned { layoutCoordinates -> val builder = PictureInPictureParams.Builder() builder.setActions( listOfRemoteActions() ) if (shouldEnterPipMode && player != null && player.videoSize != VideoSize.UNKNOWN) { val sourceRect = layoutCoordinates.boundsInWindow().toAndroidRectF().toRect() builder.setSourceRectHint(sourceRect) builder.setAspectRatio( Rational(player.videoSize.width, player.videoSize.height) ) } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { builder.setAutoEnterEnabled(shouldEnterPipMode) } context.findActivity().setPictureInPictureParams(builder.build()) } VideoPlayer(modifier = pipModifier)
अगले चरण
- Compose PiP को काम करते हुए देखने के सबसे सही तरीके जानने के लिए, Socialite ऐप्लिकेशन देखें.
- ज़्यादा जानकारी के लिए, PiP डिज़ाइन करने से जुड़े दिशा-निर्देश देखें.