Exoplayer2 - ConcatenatingMediaSource, duration of all sources

六月ゝ 毕业季﹏ 提交于 2021-01-27 11:56:42

问题


I have basic code for playing couple of audio files via exoplayer

    ExtractorMediaSource audioSource = new ExtractorMediaSource(uri, dataSourceFactory, extractor, null, null);
    ExtractorMediaSource audioSource2 = new ExtractorMediaSource(uri2, dataSourceFactory2, extractor, null, null);

    ConcatenatingMediaSource concatenatedSource =
            new ConcatenatingMediaSource(audioSource, audioSource2);

I'm trying to get duration off all media sources using code example from https://github.com/google/ExoPlayer/issues/2122

Timeline timeline = mMediaPlayer.getCurrentTimeline();
int currentWindowIndex = mMediaPlayer.getCurrentWindowIndex();
long currentPosition = mMediaPlayer.getCurrentPosition();
long totalTime = 0;
Timeline.Window tmpWindow = new Timeline.Window();
if (timeline != null) {
   for (int i = 0; i < timeline.getWindowCount(); i++) {
       long windowDuration = timeline.getWindow(i,tmpWindow).getDurationMs();
       totalTime += windowDuration;
       if (i < currentWindowIndex) {
           currentPosition += windowDuration;
       }
  }
 }

The problem is that window for the second source returns negative value, and does that until is played for the first time. So I'm guessing that I need to prepare it somehow at the start, but I'm not sure how. There is prepareSource method on MediaSource type of objects, but I'm failing to use it properly.

Did anyone had and success with implementation of this functionality?

来源:https://stackoverflow.com/questions/42798406/exoplayer2-concatenatingmediasource-duration-of-all-sources

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!