androidx / androidx/media

How to dynamically change the audio url using media3 exoplayer?

Open
#1,430 3 comments 0 reactions 1 assignee View on GitHub

Nobody has claimed this yet.

needs triage question
Dominant language
Java
Stars
3k
Forks
955
Avg merge
12d 14h
Merged PRs (30d)
2

Description

I use media3 exoplayer to develop a music playback APP. Since the audio URL will become invalid, I need to request a new URL and replace it after it expires.

As follows

val upstreamFactory = ResolvingDataSource.Factory(
	DefaultHttpDataSource.Factory()
) {
	// replace url here when needed
	dataSpec -> dataSpec.withUri(dataSpec.uri)
  }
val myDataSourceFactory = CacheDataSource.Factory()
	.setCache(myLocalCache)
	.setUpstreamDataSourceFactory(upstreamFactory)
	.setCacheWriteDataSinkFactory(null)
	.setFlags(CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR)
val myMediaSourceFactory = DefaultMediaSourceFactory(this).setDataSourceFactory(myDataSourceFactory)
ExoPlayer.Builder(this).apply {
	setMediaSourceFactory(myMediaSourceFactory)
}.build()

When the url is invalid, replace it with a new url through ResolvingDataSource.
But there is a problem. When the URL is replaced, the player will stop playing after a while.

There's no obvious error logs. In DefaultHttpDataSource, the open(DataSpec dataSpec) method is executed and the connection is established.
I guess CacheDataSource did a check and found that the two URLs were different or the container meta information was different, so it stopped. I'm not sure.

Is there a solution?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.