Search code examples
android-studioandroid-youtube-api

Unable to get a YouTube video to play with Android Studio


I know that if you want to play a YouTube video, your activity has to extend YouTubeBaseActivity and implement YouTubePlayer.OnInitialzedListener, but what if those aren't being recognized in Android Studio?

I've gotten it to work with Eclipse multiple times, but this is my first app in Android Studio and I'm not finding a lot of resources to help me know what to do differently.

public class VideoPlayerActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener{

This is the code in onCreate I used in Eclipse:

ytpv = (YouTubePlayerView) findViewById(R.id.youtubeplayer);
ytpv.initialize(DEVELOPER_KEY, this);

Also this:

@Override
    public void onInitializationFailure(Provider arg0, YouTubeInitializationResult arg1) {
    Toast.makeText(this, "Initialization Fail", Toast.LENGTH_LONG).show();
}

And the clincher where filename was just the youtube id:

@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasrestored) {
    yt_player = player;

    Bundle info = getIntent().getExtras();
    if(info == null){
        Log.d(TAG, "No extras!");
        return;
    }

    fileName = info.getString("filename");

    if(fileName.equalsIgnoreCase("null")){
        availability.setVisibility(View.VISIBLE);
        ytpv.setVisibility(View.GONE);
    }else{
        Log.d(TAG, "Play video!");
        yt_player.loadVideo(fileName);
    }
}

Layout file looks like this:

<com.google.android.youtube.player.YouTubePlayerView
    android:id="@+id/youtubeplayer"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/eturl"
    >
</com.google.android.youtube.player.YouTubePlayerView>

Any tips/advice/critique will be warmly welcomed.


Solution

  • I found the answer on this Google product forums which helped to solve my problem of the video not playing (see my comment on my answer). :)

    SOLUTION: Give ONLY the YouTube ID and not the entire YouTube URL to the loadVideo() function.