我应该如何为exoplayer创建包含视频和图像文件URI的ConcatenatingMediaSource?

时间:2017-11-06 12:21:36

标签: android exoplayer

我创建了一个concatenatingMediaSource,但它跳过图像uris。 我知道exoplayer不支持图像文件。当图像uri到来并且在图像视图中加载图像时,我试图暂停和隐藏exoplayer。

 public void loadMediaSources(){
            MediaSource[] mediaSourcesToLoad = new MediaSource[stories.size()];
            for(int i=0;i<stories.size();i++){
                MediaSource mediaSource = new ExtractorMediaSource(Uri.parse(stories.get(i).getVideoUrl()),
                        dataSourceFactory, extractorsFactory, null, null);
                mediaSourcesToLoad[i] = mediaSource;
            }

            mediaSources = new ConcatenatingMediaSource(mediaSourcesToLoad);

   if(player!=null && mediaSources!=null){
            player.prepare(mediaSources);
        }
}

@Override
public void onPositionDiscontinuity() {
        exoPlayerView.setVisibility(View.VISIBLE);
        ivStory.setVisibility(View.VISIBLE);
        mPosition = player.getCurrentWindowIndex();
        Log.d("Position of Video", String.valueOf(mPosition));

        if(stories!=null && stories.size()>0 && stories.get(player.getCurrentWindowIndex()).getPhotoUrl()!=null){
            viewPhoto(stories.get(player.getCurrentWindowIndex()).getPhotoUrl());
        }
    }



private void viewPhoto(String url){

        Glide.with(this).load(url).listener(new RequestListener<String, GlideDrawable>() {
            @Override
            public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                return false;
            }

            @Override
            public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                stopPlayingVideo();
                exoPlayerView.setVisibility(View.INVISIBLE);
                ivStory.setVisibility(View.VISIBLE);
                return false;
            }
        }).into(ivStory);

    }

1 个答案:

答案 0 :(得分:1)

目前,ExoPlayer不提供任何内置支持播放图像。 您可以为图像轨道生成N秒单色视频(例如使用ffmpeg)。

private MediaSource trackToMediaSource(@NonNull Track track) {
    MediaSource source;
    Uri trackUri;
    switch (track.getType()) {
        case VIDEO:
            trackUri = Uri.parse(track.getUrl());
            ...
            break;
        case IMAGE:
            // Currently images are not supported. We use hardcoded video URL as a workaround.
            trackUri = Uri.parse("asset:///blue.mp4");
            ...
            break;
        ...
    }
        ...
    return source;
}

FFmpeg命令:

ffmpeg -f lavfi -i color=c=#10408c -t 5 blue.mp4

播放器切换到图像曲目时显示ImageView:

@Override
public void onPositionDiscontinuity() {
    int currentTrack = player.getCurrentWindowIndex();
    Track track = tracks.get(currentTrack);
    switch (track.getType()) {
        case IMAGE:
            showImageAbovePlayer(track.getUrl()); // blue.mp4 is played for 5 seconds under the image, but user sees only image
            break;
        case VIDEO:
            hideImageAbovePlayer(); // hide image, user sees current video track
            break;
    }
}