2016-12-02 277 views
4

我的應用程序具有播放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進入後臺並恢復時,這不再起作用。有誰能告訴我這裏有什麼問題嗎?非常感謝你。

+1

嗨!我在同一個問題上苦苦掙扎,您是否找到了解決方案? – thunder413

回答

-1

我希望這會幫助你。

private final class MyPlaybackEventListener implements YouTubePlayer.PlaybackEventListener { 

    @Override 
    public void onPlaying() { 
     // Called when playback starts, either due to user action or call to play(). 

    } 

    @Override 
    public void onPaused() { 
     // Called when playback is paused, either due to user action or call to pause(). 

    } 

    @Override 
    public void onStopped() { 
     // Called when playback stops for a reason other than being paused. 

    } 

    @Override 
    public void onBuffering(boolean b) { 
     // Called when buffering starts or ends. 
    } 

    @Override 
    public void onSeekTo(int i) { 
     // Called when a jump in playback position occurs, either 
     // due to user scrubbing or call to seekRelativeMillis() or seekToMillis() 
    } 
} 
+0

我也註冊PlaybackEventListener。活動恢復後,將自動調用「onStopped」。根據文檔,這是「由於暫停以外的原因(例如視頻結束或播放錯誤)而停止播放時調用。」我無法弄清楚這裏有什麼問題。順便說一句,添加這將不會解決任何事情,因爲我已經註冊了這個監聽器。 –