离线播放 m3u8 或 mpd 文件视频

Play m3u8 or mpd file videos offline

我从服务器获取 m3u8 或 mpd 文件并将其下载到我的应用程序存储中。我将如何离线播放它。我是离线视频的新手。对其使用方法的任何建议。目前我使用 exoplayer 播放 m3u8 或 mpd 文件,即 Dash、HLS 或渐进式视频。如何通过牢记 allrights drm 离线播放这些视频。

https://www.exampleurl.com/playlist.m3u8?hdntl=st=16608748~exp=166797876~acl=/*~hmac=fdhf577jbjb5ss76dsd6ds78d7d6ghg78

下面的代码我使用 exoplayer sdk 从文件路径 lastsegment 中查找流类型

@ContentType
  public static int inferContentType(String fileName) {
    fileName = toLowerInvariant(fileName);
    if (fileName.endsWith(".mpd")) {
      return C.TYPE_DASH;
    } else if (fileName.endsWith(".m3u8")) {
      return C.TYPE_HLS;
    }
    Matcher ismMatcher = ISM_URL_PATTERN.matcher(fileName);
    if (ismMatcher.matches()) {
      @Nullable String extensions = ismMatcher.group(2);
      if (extensions != null) {
        if (extensions.contains(ISM_DASH_FORMAT_EXTENSION)) {
          return C.TYPE_DASH;
        } else if (extensions.contains(ISM_HLS_FORMAT_EXTENSION)) {
          return C.TYPE_HLS;
        }
      }
      return C.TYPE_SS;
    }
    return C.TYPE_OTHER;
  }

以及 youtube 离线视频的工作原理?

mpd 或 m3u8 文件只是清单或索引文件。它们是基于文本的,包含有关视频和媒体链接的信息,即音频、视频、字幕等流。

如果要下载 HLS 或 DASH 流以供离线播放,则需要下载清单及其引用的媒体流。

ExoPlayer 提供的功能可帮助您下载视频并解决大部分复杂问题,包括有关下载 HLS 和 DASH 视频的特定信息 - 请参阅 ExoPlayer 文档:

请注意有关曲目选择和比特率的部分 - 通常您会希望选择特定的比特率进行下载,而不是下载所有可用比特率:

For streaming playbacks, a track selector can be used to choose which of the tracks are played. Similarly, for downloading, a DownloadHelper can be used to choose which of the tracks are downloaded.

要支持加密流,您需要确保 DRM 服务器支持该内容的持久许可证并且权利允许离线播放 - 您通常需要与 DRM 提供商联系以确认这一点。

我已经为 .mpd 音频文件做了这个,它对我有用

1] 取mpd URL 传入str 如下所示(用于从.mpd url 下载歌曲到缓存)

contentUri = Uri.parse(str);
offlineAudioList.add(contentUri); //offlineAudioList is the arrayList of type uri
downloadRequest = new DownloadRequest.Builder(str, contentUri).build();
DownloadService.sendAddDownload(
getActivity(),
DemoDownloadService.class,
downloadRequest,
/* foreground= */ false);

2] 初始化Exo player(mp) 并将.mpd uri设置为源(用于播放离线歌曲)

    DataSource.Factory cacheDataSourceFactory = 
    new CacheDataSource.Factory()
    .setCache(DemoUtil.getDownloadCache(getActivity()) )
    .setUpstreamDataSourceFactory(DemoUtil.getDataSourceFactory(getActivity()))
    .setCacheWriteDataSinkFactory(null); // Disable writing.
    
    mp = new ExoPlayer.Builder(/* context= */ this)
            .setMediaSourceFactory(new DefaultMediaSourceFactory(cacheDataSourceFactory))
            .build();
    
    mp.addListener(new Player.EventListener() {
        @Override
        public void onPlayerError(PlaybackException error) {
            Toast.makeText(getActivity(), "error: "+error.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });
    
    for (int i = 0; i < OfflineAudioFragment.offlineAudioList.size(); i++) {
        DashMediaSource source =
    new DashMediaSource.Factory(cacheDataSourceFactory)
            .createMediaSource(MediaItem.fromUri(OfflineAudioFragment.offlineAudioList.get(i)));
        mediaSourceList.add(source);
    
        source.addEventListener(new Handler(), new MediaSourceEventListener() {
            @Override
            public void onLoadError(int windowIndex, @Nullable MediaSource.MediaPeriodId mediaPeriodId, LoadEventInfo loadEventInfo, MediaLoadData mediaLoadData, IOException error, boolean wasCanceled) {
    Log.d("cacheDataSourceFactory",error.getMessage());
            }
        });
    
    }
mp.setMediaSource(mediaSourceList.get(PlayerConstants.SONG_NUMBER));
mp.prepare();
mp.setPlayWhenReady(true);
mp.play();

3] 您将得到 required/missing 类 或下面的服务 git link

https://github.com/google/ExoPlayer.git

在路径上:ExoPlayer/demos/main/src/main/java/com/google/android/exoplayer2/demo/