google / ExoPlayer

This project is deprecated and stale. The latest ExoPlayer code is available in https://github.com/androidx/media
https://developer.android.com/media/media3/exoplayer
Apache License 2.0
21.74k stars 6.03k forks source link

Playing a song from the cache and updating the URL if the song didn't fully download into the cache. #11330

Closed staymn closed 10 months ago

staymn commented 1 year ago

I'm caching a song using this code.

if (simpleCache == null) { File cacheDir = getApplicationContext().getExternalCacheDir(); if (cacheDir == null) { cacheDir = getApplicationContext().getCacheDir(); } if (cacheDir.exists() || cacheDir.mkdirs()) { simpleCache = new SimpleCache(cacheDir, new LeastRecentlyUsedCacheEvictor(100 1024 1024); } }

DataSource.Factory dataSourceFactory = new CacheDataSource.Factory().setCache(simpleCache).setUpstreamDataSourceFactory( new DefaultDataSourceFactory(getApplicationContext(), Util.getUserAgent(getApplicationContext(), getApplicationContext().getString(R.string.app_name))));

mediaItem = new MediaItem.Builder() .setUri(Uri.parse(url)) .setCustomCacheKey(key) .build();

mediaSource = new ProgressiveMediaSource.Factory(dataSourceFactory) .createMediaSource(mediaItem);

simpleExoPlayer.setMediaSource(mediaSource); simpleExoPlayer.prepare(); simpleExoPlayer.setPlayWhenReady(true);

The problem is that if the song doesn't fully download, for example, due to a network failure or player release. Then i would like to initially play a part of this song from the cache without the URL. And then, I need to send a request to the server to get the URL to update it for further buffering.

Do you have any advice on this?"

tianyif commented 1 year ago

Hi @staymn,

Let me try to clarify your question, what you want is (assume the song has been partially downloaded): 1) You want the cached part of the song to play before you even have the URL of the song - which means that you need to pass the local URL of the media (and the custom key, if needed) when building the MediaItem. 2) For the un-cached part of the song, you want to request the URL of the song in flight, and let the upstream data source to continue downloading the data via that URL - which means that you need some mechanism to get the media URL from your server before using the URL to request the media from the upstream data source.

I think you've already done 1), and for 2), you may want to try ResolvingDataSource, and implement Resolver with the logic of getting the upstream URL from your server and produces a resolved dataSpec, which will be forwarded to the upstream data source.

Please let me know if this works for your case.

google-oss-bot commented 11 months ago

Hey @staymn. We need more information to resolve this issue but there hasn't been an update in 14 weekdays. I'm marking the issue as stale and if there are no new updates in the next 7 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

staymn commented 11 months ago

@tianyif Thank you for the response. Resolving data source - helped solve my problem!

I used exoplayer 2.18.7 before. Now I'm using media3 1.2.0 and pleasantly surprised!

But in media3 1.2.0, DefaultLoadControl doesn't work correctly compared to 2.18.7.

These two parameters are ignored (bufferForPlaybackMs, bufferForPlaybackAfterRebuffer). If you made any changes, could you briefly explain in two words...

DefaultLoadControl loadControl = new DefaultLoadControl.Builder() .setBufferDurationsMs(5 1024, 2000000, 1024, 1024).setBackBuffer(100 60 * 60, true);

tianyif commented 11 months ago

Hi @staymn,

Could you please explain what the "being ignored" means? Are the values of these two parameters actually used still the default ones rather than those you set via DefaultLoadControl.Builder?

google-oss-bot commented 10 months ago

Hey @staymn. We need more information to resolve this issue but there hasn't been an update in 14 weekdays. I'm marking the issue as stale and if there are no new updates in the next 7 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

google-oss-bot commented 10 months ago

Since there haven't been any recent updates here, I am going to close this issue.

@staymn if you're still experiencing this problem and want to continue the discussion just leave a comment here and we are happy to re-open this.