0

I am working with an app that is supposed to play YouTube videos using ExoPlayer2. To fetch the DASH URL of the video I am using this library. The problem with this approach is that the library returns different URLs based on what resolution (itag) you want. For example, if you want the 720p variant of a video you will do something like:

String youtubeLink = "http://youtube.com/watch?v=xxxx";

new YouTubeExtractor(this) {
    @Override
    public void onExtractionComplete(SparseArray<YtFile> ytFiles, VideoMeta vMeta) {
        if (ytFiles != null) {
            int itag = 22;    //22 == 720p
        String downloadUrl = ytFiles.get(itag).getUrl();
        }
    }
}.extract(youtubeLink, true, true);

I can make this URL as a MediaSource and set it using setMediaSource and it works but with only a single resolution (720p in this case). I want to do something like the photo attached to the answer to this question.

I tried reading the docs, I tried Google-Fu but all went in vain. Any ideas on how can I create different tracks with different resolutions for the same video? I tried putting MediaItems and setting them to the player but it instead creates a playlist rather than tracks. Any help?

Sorry for the lack of code, the previous developer has made a complete mess in organizing the code and I do not know what is where. If I can just get a rough code that would be insanely helpful.

Thanks

Mihir Kandoi
  • 101
  • 1
  • 10

1 Answers1

2

Update 2023

The YouTube player SDK has been deprecated as pointed out by
hannunehg in the comments. The new advice is to use their iFrame player and the documentation is here: https://developers.google.com/youtube/iframe_api_reference

Original answer

YouTube prefer you to use their own player and provide an SDK and API for this:

If you do build a solution which will extract the streams and play them in your custom player, and there are plenty of examples on SO for this, you will be at the mercy of something changing that may break you implementation without warning - look at the comments histories in some of the examples.

You also run the risk of the Android App review algorithms having a check for this and your app suddenly not complying with the the guidelines and being rejected - being accepted in the past is not really a guarantee that it will be again in the future.

The ExoPlayer track selector expects that your source manifest will have multiple tracks and its job is then to choose between them. In your case you don't have this so would either need to build some sort of proxy to create a new manifest combining the individual URL's you get by querying each resolution as you show above, or modify the player to request a completely new manifest with the single resolution you want.

Mick
  • 24,231
  • 1
  • 54
  • 120