活动从后台恢复后,以编程方式播放/暂停YoutubePlayer

时间:2016-12-02 15:36:33

标签: android android-youtube-api

我的应用程序有活动A用于播放Youtube视频(使用YoutubePlayerFragment)此活动A有一个自己的按钮B来控制播放。当活动加载并正在播放视频时,我可以使用B按钮来控制播放。然后我按Home键。当我从最近的任务菜单恢复活动A时,按钮B不再起作用。我只能从自己的播放按钮(YoutubePlayerFragment内)播放视频。 目前我在调用onInitializationSuccess时存储YoutubePlayer实例:

private val onInitializedListener = object : YouTubePlayer.OnInitializedListener {
    override fun onInitializationSuccess(provider: YouTubePlayer.Provider, youTubePlayer: YouTubePlayer, wasRestored: Boolean) {
        Log.d(DEBUG_TAG, "YoutubePlayer - onInitializationSuccess")
        if (!wasRestored) {
            mYoutubePlayer = youTubePlayer
            mYoutubePlayer!!.setPlayerStateChangeListener(playerStateChangeListener)
            mYoutubePlayer!!.setPlaybackEventListener(mPlaybackEventListener)
            mYoutubePlayer!!.setShowFullscreenButton(false)
            mYoutubePlayer!!.loadVideo(mCurrentVideoId)
        } else {
            Log.d(DEBUG_TAG, "Restored from a previously saved state")
        }
    }

    override fun onInitializationFailure(provider: YouTubePlayer.Provider, youTubeInitializationResult: YouTubeInitializationResult) {
        if (youTubeInitializationResult.isUserRecoverableError) {
            youTubeInitializationResult.getErrorDialog(this@KActivityPlayVideo, 1).show()
        } else {
            Toast.makeText(this@KActivityPlayVideo,
                    "Failed to initialize video, please try again!",
                    Toast.LENGTH_LONG).show()
        }
    }
}

按钮B使用此YoutubePlayer实例根据请求调用播放/暂停。但是当活动A进入后台并恢复时,这不再起作用。谁能告诉我这里有什么问题?非常感谢你。

0 个答案:

没有答案