Monday 16 February 2015

You Tube Player Activity

YouTubePlayerActivity

Simply pass a url to play youtube video on new activity. It supports screen orientation, media volume control and etc.

Set Up AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />

<activity
    android:name="com.thefinestartist.ytpa.YouTubePlayerActivity"
    android:configChanges="keyboardHidden|orientation|screenSize"
    android:screenOrientation="sensor"
    android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" />

<meta-data
    android:name="com.thefinestartist.ytpa.YouTubePlayerActivity.ApiKey"
    android:value="your_google_api_key" />

Usage

Intent intent = new Intent(MainActivity.this, YouTubePlayerActivity.class);

// Youtube video ID or Url (Required)
intent.putExtra(YouTubePlayerActivity.EXTRA_VIDEO_ID, "iS1g8G_njx8");
// These kind of url can be parsed!!
// https://youtu.be/iS1g8G_njx8
// https://www.youtube.com/watch?v=iS1g8G_njx8
// https://www.youtube.com/watch?v=iS1g8G_njx8&vq=hd1080
intent.putExtra(YouTubePlayerActivity.EXTRA_VIDEO_URL, "https://youtu.be/iS1g8G_njx8");

// Show audio interface when user adjust volume
// true for default
intent.putExtra(YouTubePlayerActivity.EXTRA_SHOW_AUDIO_UI, true);

// If the video is not playable, use Youtube app or Internet Browser to play it
// true for default
intent.putExtra(YouTubePlayerActivity.EXTRA_HANDLE_ERROR, true);

// Animation when closing youtubeplayeractivity (none for default)
intent.putExtra(YouTubePlayerActivity.EXTRA_ANIM_ENTER, R.anim.fade_in);
intent.putExtra(YouTubePlayerActivity.EXTRA_ANIM_EXIT, R.anim.fade_out);

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

YoutubeUrlParser

This util helps to retrieve youtube video id from youtube url or vice versa. Reference
String vidoeId = YoutubeUrlParser.getVideoId(videoUrl);
String vidoeUrl = YoutubeUrlParser.getVideoId(videoId);

No comments:

Post a Comment