8

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.

Community
  • 1
  • 1
Alin
  • 14,809
  • 40
  • 129
  • 218
  • 4
    I think when you share Youtube videos at a specific time the link changes, so you might need to change the link to the video accordingly. – Lehue Jan 17 '17 at 12:19

2 Answers2

10

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

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.

Bradley Wilson
  • 1,197
  • 1
  • 13
  • 26
  • 1
    This works when starting the video in a browser. I need to test when using the YT app. Thanks – Alin Jan 17 '17 at 16:02
  • For me, by using `vnd.youtube` way on android 5 does not work. It opens the youtube app but fails to load the video. However, opening with webIntent works, even if selection from menu to open with YouTube app. – Alin Feb 13 '17 at 19:14
0

Try Opening url with url something similar to this https://www.youtube.com/watch?v=u0IU8uQniX8&t=2m35s

where t= time in minutes and seconds

t=2m35s

silentsudo
  • 6,730
  • 6
  • 39
  • 81