Android Exo Player从Android资源播放.m3u8

问题描述 投票:-3回答:1

我无法从像http://example.com/file.m3u8这样的网络服务器播放.m3u8文件

我在android资源中添加了播放列表文件,但它没有播放。

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

谢谢,

android exoplayer exoplayer2.x
1个回答
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);
© www.soinside.com 2019 - 2024. All rights reserved.