【发布时间】:2021-07-02 18:58:49
【问题描述】:
我的 Android 应用使用遥控器浏览一系列 http 直播流。 我使用 TreeMap 将流存储为电视频道,频道号作为键。 下面的代码是打开/更改源 url 的函数。 我想知道您是否可以提出一种更简洁有效的方法来快速切换到另一个来源。也可能减少下一个来源的加载时间。
private void playUrl(String url) {
Uri videoUri = Uri.parse(url);
DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
try {
player.stop();
} catch (Exception e) {
TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
player = ExoPlayerFactory.newSimpleInstance(this, trackSelector);
PlayerView simpleExoPlayerView = findViewById(R.id.player_view);
////Set media controller
simpleExoPlayerView.setUseController(false);//set to true or false to see controllers
simpleExoPlayerView.requestFocus();
// Bind the player to the view.
simpleExoPlayerView.setPlayer(player);
}
// Measures bandwidth during playback. Can be null if not required.
// Produces DataSource instances through which media data is loaded.
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this, Util.getUserAgent(this, "CanaliTV2"), bandwidthMeter);
MediaSource videoSource = new HlsMediaSource(videoUri, dataSourceFactory, 1, null, null);
// Prepare the player with the source.
player.prepare(videoSource);
player.setPlayWhenReady(true); //run file/link when ready to play.
}
【问题讨论】:
标签: android performance http-live-streaming exoplayer2.x