来自Android资源的Android Exo Player Play .m3u8

时间:2018-06-29 11:09:20

标签: android exoplayer exoplayer2.x

我无法从http://example.com/file.m3u8之类的Web服务器上播放.m3u8文件。

我已在Android资源中添加了播放列表文件,但未播放。

如何从本地资源播放同一文件。

谢谢

1 个答案:

答案 0 :(得分:0)

来自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>

已更新: 我不确定使用本地存储中的* .m3u8文件是否是一个好主意,但是您可以使用apk中的媒体文件进行播放:

1)原始

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

2)资产

MediaSource contentMediaSource = buildMediaSource(
Uri.fromFile(new File("//android_asset/sample_video.mp4")), 
/* handler= */ null, /* listener= */ null);