Skip to content

How to prevent PIP mode from controlling the MediaLibraryService #2808

@omkarquest

Description

@omkarquest

How to prevent PIP mode from controlling the MediaLibraryService

I have a audio player which uses a service and a VideoPlayer which is in a activity.

The codes used are from the DemoPlaybackService and DemoMediaLibrarySessionCallback from the sessions demo app for the audio service and PlayerActivity from the main app for the video player.

These codes work perfectly when used Separately.

I am trying to implement the Picture-in-picture mode, when I minimize the app to PIP mode the video player looses its control and the audio player takes over. i.e if in PIP mode we click the play/pause /next/previous buttons they play the songs which are from the service instead of communicating with the videoplayer.
Also even though we have override fun onPictureInPictureModeChanged the playerview doesnot show its controller when they are back in full mode.

I understand this is an issue related to mediacontroller and how it is connected int he DemoMediaLibrarySessionCallback

For Pip mode I have used this code in the PlayerActivity

 private fun startPictureInPicture(): Boolean {
        
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            enterPictureInPictureMode(
                PictureInPictureParams.Builder()
                    .setAspectRatio(
                        Rational(
                            16,
                            9
                        )
                    ) 
                    .build()

            )

           
            return true
        }
        else {
            Toast.makeText(
                this,
                getString(R.string.not_supported),
                Toast.LENGTH_SHORT
            ).show()
             
        }
        return false
    }


override fun onPictureInPictureModeChanged(
        isInPictureInPictureMode: Boolean,
        newConfig: Configuration,
    ) {
        super.onPictureInPictureModeChanged(isInPictureInPictureMode,
            newConfig
        )
        isCrossChecked = isInPictureInPictureMode
        if (isInPictureInPictureMode) {
           

            playerView!!.hideController()
        }
        else {
            
            playerView!!.showController()
        }
    }

and the mediacontroller connection is handled using the same code from DemoMediaLibrarySessionCallback

@OptIn(UnstableApi::class) // ConnectionResult.AcceptedResultBuilder
  override fun onConnect(
    session: MediaSession,
    controller: MediaSession.ControllerInfo,
  ): MediaSession.ConnectionResult {
    if (controller.isTrusted) {
      // Provide full information and specify media button preferences.
      val customButton = commandButtons[if (session.player.shuffleModeEnabled) 1 else 0]
      return MediaSession.ConnectionResult.AcceptedResultBuilder(session)
        .setAvailableSessionCommands(sessionCommandsWithCustomCommands)
        .setMediaButtonPreferences(ImmutableList.of(customButton))
        .build()
    } else {
      // Restricted read-only view on currently playing item only.
      return MediaSession.ConnectionResult.accept(
        SessionCommands.EMPTY,
        restrictedAccessPlayerCommands,
      )
    }
  }

The question, is how to handle the controller to ensure videoplayer doesnot loose its control when it is in PIP mode and starts communicating with the audioPlayer.

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions