ServiceConnectionLeaked with youtube api

ServiceConnectionLeaked with youtube api

首先,我已经在此处的某些线程中阅读了这个问题,并且我知道可以修复将 getActivity() 替换为应用程序上下文。所有这些答案都是几年前的,现在我认为它不支持应用程序上下文,因为当我放置应用程序上下文时出现错误,并且在库的方法中我找不到任何方法可以放置应用上下文。

当我创建 YoutubeStandalonePlayer 时,我是这样做的:

Intent intent = YouTubeStandalonePlayer.createVideoIntent(getActivity(), getResources().getString(google_maps_key), mPublication.getYoutubeCode());

如果我尝试放置应用程序上下文,我会收到错误消息,因为我传递的是应用程序上下文而不是 activity,即方法正在等待的 属性。

 Intent intent = YouTubeStandalonePlayer.createVideoIntent(ApplicationConfig.getAppContext(), getResources().getString(google_maps_key), mPublication.getYoutubeCode());

那么,我的问题是...如何使用 YouTubeStandalonePlayer 解决 ServiceConnectionLeaked 问题:

android.app.ServiceConnectionLeaked: Activity com.buzinger.loycus.activity.HomeActivity has leaked ServiceConnection com.google.android.youtube.player.internal.r$e@cbfec60 that was originally bound here

提前致谢

试试我在这个网站上找到的这个解决方案(https://androidadagnitio.wordpress.com/2017/03/09/activity-has-leaked-serviceconnection-com-google-android-youtube-player-internal-re391c339-that-was-originally-bound-here-error-solution/)

您需要添加此行以避免 ServiceConnectionLeaked with youtube api。

 youTubeThumbnailLoader.release();

全部代码:

 @Override
    public void onBindViewHolder(final VideoInfoHolder holder,final int position) {

        holder.youTubeThumbnailView.initialize(DEVELOPER_KEY, new YouTubeThumbnailView.OnInitializedListener() {
            @Override
            public void onInitializationSuccess(YouTubeThumbnailView youTubeThumbnailView, final YouTubeThumbnailLoader youTubeThumbnailLoader) {

                youTubeThumbnailLoader.setVideo(videos.get(position));
          //here is the magic to solve the logcat error 
                youTubeThumbnailLoader.setOnThumbnailLoadedListener(new YouTubeThumbnailLoader.OnThumbnailLoadedListener() {
                    @Override
                    public void onThumbnailLoaded(YouTubeThumbnailView youTubeThumbnailView, String s) {
                        youTubeThumbnailView.setVisibility(View.VISIBLE);
                        holder.relativeLayoutOverYouTubeThumbnailView.setVisibility(View.VISIBLE);
                        youTubeThumbnailLoader.release();
                    }

                    @Override
                    public void onThumbnailError(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader.ErrorReason errorReason) {

                    }
                });
            }

            @Override
            public void onInitializationFailure(YouTubeThumbnailView youTubeThumbnailView, YouTubeInitializationResult youTubeInitializationResult) {
                //write something for failure
            }
        });
    }