Search code examples
androidyoutubeyoutube-apiandroid-youtube-api

ServiceConnectionLeaked with youtube api


First of all, I've read this problem in some threads here and I know that could be fix replacing the getActivity() with the application context. All this answers are from some years ago, and now I think that is not supportes the application context, because I get an error when I put the application context and in the methods of the library I can't find any method to can put the application context.

When I create tye YoutubeStandalonePlayer I'm doing this:

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

if I try to put the app context I get an error because I'm passing an app context and not the activity, that is the property that the method is waiting for.

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

Then, my question is... how can I fix the problem of ServiceConnectionLeaked using the YouTubeStandalonePlayer:

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

Thanks in advance


Solution

  • Try this solution that I've found in this website(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/)

    You need to add this line to avoid the ServiceConnectionLeaked with youtube api.

     youTubeThumbnailLoader.release();
    

    All the code:

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