i have a url and it changes every 3 seconds. I make a request to the url every 2 seconds and refresh the url. 3 seconds becomes a valid m3u8 file.Only the query parameters in the url change every 3 seconds. I'm returning the same post just a different link.
DataSource.Factory dataSourceFactory = new DefaultHttpDataSourceFactory();
HlsMediaSource hlsMediaSource =
new HlsMediaSource.Factory(dataSourceFactory)
.createMediaSource(MediaItem.fromUri(dataItem.getVideo()));
concatenatingMediaSource = new ConcatenatingMediaSource();
concatenatingMediaSource.addMediaSource(hlsMediaSource);
player.setMediaSource(concatenatingMediaSource);
player.prepare();
player.setPlayWhenReady(true);
private void setLiveStreamData(String id) {
Call<LiveStreamData> liveStreamDataCall = RetrofitBuilder.newCreate().getStreamLive(id);
liveStreamDataCall.enqueue(new Callback<LiveStreamData>() {
@Override
public void onResponse(@NotNull Call<LiveStreamData> call, @NotNull Response<LiveStreamData> response) {
if (response.isSuccessful() && response.body() != null) {
DataSource.Factory dataSourceFactory = new DefaultHttpDataSourceFactory();
HlsMediaSource hlsMediaSource =
new HlsMediaSource.Factory(dataSourceFactory)
.createMediaSource(MediaItem.fromUri(response.body().getUrl()));
concatenatingMediaSource.addMediaSource(hlsMediaSource);
}
}
@Override
public void onFailure(@NotNull Call<LiveStreamData> call, @NotNull Throwable t) {
Log.e(TAG, "onFailure: ", t);
}
});
}
I may not be able to add the exoplayer correctly. because after 3 seconds exoplayer keeps playing the first link and gives an error. After 3 seconds the old url no longer returns an m3u8 file. How can I set up such a structure correctly?
Playback error
com.google.android.exoplayer2.ExoPlaybackException: Source error
It looks like your use case is a Live HLS stream.
For Live you should not have to worry about manually re-requesting the mpd file yourself when it updates as the player will recognise it is a Live stream and request updates itself.
This is actually specified in the HLS RFC along with guidance so the player does not generate too many requests and overload the server:
The client MUST periodically reload a Media Playlist file to learn what media is currently available, unless it contains an EXT-X- PLAYLIST-TYPE tag with a value of VOD, or a value of EVENT and the EXT-X-ENDLIST tag is also present.
However, the client MUST NOT attempt to reload the Playlist file more frequently than specified by this section, in order to limit the collective load on the server.
(HLS RFC: https://datatracker.ietf.org/doc/html/rfc8216)
One important check is to make sure the manifest is correctly formatted for Live streams and in particular that it does not contain the EXT-X-ENDLIST tag as noted above and in the Apple HLS guidelines:
In live sessions, the index file is updated by removing media URIs from the file as new media files are created and made available. The EXT-X-ENDLIST tag isn't present in the live playlist, indicating that new media files will be added to the index file as they become available.
More info including the above at this link: https://developer.apple.com/documentation/http_live_streaming/example_playlists_for_http_live_streaming/live_playlist_sliding_window_construction