我正在尝试在活动中发起YouTube播放器。但是我不时会遇到这个例外:
Fatal Exception: java.lang.IllegalStateException YouTubeServiceEntity not initialized
以下是我尝试在我的活动中初始化youtube播放器的方法。 (这是在OnCreate()中完成的)
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 Boolean) {
if(ad==null || ad.getVideo_urls() == null)
return;
if (!Boolean)
{
try {
if (ad.getVideo_urls() != null && ad.getVideo_urls().length() > 0) {
String url = ad.getVideo_urls().getString(0);
if (url.contains("youtube")) {
String id = ad.getVideo_urls().getString(0).split("embed/")[1];
youTubeView.setVisibility(View.VISIBLE);
MyYouTubePlayer = youTubePlayer;
MyYouTubePlayer.cueVideo(id);
}
} 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();
}
}
}
catch (JSONException e) {
youTubePlayer.release();
e.printStackTrace();
}
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
youTubeView.removeAllViews();
}
});
} catch(Exception e){
e.printStackTrace();
Log.e("Youtube", "error initializing youtube");
}
最奇怪的是这个问题并不持久。它只是不时发生,我不明白为什么。你能告诉我是什么原因引起的吗?
这里也是logcat:
java.lang.IllegalStateException: YouTubeServiceEntity not initialized
at android.os.Parcel.readException(Parcel.java:1433)
at android.os.Parcel.readException(Parcel.java:1379)
at com.google.android.youtube.player.internal.l$a$a.a()
at com.google.android.youtube.player.internal.o.a()
at com.google.android.youtube.player.internal.ad.a()
at com.google.android.youtube.player.YouTubePlayerView.a()
at com.google.android.youtube.player.YouTubePlayerView$1.a()
at com.google.android.youtube.player.internal.r.g()
at com.google.android.youtube.player.internal.r$c.a()
at com.google.android.youtube.player.internal.r$b.a()
at com.google.android.youtube.player.internal.r$a.handleMessage()
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4960)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(NativeStart.java)
答案 0 :(得分:2)
我现在也在我的应用中面对这个错误。当用户退出包含活动的Youtube播放器片段而没有正确初始化时,会出现问题。它非常随机,不易复制。
我只是使用try-catch来避免它,幸运的是,它尚未被复制。
try {
mYouTubePlayerFragment.initialize(DEVELOPER_KEY, this);
} catch(IllegalStateException w){}
再次,它可能不是确定的解决方案。
一种解决方案可能是检查OnBackPressed()
你的游戏玩家是否已被初始化。
isInitializationComplete = false
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
isInitializationComplete = true;
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult youTubeInitializationResult) {
isInitializationComplete = true;
}
@Override
public void onBackPressed() {
if(!isInitializationComplete) return;
}
尝试使用猴子来更频繁地重现它。
adb shell monkey -p com.packagename -v 500
这是link发布此错误的跟踪器。
答案 1 :(得分:1)
我也有这个错误,我通过创建一个扩展YouTubePlayerSupportFragment
的自定义类来解决它,也许我的代码对你有所帮助,这对我来说很好。
我像这样初始化我的youtube播放器:
private String currentVideoID = "<your video id>";
private YouTubePlayer activePlayer;
private void init() {
initialize(ConstantsRepository.GOOGLE_API_SERVER_KEY, new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationFailure(YouTubePlayer.Provider arg0, YouTubeInitializationResult arg1) {
// youtube is not installed
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
activePlayer = player;
activePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
if (!wasRestored) {
activePlayer.loadVideo(getArguments().getString("url"), 0);
}
}
});
}
在您的活动中,在layout.xml中提供youtube片段,并在onCreate中执行以下替换:
YTPlayerFragment myFragment = YTPlayerFragment.newInstance("oVkK3X9zMyI");
getFragmentManager().beginTransaction().replace(R.id.youtube_container, myFragment).commit();
这个帖子很相似,帮助了我:
答案 2 :(得分:1)
经过一番挖掘后,我发现了我的问题。问题在于我使用的youtube库。它已经过时了HERE一切都很好:)
答案 3 :(得分:1)
尝试这种方式
此处活动从YouTubeBaseActivity扩展,该活动将出现在 YouTubeAndroidPlayerApi.jar 中。此活动还包含一些初始化侦听器方法,以了解youtube播放器的状态。和follow this example for more help
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;
import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayer.PlayerStyle;
import com.google.android.youtube.player.YouTubePlayerView;
public class MainActivity extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener {
private static final int RECOVERY_DIALOG_REQUEST = 1;
// YouTube player view
private YouTubePlayerView youTubeView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
// Initializing video player with developer key
youTubeView.initialize(Config.DEVELOPER_KEY, this);
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
} else {
String errorMessage = String.format(
getString(R.string.error_player), errorReason.toString());
Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
}
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider,
YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
// loadVideo() will auto play video
// Use cueVideo() method, if you don't want to play it automatically
player.loadVideo(Config.YOUTUBE_VIDEO_CODE);
// Hiding player controls
player.setPlayerStyle(PlayerStyle.CHROMELESS);
}
}
@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(Config.DEVELOPER_KEY, this);
}
}
private YouTubePlayer.Provider getYouTubePlayerProvider() {
return (YouTubePlayerView) findViewById(R.id.youtube_view);
}
}
答案 4 :(得分:0)
这对我有用
请从here
下载.Jar在你的垃圾箱文件夹中添加YouTubeAndroidPlayerApi.jar。
播放Youtube视频的及以下活动
if (url.contains(Constants.YOUTUBE_URL)) {
final String key[] = url.split("v=");
if (key.length > 1) {
final Intent intent = new Intent(getActivity(), YouTubeActivity.class);
intent.putExtra(Constants.KEY_VIDEO_KEY, key[1]);
startActivity(intent);
getActivity().overridePendingTransition(R.anim.anim_zoom_in, R.anim.anim_zoom_out);
} else {
Utils.showSnackbarNonSticky(mRecyclerView, "URL is not valid", true, mContext);
}
}
以下活动是播放视频
public class YouTubeActivity extends YouTubeBaseActivity
implements YouTubePlayer.OnInitializedListener, View.OnClickListener {
private static final int RECOVERY_REQUEST = 1;
private YouTubePlayerView youTubeView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_youtube);
initView();
}
//this method is for the initialization of the view component
private void initView() {
final LinearLayout llParent = (LinearLayout) findViewById(R.id.activity_you_tube_parent_layout);
youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
youTubeView.initialize(Constants.YOUTUBE_DEVELOPER_KEY, this);
final TextView tvClose = (TextView) findViewById(R.id.activity_you_tube_tv_close);
tvClose.setOnClickListener(this);
llParent.setOnTouchListener(new OnSwipeTouchListener(this) {
public void onSwipeTop() {
}
public void onSwipeRight() {
}
public void onSwipeLeft() {
}
public void onSwipeBottom() {
finish();
overridePendingTransition(R.anim.anim_zoom_in, R.anim.anim_zoom_out);
}
});
final WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
final Window window = this.getWindow();
lp.copyFrom(window.getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(lp);
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
player.loadVideo(getIntent().getStringExtra(Constants.KEY_VIDEO_KEY));
player.setShowFullscreenButton(false);
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this, RECOVERY_REQUEST).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECOVERY_REQUEST) {
getYouTubePlayerProvider().initialize(Constants.YOUTUBE_DEVELOPER_KEY, this);
}
}
protected YouTubePlayer.Provider getYouTubePlayerProvider() {
return youTubeView;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.activity_you_tube_tv_close:
finish();
this.overridePendingTransition(R.anim.anim_zoom_in, R.anim.anim_zoom_out);
break;
}
}
@Override
public void onBackPressed() {
final boolean backEnable = false;
if (backEnable) {
super.onBackPressed();
}
}
}
XML在
之下<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_you_tube_parent_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBlackOpacity60"
android:gravity="center"
android:orientation="vertical">
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtube_view"
android:layout_width="match_parent"
android:layout_height="@dimen/_250Cdp" />
<TextView
android:id="@+id/activity_you_tube_tv_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_100dp"
android:padding="@dimen/_5Ddp"
android:text="@string/close"
android:textColor="@color/white"
android:textSize="@dimen/_18Csp"
app:font_name="@string/font_roboto_light" />