Android Youtube App Play Video Intent

How to open the youtube app using Intent

you can open it using the package name com.google.android.youtube

start application knowing package name

Intent.getLaunchIntentForPackage("com.google.android.youtube");

How to play YouTube video via Intent?

This code i have used to play youtube video

Matcher matcher = Pattern.compile("http://www.youtube.com/embed/").matcher(mVideoId);
matcher.find()
Intent lVideoIntent = new Intent(
null,
Uri.parse("ytv://" + mVideoId),
MainScreen.mContext,
com.kids.youtube.OpenYouTubePlayerActivity.class);
startActivity(lVideoIntent);

Intent to play playlist in youtube app

After doing some more research, I figured out how to open a playlist within the youtube app

    Uri uri = Uri.parse("http://www.youtube.com/playlist?list=" + playlist_id);
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(uri);
i.setClassName("com.google.android.youtube", "com.google.android.youtube.app.froyo.phone.PlaylistActivity");
startActivity(i);

I'm still not quite sure how to automatically start playing the first video though...

Open youtube video at a specific time using android intent

YouTube have a great way of indicating time in their URL for the videos.

  • So let's say a url of a video is
    https://www.youtube.com/watch?v=Z149x12sXsw
  • You can reference the same URL with it playing automatically 30
    seconds in by putting &t=0m30s at the end.
  • When you open the video, pass in the new url with the new
    extension. It should look something like
    https://www.youtube.com/watch?v=Z149x12sXsw&t=0m30s

The Intent will look something like startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.youtube.com/watch?v=Z149x12sXs" + time))); where String time = "&t=0m30s";

Edit: Expansion for YouTube App.

public static void watchYoutubeVideo(String id, String time){
Intent appIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:" + id + time));
Intent webIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.youtube.com/watch?v=" + id + time));
try {
startActivity(appIntent);
} catch (ActivityNotFoundException ex) {
startActivity(webIntent);
}
}

Using another answer from that question. The same logic can be applied to any intent just add the Time string to the URI like shown above regardless of intention.

How to open a Youtube video link directly from android app?

you can set package name of youtube application

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.youtube.com/watch?v=bzSTpdcs-EI"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setPackage("com.google.android.youtube");
startActivity(intent)


Related Topics



Leave a reply



Submit