Android Exo Player Play .m3u8 from Android Resources


Android Exo Player Play .m3u8 from Android Resources



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,





Could you post your code?
– Dima Kozhevin
Jun 29 at 11:12




1 Answer
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 use *.m3u8 files from local storage a good idea, but you can play 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);





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 at 13:29






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV