Android:在全屏播放视频时播放视频是真实的

时间:2015-10-05 12:33:39

标签: android video youtube android-fullscreen

我的活动里面有一个管玩家。但是我遇到了视频全屏选项的一些问题。

当我第一次开始活动时,会显示youtubeview并正确初始化我可以看到视频播放并使用它进入全屏。但是,当我按下按钮退出全屏模式/旋转手机时,整个活动正在重新加载,并且活动中缺少youtubeview。以下是我初始化youtube播放器/视图的方法:

   try {
        final YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);

        youTubeView.initialize("KEY", new YouTubePlayer.OnInitializedListener() {
            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean VidBool) {
                if(ad==null || ad.getVideo_urls() == null)
                    return;

                if (!VidBool)
                {
                    try {
                        if (ad.getVideo_urls() != null && ad.getVideo_urls().length() > 0) {
                            String url = ad.getVideo_urls().getString(0);
                            if (url.contains("youtube")){                                    
                                VideoID = "kQsN-pvokrw";
                                youTubeView.setVisibility(View.VISIBLE);
                                MyYouTubePlayer = youTubePlayer;
                                MyYouTubePlayer.cueVideo(VideoID);
                                MyYouTubePlayer.setOnFullscreenListener(new YouTubePlayer.OnFullscreenListener() {
                                    @Override
                                    public void onFullscreen(boolean b) {
                                        fullScreen = true;
                                    }
                                });
                            }
                        } else {
                            youTubeView.setVisibility(View.GONE);
                            Log.i(Constants.getTag(), "Video not found");
                            //Making sure the MyYouTubePlayer is null and if not is being released
                            if(MyYouTubePlayer != null)
                            {
                                MyYouTubePlayer.release();
                                MyYouTubePlayer.setFullscreenControlFlags(-1);
                            }
                        }
                    }
                    catch (JSONException e) {
                        youTubePlayer.release();
                        e.printStackTrace();
                    }
                }

            }

            @Override
            public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
                youTubeView.removeAllViews();
            }
        });



        //Catching A dead object exception
    } catch(Exception e){
        e.printStackTrace();
        Log.e("Youtube", "error initializing youtube");
    }

1 个答案:

答案 0 :(得分:2)

如果您不希望在方向更改后重新创建Activty,则必须在Manifest(活动标记内)添加下一行:

android:configChanges="orientation|screenSize"

但是,如果您想支持方向更改,则应该覆盖该Activity的 onConfigurationChanged 方法。

@Override
public void onConfigurationChanged(Configuration newConfig) {}

进一步阅读:http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange