我的應用程序具有播放Youtube視頻的活動A(使用YoutubePlayerFragment)此活動A有一個自己的按鈕B來控制播放。當活動加載並正在播放視頻時,我可以使用B按鈕來控制播放。然後我按下主頁按鈕。當我從最近的任務菜單中恢復活動A時,按鈕B不再工作。我只能播放自己的播放按鈕(位於YoutubePlayerFragment內)的視頻。 目前我儲存YoutubePlayer實例時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([email protected], 1).show()
} else {
Toast.makeText([email protected],
"Failed to initialize video, please try again!",
Toast.LENGTH_LONG).show()
}
}
}
按鈕B已經使用這個YoutubePlayer實例調用播放/根據請求暫停。但是當活動A進入後臺並恢復時,這不再起作用。有誰能告訴我這裏有什麼問題嗎?非常感謝你。
嗨!我在同一個問題上苦苦掙扎,您是否找到了解決方案? – thunder413