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



我试图在我的应用程序中集成Youtube播放器api。但当我切换到全屏时,会出现几个问题:

  1. 视频从头开始重新加载
  2. 在全屏模式下按下后退按钮,应用程序将被迫进入横向

公共类YouTubeVideoElement扩展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();
        }
    }
}

请尝试在您的AndroidManifest文件中更新此文件:

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

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

来自官方安卓指南。

相关内容

  • 没有找到相关文章

最新更新