Android:如何正确初始化活动中的YouTube Player? [英] Android: How to properly initialize YouTube Player inside activity?

查看:226
本文介绍了Android:如何正确初始化活动中的YouTube Player?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在活动中启动YouTube播放器。但是有时我会不断收到此异常:

I am trying to initiate an YouTube player inside an activity. However from time to time I keep getting this exception:

Fatal Exception: java.lang.IllegalStateException YouTubeServiceEntity not initialized

这是我在活动中尝试初始化youtube播放器的方法。 (在OnCreate()内部完成)

Here is how I try to initialize the youtube player in my activity. (this is done inside 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");
    }

最奇怪的是,这个问题并不持久。它只是不时发生,我不知道为什么。你能告诉我是什么原因造成的吗?

The weirdest thing is that this issue is not persistent. It just happens from time to time and I have no idea why. Can you tell me what could be causing this?

这里也是logcat:

Here is also the 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) 


推荐答案

我现在也正在我的应用程序中遇到此错误。当用户退出包含活动的Youtube播放器片段而未正确初始化时,就会发生问题。

I am also facing this bug right now in my app. The problem happens when the user quit the Youtube player fragment containing activity without being it properly initialized. It is quite random and not easy to reproduce.

我只是使用try-catch来避免它,幸运的是,它尚未被复制。

I am simply using try-catch to avoid it and luckily, it has not been reproduced yet.

try {
    mYouTubePlayerFragment.initialize(DEVELOPER_KEY, this);
} catch(IllegalStateException w){}

再次可能这不是确定的解决方案。

Again It may not be the sure solution.

一种解决方案可能是检查 OnBackPressed()您的视频播放器是否已初始化。

One solution may be to check in OnBackPressed() whether the you-tube player has been initialized.

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; 
}

尝试猴子以更频繁地重现它。

Try out monkey to reproduce it more often.

adb shell monkey -p com.packagename -v 500

这里是链接来发布跟踪器对于此错误。

Here is link to issue tracker for this bug.

这篇关于Android:如何正确初始化活动中的YouTube Player?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆