The HlsMediaSource()
method is deprecated (I'm currently on exoplayer:2.6.1
). What is the recommended method to use for HLS-media instead?
Asked
Active
Viewed 1.3k times
23

Algar
- 5,734
- 3
- 34
- 51
4 Answers
38
After digging into the source code I concluded that
HlsMediaSource.Factory(dataFactory).createMediaSource(mediaUri)
is the way to go.
Edit: Expanding on the other factories
The factory pattern is also the recommended way to instantiate ExtractorMediaSource
, SsMediaSource
, DashMediaSource
, and SingleSampleMediaSource
as per the 2.6.1 release notes.
The factory methods simplifies MediaSource
instantiation, especially in cases when you wish to configure optional parameters whilst leaving others set to their default values, e.g.
DashMediaSource.Factory(chunkSourceFactory, manifestDataSourceFactory)
.setManifestParser(new CustomManifestParser())
.createMediaSource(manifestUri, eventHandler, eventListener)

Algar
- 5,734
- 3
- 34
- 51
-
1Thanks! +1 : I assume similar solutions apply for depreciated functions `SsMediaSource`, `DashMediaSource`, and `ExtractorMediaSource`. – Ωmega Feb 06 '18 at 20:17
-
Thats correct :) Maybe I should add that to the answer as a side note. – Algar Feb 07 '18 at 09:25
-
@Abhishek `val dataFactory = DefaultDataSourceFactory(context, "ua")` – Algar Apr 18 '18 at 17:43
-
I was able to figure that out already. But really thanks for answering – Abhishek Apr 18 '18 at 17:44
-
I'm glad to hear!! Sorry for the delay :) – Algar Apr 18 '18 at 17:45
4
If you cannot find HlsMediaSource you will need to add one more dependency:
implementation "com.google.android.exoplayer:exoplayer-hls:$exoplayer_version"
To implement it we need to have code similar to this:
val userAgent = Util.getUserAgent(context, USER_AGENT)
DefaultDataSourceFactory(
context,
userAgent
)
val source = "https://some_url_link"
val uri = source.toUri()
HlsMediaSource.Factory(factory).createMediaSource(uri)

Mladen Rakonjac
- 9,562
- 7
- 42
- 55
1
You can use this way to get rid of deprecated
val dataSourceFactory = DefaultDataSourceFactory(this, "sample")
ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(MediaItem.fromUri(Uri.parse(uri)))

Amnah
- 27
- 3
0
You should use newest version: com.google.android.exoplayer:exoplayer:2.18.2
This code will work for you:
val defaultHttpDataSourceFactory = DefaultHttpDataSource.Factory()
val mediaItem = MediaItem.fromUri(URL)
val mediaSource = HlsMediaSource.Factory(defaultHttpDataSourceFactory).createMediaSource(mediaItem)
exoPlayer?.apply {
setMediaSource(mediaSource)
seekTo(playbackPosition)
playWhenReady = playWhenReady
prepare()
}

Halil Ozel
- 2,482
- 3
- 17
- 32