在exoplayer中播放动态url

Playing dynamic url in exoplayer

我有一个 url,它每 3 秒更改一次。我每 2 秒向 url 发出请求并刷新 url。 3 秒成为有效的 m3u8 file.Only url 中的查询参数每 3 秒更改一次。我要返回相同的 post 只是不同的 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);
            }
        });
    }

我可能无法正确添加exoplayer。因为 3 秒后 exoplayer 继续播放第一个 link 并给出错误。 3 秒后,旧 url 不再是 returns m3u8 文件。 如何正确设置这样的结构?

 Playback error
          com.google.android.exoplayer2.ExoPlaybackException: Source error

您的用例似乎是一个 Live HLS 流。

对于直播,您不必担心在更新时自己手动重新请求 mpd 文件,因为播放器会识别它是直播流并自行请求更新。

这实际上是在 HLS RFC 和指导中指定的,因此播放器不会生成太多请求并使服务器过载:

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

一项重要的检查是确保清单的格式正确适用于直播流,特别是它不包含 EXT-X-ENDLIST 标签,如上文和 Apple HLS 指南所述:

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.

包括以上内容在内的更多信息在此 link:https://developer.apple.com/documentation/http_live_streaming/example_playlists_for_http_live_streaming/live_playlist_sliding_window_construction