YouTube播放器Api从切换到全屏时开始重新加载视频

时间:2016-01-11 13:25:36

标签: android youtube-api android-youtube-api

我试图将Youtube播放器api集成到我的应用中。但是当我切换到全屏时会出现一些问题:

  1. 视频从头开始重新加载。
  2. 在全屏模式下按后退按钮时,应用程序将被强制为横向显示。
  3. 公共类YouTubeVideoElement extends Element实现了YouTubePlayer.OnInitializedListener {

    private final String DEVELOPER_KEY = "MY_KEY";
    private static final int RECOVERY_DIALOG_REQUEST = 1;
    
        private RelativeLayout.LayoutParams layoutParams;
    
        private YouTubePlayerFragment playerFragment;
        private View playerView;
        private ViewGroup viewFrame;
        public State state;
        private Dialog errorDialog;
        private Context context;
    
        public YouTubeVideoElement(Context context) {
            this.context = context;
    
        }
    
    
        @Override
        public String getType() {
            return null;
        }
    
        @Override
        public ViewGroup getView() {
            return viewFrame;
        }
    
        @Override
        public ViewGroup populateView(Context context, JsonObject data, Map<String, String> style) {
    
            viewFrame = new FrameLayout(context);
            playerView = new FrameLayout(context);
            playerView.setId(R.id.player_view);
            playerView.setVisibility(View.INVISIBLE);
            viewFrame.addView(playerView, -1, -1);
            playerView.setVisibility(View.VISIBLE);
            playerFragment = YouTubePlayerFragment.newInstance();
            playerFragment.initialize(DEVELOPER_KEY, this);
            ((Activity) context).getFragmentManager().beginTransaction().add(R.id.player_view, playerFragment).commit();
            return viewFrame;
        }
    
        public RelativeLayout.LayoutParams getLayoutParams() {
            return layoutParams;
        }
    
        public YouTubeVideoElement setLayoutParams(RelativeLayout.LayoutParams layoutParams) {
            this.layoutParams = layoutParams;
            return this;
        }
    
        @Override
        public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
    
            Log.d("xxx", "onInitSuccess YTP");
            if (!wasRestored) {
                player.cueVideo("nCgQDjiotG0");
            }
        }
    
    
        @Override
        public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult errorReason) {
    
            Log.d("xxx", "onInitFailure YTP");
            if (errorReason.isUserRecoverableError()) {
                errorReason.getErrorDialog((Activity) context, RECOVERY_DIALOG_REQUEST).show();
            } else {
                //String errorMessage = String.format(getString(R.string.error_player), errorReason.toString());
                Toast.makeText(context, "error", Toast.LENGTH_LONG).show();
            }
        }
    }
    

1 个答案:

答案 0 :(得分:9)

请尝试在AndroidManifest文件中更新:

 <activity
  android:configChanges="keyboardHidden|orientation|screenSize"
  android:name="com.example.yourClassThatHandlesTheYoutubePlayer"> 
  

列出活动将自行处理的配置更改。什么时候   在运行时发生配置更改,活动将关闭   默认情况下重新启动,但使用此声明配置   属性将阻止活动重新启动。相反,   活动仍在运行,其onConfigurationChanged()方法是   调用。

来自official android guidelines