准备好exoplayer后如何重新添加媒体源?

时间:2018-06-30 00:23:00

标签: android exoplayer exoplayer2.x

我正在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);
    }
}

2 个答案:

答案 0 :(得分:0)

请尝试添加此concatenating_mediaSource_contents.addMediaSource(source2);而不是mediaSources_contents.add(source2);,并且也要添加第一个媒体源(不必要,以提高可读性)

答案 1 :(得分:0)

在准备时间

初始化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