我在Android应用中使用Exoplayer
库来播放一些实时HLS视频(.m3u8
格式)。版本是2.8.4
。我需要使用户可以在观看视频时更改实时流的视频质量。
为此,我使用followng方法(我来自exoplayer
示例项目):
Pair<AlertDialog, TrackSelectionView> dialogPair = TrackSelectionView.getDialog(this, title, trackSelector, rendererIndex);
dialogPair.second.setShowDisableOption(true);
dialogPair.second.setAllowAdaptiveSelections(allowAdaptiveSelections);
dialogPair.first.show();
当我选择某种视频质量时,我看到了这种新质量,但是exoplayer经常在几秒钟之后(有时非常频繁)在Player.EventListener
方法中的onPlayerError(ExoPlaybackException error)
中引发错误。 ExoPlaybackException始终具有TYPE_SOURCE和空消息。而且我还注意到,当我看到自动质量的实时视频时,当我的互联网连接断开时,它的质量并没有降低,我发现TYPE_SOURCE出现相同的exoplayer错误。
这是我初始化播放器的代码:
bandwidthMeter = new DefaultBandwidthMeter();
AdaptiveTrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
player = ExoPlayerFactory.newSimpleInstance(
new DefaultRenderersFactory(getActivity()),
trackSelector,
new DefaultLoadControl());
exoPlayerOnline.setPlayer(player);
MediaSource mediaSource = buildMediaSource(Uri.parse(videoStreamUrl));
player.prepare(mediaSource, true, false);
player.setPlayWhenReady(playWhenReady);
和方法buildMediaSource()
:
private MediaSource buildMediaSource(Uri uri) {
DefaultHttpDataSourceFactory httpDataSourceFactory = new DefaultHttpDataSourceFactory(userAgent,
bandwidthMeter,
20 * 1000,
20 * 1000,
true);
DefaultDataSourceFactory defaultDataSourceFactory = new DefaultDataSourceFactory(getActivity(),
bandwidthMeter,
httpDataSourceFactory);
OkHttpDataSourceFactory okHttpDataSourceFactory = new OkHttpDataSourceFactory(okHttpClient,
userAgent,
bandwidthMeter);
int type = Util.inferContentType(uri);
switch (type) {
case C.TYPE_SS:
return new SsMediaSource.Factory(new DefaultSsChunkSource.Factory(defaultDataSourceFactory), okHttpDataSourceFactory)
.createMediaSource(uri);
case C.TYPE_DASH:
return new DashMediaSource.Factory(new DefaultDashChunkSource.Factory(defaultDataSourceFactory), okHttpDataSourceFactory)
.createMediaSource(uri);
case C.TYPE_HLS:
return new HlsMediaSource.Factory(okHttpDataSourceFactory)
.createMediaSource(uri);
case C.TYPE_OTHER:
return new ExtractorMediaSource.Factory(okHttpDataSourceFactory)
.createMediaSource(uri);
default:
Toast.makeText(getActivity(), "Unsupported video type", Toast.LENGTH_LONG).show();
return null;
}
}
那么,请帮助我,如何解决这个问题?