Get video id from youtube url using Android Youtube ApI v3

纵然是瞬间 提交于 2020-01-01 19:25:15

问题


I'm using Android Youtube API v3. My application plays youtube video. I want to get videoID from youtube url.
I see there are a lot of sample that uses pattern to parse the url to get the video ID.
But I'm afraid of this way will fail when youtube change their url in the future.
There are some formats of youtube video url:

http://www.youtube.com/watch?v=0zM4nApSvMg&feature=feedrec_grec_index
http://www.youtube.com/user/SomeUser#p/a/u/1/QDK8U-VIH_o
http://www.youtube.com/v/0zM4nApSvMg?fs=1&hl=en_US&rel=0
http://www.youtube.com/watch?v=0zM4nApSvMg#t=0m10s
http://www.youtube.com/embed/0zM4nApSvMg?rel=0
http://www.youtube.com/watch?v=0zM4nApSvMg
http://youtu.be/0zM4nApSvMg

Are there any API to get video ID from the url?
Thanks in advance.


回答1:


Yes, as you already figured this out that it is not that simple to get the video ID of a youtube video because of the inconsistency. You need to include all the possible permutations and combinations in the PHP (or what ever language script) you are going to parse the URL using regular expressions.

This question has been asked previously on SO you can refer that and add your own logic to parse the URL. I am pasting the links of some of them:

Youtube API - Extract video ID

how to get youtube video id from url

JavaScript REGEX: How do I get the YouTube video id from a URL?

https://halgatewood.com/php-get-the-youtube-video-id-from-a-youtube-url

http://www.microtuts.com/c-get-youtube-video-id-from-url-with-regex/




回答2:


It will return you the video id: watchLink ="https://www.youtube.com/watch?v=s6RPsaco-vA"

 public static String getVideoId(String watchLink){

     return watchLink.substring(watchLink.length() - 11);
}


来源:https://stackoverflow.com/questions/33273137/get-video-id-from-youtube-url-using-android-youtube-api-v3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!