Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to fetch media urls for media items just in time? #13

Closed
NikSatyr opened this issue Dec 1, 2021 · 3 comments
Closed

How to fetch media urls for media items just in time? #13

NikSatyr opened this issue Dec 1, 2021 · 3 comments
Assignees
Labels

Comments

@NikSatyr
Copy link

NikSatyr commented Dec 1, 2021

I'm trying to migrate an app from bare ExoPlayer to Media3. The app heavily uses background audio playback, so I thought that I should go with session demo.

Due to our backend limitations, we have to request media url from server separately for each media item. Previously we were using exoPlayer.setMediaSource() with a ConcatenatingMediaSource that relied on DataSource that handled just in time fetching logic. I think a pretty similar example can be found here.

As far as I understand, I am supposed to use MediaController for controlling the underlying player to ensure background playback. However, MediaController does not have setMediaSource() and instead has setMediaItems() method.

Personally, I see 2 options and so far they do not seem to be viable:

  1. Use MediaSession.MediaItemFiller & fetch the url in fillInLocalConfiguration() – however, I suspect that when using setMediaItems() it will try to load all the items' urls & furthermore block the main thread
  2. Not to use MediaController & instead directly interact with setMediaSource() of ExoPlayer that would be stored as a field in MediaLibraryService & used to create a MediaLibrarySession. However, I think this will create a ton of issues for syncing the media session and ensuring background work

So could you please explain me how do I implement just in time links fetching with Media3?

@ojw28
Copy link
Contributor

ojw28 commented Dec 2, 2021

Starting from using bare ExoPlayer, I'd suggest updating your code incrementally as follows:

  1. Remove your usage of ConcatenatingMediaSource. Instead, add the child MediaSource instances directly to the player using ExoPlayer.addMediaSource. To remove an item at an index, you can use removeMediaItem. You should find that you can pretty directly replace your usage of ConcatenatingMediaSource with equivalent method calls directly on the player.

  2. Try and switch to passing MediaItem instances to addMediaItem, rather than MediaSource instances to addMediaSource. Each MediaItem is converted into a MediaSource inside the player, by a factory that you give to the player when you create it. So you just need to make sure this factory creates MediaSource instances that use your special DataSource:

MediaSourceFactory mediaSourceFactory =
    new DefaultMediaSourceFactory(dataSourceFactory) // Use a factory for your custom DataSource here
           // Any other config you want
player =
    new ExoPlayer.Builder(/* context= */ this)
           .setMediaSourceFactory(mediaSourceFactory)
           // Any other config you want
           .build();

At this point you're adding and removing MediaItem instances, so it should be pretty easy for you to switch to doing that via MediaController.

@ojw28 ojw28 self-assigned this Dec 2, 2021
@ojw28 ojw28 removed the needs triage label Dec 2, 2021
@NikSatyr
Copy link
Author

NikSatyr commented Dec 3, 2021

Thanks for the explanation, I've got the idea. I'll have to play around with this for a few more days to fit this into our business logic & I'll close the issue then. Thanks a lot!

@NikSatyr
Copy link
Author

NikSatyr commented Dec 6, 2021

Yep, works for our needs, thanks! We'll combine the approach you suggested with passing custom urls to resolver to parse them for getting needed information to fetch the stream link (smth similar can be found here)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants