Exoplayer player video mp4 e stream m3u8

巧了我就是萌 提交于 2019-12-07 09:35:30

I believe you do need to use HlsMediaSource for streaming HLS (m3u8). The following example works for me for DASH, HLS, and mp4.

// 1. Create a default TrackSelector
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelection.Factory videoTrackSelectionFactory = new 
    AdaptiveTrackSelection.Factory(bandwidthMeter);
TrackSelector trackSelector = new 
    DefaultTrackSelector(videoTrackSelectionFactory);

playerView = rootView.findViewById(R.id.videoView);
player = ExoPlayerFactory.newSimpleInstance(<context>, trackSelector);
player.setPlayWhenReady(true);
playerView.setPlayer(player);

// DASH
//    DefaultHttpDataSourceFactory dataSourceFactory = new DefaultHttpDataSourceFactory(
//        Util.getUserAgent(<context>, "ExoPlayer"));
//    DefaultDashChunkSource.Factory chunkSourceFactory = new 
//        DefaultDashChunkSource.Factory(dataSourceFactory);
//
//    MediaSource mediaSource = new DashMediaSource(Uri.parse(<dash url>), 
//        dataSourceFactory, chunkSourceFactory, null, null);


// HLS
//    DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(<context>,
//        Util.getUserAgent(rootView.getContext(), "ExoPlayer"));
//
//    // Produces Extractor instances for parsing the media data.
//    ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
//    Handler mainHandler = new Handler();
//    MediaSource mediaSource = new HlsMediaSource(Uri.parse(
//    "https://live3-mediaset-it.akamaized.net/content/hls_clr_xo/live/channel(ch09)/Stream(02)/index.m3u8"),
//        dataSourceFactory, null, null);

// MP4
// Produces DataSource instances through which media data is loaded.
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(<context>,
    Util.getUserAgent(rootView.getContext(), "ExoPlayer"));

// Produces Extractor instances for parsing the media data.
final ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();

// This is the MediaSource representing the media to be played.
MediaSource mediaSource = new ExtractorMediaSource(Uri.parse(
    "https://www.w3schools.com/html/mov_bbb.mp4"),
    dataSourceFactory, extractorsFactory, null, null);

player.prepare(mediaSource);
player.setPlayWhenReady(true);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!