Android-youtube-player: Provide an option to play by URL instead of by ID

Created on 26 Mar 2020  路  9Comments  路  Source: PierfrancescoSoffritti/android-youtube-player

It would be easier to use to just provide a direct Youtube URL

question

Most helpful comment

I thought the same to have it being part of the functionality. But urls can be trivial, and is not so simple to handle ones which can even contain url params. In my case I just used the following sample code to get the youtube id.

companion object {
        // https://regexr.com/566ho
        val YOUTUBE_ID = """[=\/]([\w\d_-]+)${'$'}""".toRegex(RegexOption.IGNORE_CASE)

        fun getYoutubeId(url: String): String? {
            val matches = YOUTUBE_ID.find(url)
            return matches?.groupValues?.lastOrNull()
        }
    }

All 9 comments

Why? the video id is in the URL.

Yes, it may be trivial, but also more convenient to use. Instead of having to extract the id with regex, that functionality could be provided directly by the library

I thought the same to have it being part of the functionality. But urls can be trivial, and is not so simple to handle ones which can even contain url params. In my case I just used the following sample code to get the youtube id.

companion object {
        // https://regexr.com/566ho
        val YOUTUBE_ID = """[=\/]([\w\d_-]+)${'$'}""".toRegex(RegexOption.IGNORE_CASE)

        fun getYoutubeId(url: String): String? {
            val matches = YOUTUBE_ID.find(url)
            return matches?.groupValues?.lastOrNull()
        }
    }

Why? the video id is in the URL.

I have to play the live video in which the URL is almost fixed (ex: https://www.youtube.com/c/news24tv/live), and there is no video id in that URL, so how can I play that?

The probably easiest and most error free way is to convert a URL string to a URI object and read the video id with predefined methods. This would look like below:

String urlString = "https://www.youtube.com/watch?v=F-xysz1wNbo&ab_channel=SoundCloudx";
Uri uri = Uri.parse(urlString);
String videoId = uri.getQueryParameter("v");

I think it's better if the library requires only the video ID and does not support URLs, because YouTube might change the URL format or provide various other formats of URLs or URIs (next to the share URL that looks like https://youtu.be/F-xysz1wNbo) which might crash the apps or make the library not functional in the future. By requiring the video ID you always have a common entry point regardless the format of the URI.

@Faravy It seems that the live stream support is not working at all (I guess the iFrame API from YouTube does not allow that). This URL format however would not work, even if live streams would work properly. You should use the video ID that is provided in the URL of the share url of the live stream / video (in your case L9So8AmTr1k).

I think this ticket can be closed for now. Maybe in the future if this will be a more popular request we can discuss again. :)

@malliaridis I like your approach finding the v param. Maybe there is more information regarding finding the first segment fromhttp://youtu.be/NLqAF9hrVbY, that's part of why I shared that regex.

@juanmendez it's as simple as

Uri uri = Uri.parse("http://youtu.be/some-id");
String path = uri.getPath(); // result to "/some-id"
// or if it contains multiple segments seperated by "/"
List<Strging> segments = uri.getPathSegments();
// uri.getPathSegments().get(0) results to "some-id"

I hope I understood your question correctly. :)

@malliaridis yes, that's what I meant. That's awesome!

@Faravy live video support was working last time i checked (it was a while ago). But you probably need to use the YouTube Data API to get the video ID.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

uzaysan picture uzaysan  路  7Comments

Apsaliya picture Apsaliya  路  3Comments

DorenBoust picture DorenBoust  路  6Comments

abahl817 picture abahl817  路  4Comments

divyas93 picture divyas93  路  7Comments