gpt4 book ai didi

android - YouTube Player Api 重新加载视频从开始切换到全屏

转载 作者:太空狗 更新时间:2023-10-29 15:45:31 26 4
gpt4 key购买 nike

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

  1. 视频从头重新加载。
  2. 在全屏模式下按下后退按钮时,应用会强制进入横向模式。

公共(public)类 YouTubeVideoElement 扩展元素实现 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">

Lists configuration changes that the activity will handle itself. When a configuration change occurs at runtime, the activity is shut down and restarted by default, but declaring a configuration with this attribute will prevent the activity from being restarted. Instead, the activity remains running and its onConfigurationChanged() method is called.

来自official android guidelines .

关于android - YouTube Player Api 重新加载视频从开始切换到全屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34722711/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com