youtube / yt-android-player

Apache License 2.0
130 stars 88 forks source link

youtube player gets crashed #17

Closed Shrikant-B closed 7 years ago

Shrikant-B commented 7 years ago

I'm trying to play video using YouTube Android Player API. Even though I have video id, my application gets crashed saying "Unfortunately, Youtube has Stopped." I'm getting videos using below api: https://www.googleapis.com/youtube/v3/search?key=AIzaSyC1pCnqKowxxmITL4YIwsuYi6GEeaOgEtY&channelId=UCMEmNnHT69aZuaOrE-dF6ug&part=snippet,id&order=date&maxResults=20

This is logcat:

E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: com.google.android.youtube.player, PID: 19410
                                                   java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String com.google.android.apps.youtube.core.utils.y.i()' on a null object reference
                                                       at com.google.android.apps.youtube.core.player.AdStatsMacros.a(SourceFile:185)
                                                       at com.google.android.apps.youtube.core.client.DefaultAdStatsClient.a(SourceFile:481)
                                                       at com.google.android.apps.youtube.core.client.DefaultAdStatsClient.a(SourceFile:467)
                                                       at com.google.android.apps.youtube.core.client.DefaultAdStatsClient.a(SourceFile:258)
                                                       at com.google.android.apps.youtube.core.player.StatsTracker.a(SourceFile:555)
                                                       at com.google.android.apps.youtube.core.player.Director.a(SourceFile:90)
                                                       at com.google.android.apps.youtube.core.player.ag.a(SourceFile:1562)
                                                       at com.google.android.apps.youtube.core.async.p.a(SourceFile:35)
                                                       at com.google.android.apps.youtube.core.async.bm.run(SourceFile:96)
                                                       at android.os.Handler.handleCallback(Handler.java:739)
                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                       at android.os.Looper.loop(Looper.java:135)
                                                       at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at java.lang.reflect.Method.invoke(Method.java:372)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Here is code for YoutubePlayerActivity:

public class YoutubePlayerActivity extends YouTubeFailureRecoveryActivity {
    private YouTubePlayerView mYouTubePlayerView;
    private Item mItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.video_detail);
        mYouTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player_view);
        mItem = getIntent().getParcelableExtra(ITEM);
        mYouTubePlayerView.initialize(API_KEY, this);
    }

    @Override
    protected YouTubePlayer.Provider getYouTubePlayerProvider() {
        return mYouTubePlayerView;
    }

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
        youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
        youTubePlayer.setFullscreen(true);
        youTubePlayer.setShowFullscreenButton(true);
        if (!wasRestored) {
            if(mItem.getId().getVideoId() != null) youTubePlayer.cueVideo(mItem.getId().getVideoId());
        }
    }
}

Here is code for YouTubeFailureRecoveryActivity :

public abstract class YouTubeFailureRecoveryActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
    private static final int RECOVERY_DIALOG_REQUEST = 1;

    @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
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == RECOVERY_DIALOG_REQUEST) {
            getYouTubePlayerProvider().initialize(API_KEY, this);
        }
    }

    protected abstract YouTubePlayer.Provider getYouTubePlayerProvider();
}

Please help, thanks.