Creating a simple instance of ExoPlayer

前端 未结 3 1538
心在旅途
心在旅途 2020-12-29 11:48

I am currently looking to develop an application that utilises Dash through the ExoPlayer in Android.

To begin with I am going through the demo project however am ha

3条回答
  •  感情败类
    2020-12-29 12:40

    Here is how you would do it using the new ExoPlayer 2 API, and the SimpleExoPlayer.

    First create the player:

    DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
    DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(context, bandwidthMeter);
    
    TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
    DefaultTrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
    LoadControl loadControl = new DefaultLoadControl();
    
    SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance(context, trackSelector, loadControl);
    
    player.addListener(...); // To receive events from the player
    

    Then create your MediaSource. For MP3 you can use ExtractorMediaSource:

    ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
    Uri uri = Uri.parse(mp3UriString);
    Handler mainHandler = new Handler(Looper.getMainLooper());
    MediaSource mediaSource = new ExtractorMediaSource(uri, dataSourceFactory, extractorsFactory, mainHandler, mediaSourceListener); // Listener defined elsewhere
    

    Then prepare and play when ready:

    player.prepare(mediaSource);
    player.setPlayWhenReady(true);
    

    For DASH you would use DashMediaSource instead of ExtractorMediaSource.

提交回复
热议问题