Search code examples
androidmedia-playerandroid-mediaplayerexoplayerexoplayer2.x

Android : How to use exoplayer 2.x.x to stream a radio link


I want to stream for an online radio with exoplayer version rc2.x.x though an android service.

I have been trying to learn from all tutorials online I could but they are all prior to the 2.x.x version and a lot of them are video streaming related as well (which I don't need).

Does anyone has a very nice tutorial?


Solution

  • I don't have deep knowledge of ExoPlayer but I have prepared this code snippet which can stream mp3 link.

    EXO Player Verison : r2.0.4

      private void initMediaPlayer() {
            Handler mHandler = new Handler();
    
            String userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:40.0) Gecko/20100101 Firefox/40.0";
            Uri uri = Uri.parse("http://feedproxy.google.com/~r/TheCombatJackShow/~5/s_9fWPxLDu0/188058705-thecombatjackshow-the-j-cole-episode.mp3");
            DataSource.Factory dataSourceFactory = new DefaultHttpDataSourceFactory(
                    userAgent, null,
                    DefaultHttpDataSource.DEFAULT_CONNECT_TIMEOUT_MILLIS,
                    DefaultHttpDataSource.DEFAULT_READ_TIMEOUT_MILLIS,
                    true);
            MediaSource mediaSource = new ExtractorMediaSource(uri, dataSourceFactory, Mp3Extractor.FACTORY,
                    mHandler, null);
    
            TrackSelector trackSelector = new DefaultTrackSelector(mHandler);
            DefaultLoadControl loadControl = new DefaultLoadControl();
            ExoPlayer exoPlayer = ExoPlayerFactory.newSimpleInstance(this, trackSelector, loadControl);
            //exoPlayer.addListener(this);
    
            exoPlayer.prepare(mediaSource);
            exoPlayer.setPlayWhenReady(true);
    
        }
    

    This link may also help you : https://medium.com/@emuneee/migrating-from-exoplayer-1-x-to-2-0-58fbda36b46c#.b8joifc36