Play m3u8 or mpd file videos offline

3.9k views Asked by At

I get m3u8 or mpd file from server and i download it in my app storage. How will i play it offline. Im newbie to offline videos. Any suggestion for its usage methods. Currently i play m3u8 or mpd files with exoplayer which is Dash, HLS or progressive videos. How to play these videos offline by keeping allrights drm in mind.

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

below code i use from exoplayer sdk to find type of stream from file path 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;
  }

And How youtube offline video works?

2

There are 2 answers

0
Mick On BEST ANSWER

The mpd or m3u8 files are just manifests or index files. They are text based containing information about the video and links to the media, i.e. the audio, video, subtitles etc streams.

If you want to download a HLS or DASH stream for offline playback, you will need to download the manifest and the media streams they refer to.

ExoPlayer provides functionality to help you download video and take care of much of this complexity, including specific information on downloading HLS and DASH videos - see the ExoPlayer documentation:

Note the section around tracks selection and bit rates - typically you will want to choose a particular bitrate to download rather than downloading all available ones:

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.

To support encrypted streams you need to ensure that the DRM server supports persistent licenses for that content and the entitlements allow offline playback - you will generally need to speak to your DRM provider to confirm this.

1
Rahul Kamble On

I have done this for .mpd audio files, and its working for me

1] Take mpd URL and pass into str as shown below (It is used to download song from .mpd url to cache memory)

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] Initialize Exo player(mp) and set .mpd uri to the source (It is used to play offline song)

    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] You will get required/missing classes or service from below git link

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

on path : ExoPlayer/demos/main/src/main/java/com/google/android/exoplayer2/demo/