PierfrancescoSoffritti / android-youtube-player

YouTube Player library for Android and Chromecast, stable and customizable.
https://pierfrancescosoffritti.github.io/android-youtube-player/
MIT License
3.44k stars 765 forks source link

androidx lifecycle observer incompability #235

Closed setzner closed 6 years ago

setzner commented 6 years ago

Hey, after plenty of unsuccessful attempts on using the official youtube api together with androidx, I was recommended to use this library- which seems a whole lot more comprehensive and supported. Unfortunately, I'm still stuck with the detail that lifecycle requires observer to be of androidx type.

build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'com.google.android.gms.oss-licenses-plugin'

android {
     compileSdkVersion 28
     defaultConfig {
              applicationId "com.moomoo.testapp"
              minSdkVersion 21
              targetSdkVersion 28
              versionCode 1
              versionName "1.0"
     }

     buildTypes {
              release {
                  minifyEnabled false
                  proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
              }
      }
 }

dependencies {
     def lifecycle_version = "1.1.1"
     implementation fileTree(include: ['*.jar'], dir: 'libs')
     implementation 'com.google.android.material:material:1.0.0'                  // Material design
     implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'                    // Graphs
     implementation 'com.pierfrancescosoffritti.androidyoutubeplayer:core:8.0.1'  // Youtube
     implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
}
public class SingleTestFragment extends Fragment  {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_single_test, container, false);

        YouTubePlayerView youtubePlayerView = rootView.findViewById(R.id.youtube_player_view);
        getLifecycle().addObserver(youtubePlayerView);  <---- issue with androidx and library
        youtubePlayerView.initialize(new YouTubePlayerInitListener() {
            @Override
            public void onInitSuccess(@NonNull final YouTubePlayer initializedYouTubePlayer) {
                initializedYouTubePlayer.addListener(new AbstractYouTubePlayerListener() {
                    @Override
                    public void onReady() {
                        String videoId = "6JYIGclVQdw";
                        initializedYouTubePlayer.loadVideo(videoId, 0);
                    }
                });
            }
        }, true);

        return rootView;
    }
}

I wouldnt mind doing the mentioned workaround, of manually releasing the player, if necessary - but I was also unable to figure that one out. Any suggestions?

PierfrancescoSoffritti commented 6 years ago

Hey, yes isn't using androidx yet. It will come with the next update.

For now, as you mentioned, releasing manually is the solution.

Just add this to your Activity/Fragment:

@Override
public void onStop() {
  super.onStop();
  youtubePlayer.pause(); // to stop the player when the activity/fragment is not visibile
}

@Override
public void onDestroy() {
    super.onDestroy();
    youtubePlayerView.release();
}
setzner commented 6 years ago

Thank you for the quick reply.

Unfortunately, it still cannot compile because of "error: cannot access LifecycleObserver class file for android.arch.lifecycle.LifecycleObserver not found" (no line number indicated). If I change my gradle build implementation from:

    implementation 'androidx.lifecycle:lifecycle-livedata:1.1.1'
    implementation 'androidx.lifecycle:lifecycle-viewmodel:1.1.1'
    implementation 'androidx.lifecycle:lifecycle-extensions:1.1.1'

to

    implementation 'android.arch.lifecycle:livedata:1.1.1'
    implementation 'android.arch.lifecycle:viewmodel:1.1.1'
    implementation 'android.arch.lifecycle:extensions:1.1.1'

The error becomes "Error: Program type already present: android.support.v4.os.ResultReceiver" (because of androidx). So my guess is that the library specifically wants "android.arch.lifecycle.LifecycleObserver". Any other suggestions? :)

PierfrancescoSoffritti commented 6 years ago

mmm you could try to import it directly

implementation "android.arch.lifecycle:runtime:$lifecycle_version"

if this doesn't work I can do some tests later today!

setzner commented 6 years ago

Thanks - It's getting closer! :) The application can compile for the first time since I started integrating youtube into it...buuut when I open the fragment and youtube starts loading, it crashes with error of


> Process: com.moo.testapp, PID: 9391
>     java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/content/ContextCompat;
>         at com.pierfrancescosoffritti.androidyoutubeplayer.ui.DefaultPlayerUIController.onStateChange(Unknown Source:45)
>         at com.pierfrancescosoffritti.androidyoutubeplayer.player.YouTubePlayerBridge$3.run(Unknown Source:28)
>         at android.os.Handler.handleCallback(Handler.java:789)
>         at android.os.Handler.dispatchMessage(Handler.java:98)
>         at android.os.Looper.loop(Looper.java:164)
>         at android.app.ActivityThread.main(ActivityThread.java:6944)
>         at java.lang.reflect.Method.invoke(Native Method)
>         at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
>         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
>      Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v4.content.ContextCompat"
PierfrancescoSoffritti commented 6 years ago

well, I assume you are using the androidx version of appcompat :/ while the library is expecting the old android.support version.

You probably have this implementation 'androidx.appcompat:appcompat:verion', but the library wants this implementation 'com.android.support:appcompat-v7:version'

setzner commented 6 years ago

Yup - I guess i'll have to look into alternatives for the time being. Thank you so much though! In this case, the issue comes from someone deciding that it would be smart for the project to use material design: implementation 'com.google.android.material:material:1.0.0' without reading up on its maturity :/

PierfrancescoSoffritti commented 6 years ago

You can use com.android.support:design instead of that, if you want to revert back from androidx.

I will introduce androidx in the next update, which will probably be in approximately a month from now.

setzner commented 6 years ago

I will consider switching, or if time permits it, wait another month :) Thank you for the help and for this great library! 👍

PierfrancescoSoffritti commented 6 years ago

:+1: if you end up using it, feel free to leave a star on Github :)

setzner commented 6 years ago

While searching for solution to a different problem, I got this one working 👍 So now I'm using it (and you got a star, and soon license mention of course).

The solution was to add the two below attributes to my gradle.properties file

android.useAndroidX=true
android.enableJetifier=true
PierfrancescoSoffritti commented 6 years ago

Ah nice! thank you for letting me know, this is going to be useful :)

PierfrancescoSoffritti commented 6 years ago

I've just updated the library to depend on androidx, new updated coming soon.

Mukeshkrjha commented 5 years ago

I'm facing the same issue. Tried everything described above but nothing is working. I'm using 'YouTubePlayerView' inside recyclerview. Please help.

Using implementation 'com.pierfrancescosoffritti.androidyoutubeplayer:core:9.0.1'

Thanks

PierfrancescoSoffritti commented 5 years ago

Are you using androidx or the support libraries? Do you have appcompat in your dependencies?

Mukeshkrjha commented 5 years ago

Using implementation 'com.android.support:appcompat-v7:28.0.0'

Mukeshkrjha commented 5 years ago

Below error I'm getting while running project in android studio

Information:Gradle tasks [:app:assembleDebug] Warning:unknown enum constant Event.ON_DESTROY Warning:unknown enum constant Event.ON_STOP Warning:unknown enum constant Event.ON_DESTROY Warning:unknown enum constant Event.ON_STOP Warning:unknown enum constant Event.ON_DESTROY Warning:unknown enum constant Event.ON_STOP Warning:unknown enum constant Event.ON_DESTROY Warning:unknown enum constant Event.ON_STOP /mukesh/MukeshJha/Training/outsource/dropship-app/app/src/main/java/io/bigly/seller/help/HelpVideosAdapter.java Error:(47, 24) error: cannot access LifecycleObserver class file for androidx.lifecycle.LifecycleObserver not found Information:BUILD FAILED in 8s Information:1 error Information:8 warnings Information:See complete output in console

PierfrancescoSoffritti commented 5 years ago

Starting from version 9.0.0 of the library you need to be using androidx. If you don't want to use androidx you need to use version 8.0.1 of the library.

Mukeshkrjha commented 5 years ago

What dependency I need to add in app.gradle. please share.

PierfrancescoSoffritti commented 5 years ago

@Mukeshkrjha often one quick Google search is all it takes.

good luck!

Mukeshkrjha commented 5 years ago

Thanks. Meanwhile I tried with version 8.0.1. It's working. I'll check with 9.0.1 later.

mochadwi commented 4 years ago

While searching for solution to a different problem, I got this one working 👍 So now I'm using it (and you got a star, and soon license mention of course).

The solution was to add the two below attributes to my gradle.properties file

android.useAndroidX=true
android.enableJetifier=true

Do you mind sharing your dependencies for this fix? I cannot find a ways to fix this.

   implementation("com.github.PierfrancescoSoffritti:AndroidYouTubePlayer:8.0.1") {
        exclude(group = "com.android.support")
    }

Should I add everything here:

//    implementation("androidx.lifecycle:lifecycle-extensions:2.2.0")
    implementation("android.arch.lifecycle:livedata:1.1.1")
    implementation("android.arch.lifecycle:viewmodel:1.1.1")
    implementation("android.arch.lifecycle:extensions:1.1.1")
    implementation("android.arch.lifecycle:runtime:1.1.1")

I know that v9 & above support androidx, unfortunately my codebase still using support library

@PierfrancescoSoffritti @setzner

EDITED:

This approach fixed my issue: https://github.com/facebook/facebook-android-sdk/issues/591#issuecomment-619435280

51001624 commented 1 year ago

Hello, How can I play the next video on the youtube list? Thank you!

PierfrancescoSoffritti commented 1 year ago

@51001624 Please use or open appropriate issue for this.

51001624 commented 1 year ago

@PierfrancescoSoffritti OMG, thank you Sir. After soemtimes strugling to this, i finally can play the next song as I wished, thank you for all the good works and distribution.