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.
YouTube have a great way of indicating time in their URL for the videos.
url
of a video is
https://www.youtube.com/watch?v=Z149x12sXsw
&t=0m30s
at the end.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With