准备好外层播放器后如何重新添加媒体源?
Posted
技术标签:
【中文标题】准备好外层播放器后如何重新添加媒体源?【英文标题】:How to re-add a mediasource after the exoplayer has been prepared? 【发布时间】:2018-06-30 00:23:00 【问题描述】:我正在 android studio 的 exoplayer 中创建一个播放列表视频播放器。我正在添加从服务器提取的第一个视频,并且它正在由 exoplayer 完美播放。但是当我添加第二部分时,exoplayer 不会播放该视频。我想在 exoplayer 准备好后添加第二个视频。我想知道该怎么做。请帮帮我。
我的MainActivity.java
:
public class MainActivity extends AppCompatActivity
SimpleExoPlayer video_player;
PlayerView player_screen;
DefaultTrackSelector track_selector;
DefaultBandwidthMeter band_width_meter = new DefaultBandwidthMeter();
ArrayList<MediaSource> mediaSources_contents = new ArrayList<>();
ConcatenatingMediaSource concatenating_mediaSource_contents;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
player_screen = (PlayerView) findViewById(R.id.player_screen);
player_screen.requestFocus();
TrackSelection.Factory video_track_selection_factory = new AdaptiveTrackSelection.Factory(band_width_meter);
track_selector = new DefaultTrackSelector(video_track_selection_factory);
video_player = ExoPlayerFactory.newSimpleInstance(this, track_selector);
player_screen.setPlayer(video_player);
video_player.setPlayWhenReady(true);
Uri url1 = Uri.parse("THE - URL - OF - FIRST - VIDEO");
DataSource.Factory data_source_factory = new DefaultDataSourceFactory(this, Util.getUserAgent(this, "Application Name"), band_width_meter);
MediaSource source1 = new ExtractorMediaSource.Factory(data_source_factory).createMediaSource(url1);
mediaSources_contents.add(source1);
concatenating_mediaSource_contents = new ConcatenatingMediaSource(mediaSources_contents.toArray(new MediaSource[mediaSources_contents.size()]));
video_player.prepare(concatenating_mediaSource_contents);
// The lines below this point adds the second video to the media source. But it is not being played by the exoplayer.
Uri url2 = Uri.parse("THE - URL - OF - SECOND - VIDEO");
MediaSource source2 = new ExtractorMediaSource.Factory(data_source_factory).createMediaSource(url2);
mediaSources_contents.add(source2);
【问题讨论】:
【参考方案1】:在准备好的时候
初始化ConcatenatingMediaSource
ConcatenatingMediaSource mediaSource = new ConcatenatingMediaSource();
MediaSource firstSource = new ProgressiveMediaSource.Factory(new FileDataSourceFactory()).createMediaSource(Uri.parse(uri.get(0)));
MediaSource secondSource= new ProgressiveMediaSource.Factory(new FileDataSourceFactory()).createMediaSource(Uri.parse(uri.get(1)));
mediaSource.addMediaSources(firstSource);
mediaSource.addMediaSources(secondSource);
player.prepare(mediaSource, true, false);
准备后添加mediasource
MediaSource thirdSource= new ProgressiveMediaSource.Factory(new FileDataSourceFactory()).createMediaSource(Uri.parse(uri.get(3)));
mediaSource.addMediaSources(thirdSource);// mediaSource is refrance of ConcatenatingMediaSource
【讨论】:
【参考方案2】:尝试添加 concatenating_mediaSource_contents.addMediaSource(source2);
而不是 mediaSources_contents.add(source2);
并且也用于第一个媒体源(不是必需的,为了更好的可读性)
【讨论】:
以上是关于准备好外层播放器后如何重新添加媒体源?的主要内容,如果未能解决你的问题,请参考以下文章
C#中添加AxWindowsMediaPlayer控件后如何使用 谢谢好心人帮帮忙