-2

I am unable to play .m3u8 file from a web server like http://example.com/file.m3u8

I have added the play list file in android resource but it is not playing.

How to play the same file from local resources.

Thanks,

Raja Jawahar
  • 6,742
  • 9
  • 45
  • 56
For Guru
  • 1,197
  • 13
  • 23

1 Answers1

-1

From ExoPlayer Demo:

  public void init(Context context, PlayerView playerView) {
    // Create a default track selector.
    BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
    TrackSelection.Factory videoTrackSelectionFactory =
        new AdaptiveTrackSelection.Factory(bandwidthMeter);
    TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);

    // Create a player instance.
    player = ExoPlayerFactory.newSimpleInstance(context, trackSelector);

    // Bind the player to the view.
    playerView.setPlayer(player);

    // This is the MediaSource representing the content media (i.e. not the ad).
    String contentUrl = context.getString(R.string.content_url);
    MediaSource contentMediaSource =
        buildMediaSource(Uri.parse(contentUrl), /* handler= */ null, /* listener= */ null);

    // Compose the content media source into a new AdsMediaSource with both ads and content.
    MediaSource mediaSourceWithAds =
        new AdsMediaSource(
            contentMediaSource,
            /* adMediaSourceFactory= */ this,
            adsLoader,
            playerView.getOverlayFrameLayout(),
            /* eventHandler= */ null,
            /* eventListener= */ null);

    // Prepare the player with the source.
    player.seekTo(contentPosition);
    player.prepare(mediaSourceWithAds);
    player.setPlayWhenReady(true);
  }

Strings.xml:

<string name="content_url"><![CDATA[https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8]]></string>

Upd: I'm not sure that using *.m3u8 files from local storage would be a good idea, but you can play with the media files from your apk:

1) raw

 MediaSource contentMediaSource = buildMediaSource(
 RawResourceDataSource.buildRawResourceUri(R.raw.sample_video), 
/* handler= */ null, /* listener= */ null);

2) asset

MediaSource contentMediaSource = buildMediaSource(
Uri.fromFile(new File("//android_asset/sample_video.mp4")), 
/* handler= */ null, /* listener= */ null);
rminaj
  • 560
  • 6
  • 28
Dima Kozhevin
  • 3,602
  • 9
  • 39
  • 52
  • Thanks for reply. I am also having this working. I need to provide .m3u8 file in local resources. I don't want to store URL in string table. – For Guru Jun 29 '18 at 13:29