2

YouTube 视频在普通屏幕上播放。但是,当我切换到画中画模式时。它停止播放,没有错误。

不确定以下代码段有什么问题:

    class YouTubeVideoActivity : YouTubeBaseActivity(),
        YouTubePlayer.OnInitializedListener {

    var isInPipMode = true

    val TAG = "YouTubeVideoActivity"
    private var player: YouTubePlayer? = null

    private val youTubeView: YouTubePlayerView by lazy {
        findViewById<YouTubePlayerView>(R.id.youtube_view)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_you_tube_video)
        youTubeView.initialize(API_KEY, this)
    }

    override fun onUserLeaveHint() {
        enterInPictureAndPictureMode()
    }

    override fun onPause() {
        super.onPause()

        Log.d(TAG, "OnPause:$isInPipMode")
    }

    override fun onPictureInPictureModeChanged(isInPictureInPictureMode: Boolean, newConfig: Configuration?) {
        super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig)
        isInPipMode = isInPictureInPictureMode


    }

    override fun onInitializationSuccess(p0: YouTubePlayer.Provider?, player: YouTubePlayer?, wasRestored: Boolean) {
        if (player == null) return

        this.player = player

        player.setFullscreen(false)
        player.setPlayerStateChangeListener(youTubeStateChangeListener)
        player.setPlaybackEventListener(youTubePlayBackListener)

        if (!wasRestored) {
            player.loadVideo("pr-4GbR4DpQ")
            // player.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT)
        }
    }

    override fun onInitializationFailure(provider: YouTubePlayer.Provider?, errorReason: YouTubeInitializationResult?) {
        if (errorReason == null || provider == null) return

        if (errorReason.isUserRecoverableError) {
            errorReason.getErrorDialog(this, 1).show()
        } else {
            val errorMessage = errorReason.toString()
            Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show()
        }
    }


    private fun getYouTubePlayerProvider(): YouTubePlayer.Provider? {
        return findViewById<View>(R.id.youtube_view) as YouTubePlayerView
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (requestCode == 1) {
            // Retry initialization if user performed a recovery action
            getYouTubePlayerProvider()?.initialize(API_KEY, this)
        }
    }

    private fun enterInPictureAndPictureMode() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val supportsPIP = packageManager.hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE)

            if (supportsPIP) {
                val ratio = Rational(200, 110)
                val params = PictureInPictureParams.Builder()
                        .setAspectRatio(ratio)
                        .build()

                enterPictureInPictureMode(params)
            }
        }
    }
}

如果有人知道 WhatsApp 如何做到这一点或如何正确实施。

提前致谢!

4

0 回答 0