问题:Youtube播放器在应用中最小化youtube播放器活动/片段时崩溃。
原因:Youtube播放器更新
代码
public class YouTubePlayerActivity extends YouTubeBaseActivity
implements YouTubePlayer.OnInitializedListener {
private static final int RECOVERY_DIALOG_REQUEST = 1;
private final String TAG = YouTubePlayerActivity.class.getSimpleName();
private YouTubePlayerView youTubeView;
private String videoId;
private YouTubePlayer youtubePlayer;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_youtube_player);
videoId = getIntent().getExtras().getString(Constants.BUNDLE_VIDEO_ID);
youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
youTubeView.initialize(Constants.YOUTUBE_API_KEY, this);
}
@Override public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult errorReason) {
Log.d(TAG, "on Initialization failure : " + errorReason); }
@Override public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
boolean wasRestored) {
youtubePlayer = player;
youtubePlayer.setFullscreen(true);
youtubePlayer.setShowFullscreenButton(false);
youtubePlayer.setFullscreenControlFlags(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_ORIENTATION
| YouTubePlayer.FULLSCREEN_FLAG_CONTROL_SYSTEM_UI);
if (!wasRestored) {
youtubePlayer.loadVideo(videoId);
} }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECOVERY_DIALOG_REQUEST) {
youTubeView.initialize(Constants.YOUTUBE_API_KEY, this);
} }
@Override protected void onPause() {
super.onPause();
if(youtubePlayer != null) {
youtubePlayer.pause();
} }
@Override protected void onDestroy() {
super.onDestroy();
if (youtubePlayer != null) {
youtubePlayer.release();
} } }