嗨,YouTubeAndroidPlayerAPI中面临的问题,这是我面临的错误
W/YouTubeAndroidPlayerAPI: Forcefully created overlay:acqm@cada64
helper:Lazy@241a4cd view:null status: ....... {...}
注意-如果用户输入
FirstActivity
,视频工作正常,则我有两个 Activity 类别。如果用户在SecondActivity中将 Activity 从FirstActivity
更改为SecondActivity
,视频也可以正常工作,如果用户使用系统后退按钮导航到FirstActivity,则视频无法连续播放,但视频无法播放。如果用户单击这样的按钮,则加载相同的 Activity (在我的情况下,FirstActivity和SecondActivity是相同的,仅出于站立目的解释了两个 Activity )
Intent intent = new Intent(YoutubeVideoPlay.this, YoutubeVideoPlay.class);
startActivity(intent);
这是我正在使用的代码示例
FirstActivity.java
public class FirstActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
public static final String DEVELOPER_KEY = "xxxxxxxxxxxxxxxxxxx";
private static final int RECOVERY_DIALOG_REQUEST = 1;
private String url = "xxxxxxxxxxxxxxxxxxxx";
private String VIDEO_ID;
private YouTubePlayerFragment myYouTubePlayerFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_youtube);
myYouTubePlayerFragment = (YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.youtubeplayerfragment);
myYouTubePlayerFragment.initialize(DEVELOPER_KEY, this);
VIDEO_ID = getYoutubeVideoId(url);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(FirstActivity .this, FirstActivity .class);
startActivity(intent);
}
});
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
} else {
String errorMessage = String.format("There was an error initializing the YouTubePlayer (%1$s)",
errorReason.toString());
Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
}
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
player.cueVideo(VIDEO_ID);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECOVERY_DIALOG_REQUEST) {
// Retry initialization if user performed a recovery action
getYouTubePlayerProvider().initialize(DEVELOPER_KEY, this);
}
}
protected YouTubePlayer.Provider getYouTubePlayerProvider() {
return (YouTubePlayerView) findViewById(R.id.youtubeplayerfragment);
}
public static String getYoutubeVideoId(String youtubeUrl) {
String video_id = "";
if (youtubeUrl != null && youtubeUrl.trim().length() > 0 && youtubeUrl.startsWith("http")) {
String expression = "^.*((youtu.be" + "\\/)" + "|(v\\/)|(\\/u\\/w\\/)|(embed\\/)|(watch\\?))\\??v?=?([^#\\&\\?]*).*"; // var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
CharSequence input = youtubeUrl;
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input);
if (matcher.matches()) {
String groupIndex1 = matcher.group(7);
if (groupIndex1 != null && groupIndex1.length() == 11)
video_id = groupIndex1;
}
}
return video_id;
}
}
最佳答案
是的,YouTube主单全局播放器如果您启动新播放器,以前的播放器将自动释放。
因此,您需要在恢复 Activity 时再次重新启动YouTube播放器