Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open youtube video at a specific time using android intent

I am opening a youtube video from my app, similar with this answer: https://stackoverflow.com/a/12439378/379865

I was wondering if it's possible to open the video at a specified time, so for instance have video running from 30 seconds at start, not from the beginning.

like image 934
Alin Avatar asked Jan 17 '17 12:01

Alin


1 Answers

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.

like image 146
Bradley Wilson Avatar answered Oct 18 '22 10:10

Bradley Wilson