如何让ExoPlayer通过其URL识别源类型?

时间:2017-09-23 14:52:25

标签: android exoplayer

我最近在Android应用程序中开始使用ExoPlayer,但我遇到的问题是ExoPlayer无法通过其URL自动识别源类型。

有没有解决方案,所以我可以传递一个URL,ExoPlayer自动定义如何播放它(作为视频或HLS流)。

1 个答案:

答案 0 :(得分:2)

private MediaSource buildMediaSource(Uri uri, String overrideExtension) {
    int type = TextUtils.isEmpty(overrideExtension) ? Util.inferContentType(uri)
    : Util.inferContentType("." + overrideExtension);
    switch (type) {
      case C.TYPE_SS:
          return new SsMediaSource(uri, buildDataSourceFactory(false),
              new DefaultSsChunkSource.Factory(mediaDataSourceFactory), mainHandler, eventLogger);
      case C.TYPE_DASH:
          return new DashMediaSource(uri, buildDataSourceFactory(false),
              new DefaultDashChunkSource.Factory(mediaDataSourceFactory), mainHandler, eventLogger);
      case C.TYPE_HLS:
          return new HlsMediaSource(uri, mediaDataSourceFactory, mainHandler, eventLogger);
      case C.TYPE_OTHER:
          return new ExtractorMediaSource(uri, mediaDataSourceFactory, new DefaultExtractorsFactory(),
              mainHandler, eventLogger);
      default: {
          throw new IllegalStateException("Unsupported type: " + type);
    }
}

}

来源:https://github.com/google/ExoPlayer/blob/release-v2/demo/src/main/java/com/google/android/exoplayer2/demo/PlayerActivity.java