Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing the new released ExoPlayer 2

Exoplayer library seems so complex for me. Can anyone help me how to stream radio station url using the new released exoplayer 2 library on Android? I'm currently trying to stream mp3 files from a server and looking to put the player inside an IntentService to keep audio files playing on background. Thanks in advance.

like image 637
Anouar Bellakha Avatar asked Sep 19 '16 21:09

Anouar Bellakha


2 Answers

Here is a very basic setup to get audio streaming through ExoPlayer2:

Handler handler = new Handler();
TrackSelector trackSelector = new DefaultTrackSelector(handler);
LoadControl loadControl = new DefaultLoadControl();
SimpleExoPlayer exoPlayer = ExoPlayerFactory.newSimpleInstance(this, trackSelector, loadControl);

Uri audioUri = Uri.parse("http://stream.basso.fi:8000/stream");
DefaultHttpDataSourceFactory dataSourceFactory = new DefaultHttpDataSourceFactory("ExoPlayerDemo");
ExtractorsFactory extractor = new DefaultExtractorsFactory();
MediaSource audioSource = new ExtractorMediaSource(audioUri, dataSourceFactory, extractor, null, null);
exoPlayer.prepare(audioSource);
exoPlayer.setPlayWhenReady(true);

Yes, there are a lot of hoops to jump through... :)

like image 73
mtkopone Avatar answered Oct 20 '22 00:10

mtkopone


Here is simple demonstration of ExoPlayer for mp3

compile 'com.google.android.exoplayer:exoplayer:r2.4.2'

Then

    public class MainActivity extends AppCompatActivity implements ExoPlayer.EventListener{

private BandwidthMeter bandwidthMeter;
private TrackSelector trackSelector;
private TrackSelection.Factory trackSelectionFactory;
private SimpleExoPlayer player;
private DataSource.Factory dataSourceFactory;
private ExtractorsFactory extractorsFactory;
private DefaultBandwidthMeter defaultBandwidthMeter;
private MediaSource mediaSource;

private String songUrl = "http://www.mfiles.co.uk/mp3-downloads/edvard-grieg-peer-gynt1-morning-mood-piano.mp3";

private Button stopButton;
private Button startButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    startButton = (Button) findViewById(R.id.btnStart);
    stopButton = (Button) findViewById(R.id.btnStop);

    bandwidthMeter = new DefaultBandwidthMeter();
    extractorsFactory = new DefaultExtractorsFactory();

    trackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);

    trackSelector = new DefaultTrackSelector(trackSelectionFactory);

    defaultBandwidthMeter = new DefaultBandwidthMeter();

    dataSourceFactory = new DefaultDataSourceFactory(this,
            Util.getUserAgent(this, "mediaPlayerSample"),defaultBandwidthMeter);


    mediaSource = new ExtractorMediaSource(Uri.parse(songUrl),
            dataSourceFactory,
            extractorsFactory,
            null,
            null);

    player = ExoPlayerFactory.newSimpleInstance(this,trackSelector);

    player.addListener(this);
    player.prepare(mediaSource);

    Log.v("TEST","playing state : " + player.getPlaybackState());

    startButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            player.setPlayWhenReady(true);
        }
    });

    stopButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            player.setPlayWhenReady(false);
        }
    });
}

@Override
protected void onDestroy() {
    super.onDestroy();
    player.setPlayWhenReady(false);
}


@Override
public void onLoadingChanged(boolean isLoading) {
    Log.i("TEST", "onLoadingChanged: " + isLoading + "");
    Log.i("TEST", "Buffered Position: " + player.getBufferedPosition() + "");
    Log.i("TEST", "Buffered Percentage: " + player.getBufferedPercentage() + "");
}

@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {

    if(playbackState == ExoPlayer.STATE_READY){
        Log.i("TEST", "ExoPlayer State is: READY");
    } else if (playbackState == ExoPlayer.STATE_BUFFERING){
        Log.i("TEST", "ExoPlayer State is: BUFFERING");
    } else if (playbackState == ExoPlayer.STATE_ENDED){
        Log.i("TEST", "ExoPlayer State is: ENDED");
    } else if (playbackState == ExoPlayer.STATE_IDLE){
        Log.i("TEST", "ExoPlayer State is: IDLE");
    }


}

@Override
public void onTimelineChanged(Timeline timeline, Object manifest) {

}

@Override
public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {

}

@Override
public void onPlayerError(ExoPlaybackException error) {

}

@Override
public void onPositionDiscontinuity() {

}

@Override
public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {

}
}
like image 2
ysfcyln Avatar answered Oct 19 '22 23:10

ysfcyln