Creating a simple instance of ExoPlayer

萝らか妹 提交于 2019-12-30 01:17:05

问题


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 having trouble with even creating a simple working instance of ExoPlayer that can stream mp3 or similar.

Would really appreciate any help anyone can give relating to getting a very simple exoplayer instance working from which i can adapt and build upon or if anyone has any leads for more references or guides which I can follow as there seems to be very little documentation available.

Thanks very much for all and any help!


回答1:


First of all instantiate your ExoPlayer with this line:

exoPlayer = ExoPlayer.Factory.newInstance(RENDERER_COUNT, minBufferMs, minRebufferMs);

If you want to play audio only you can use these values:

RENDERER_COUNT = 1 //since you want to render simple audio
minBufferMs = 1000 
minRebufferMs = 5000

Both buffer values can be tweaked according to your requirements

Now you have to create a DataSource. When you want to stream mp3 you can use the DefaultUriDataSource. You have to pass the Context and a UserAgent. To keep it simple play a local file and pass null as userAgent:

DataSource dataSource = new DefaultUriDataSource(context, null);

Then create the sampleSource:

ExtractorSampleSource sampleSource = new ExtractorSampleSource(
                    uri, dataSource, new Mp3Extractor(), RENDERER_COUNT, requestedBufferSize);

uri points to your file, as an Extractor you can use a simple default Mp3Extractor if you want to play mp3. requestedBufferSize can be tweaked again according to your requirements. Use 5000 for example.

Now you can create your audio track renderer using the sample source as follows:

MediaCodecAudioTrackRenderer audioRenderer = new MediaCodecAudioTrackRenderer(sampleSource);

Finally call prepare on your exoPlayer instance:

exoPlayer.prepare(audioRenderer);

To start playback call:

exoPlayer.setPlayWhenReady(true);



回答2:


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.



来源:https://stackoverflow.com/questions/31048475/creating-a-simple-instance-of-exoplayer

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