Mladen Rakonjac
Mladen Rakonjac

Reputation: 9812

How to stream mp3 url using exoplayer?

Exoplayer library seems so complex for me. Can anyone help me how to stream radio station url using exoplayer library? I tried with MediaPlayer , it works fine but it took so much time to prepare. Here is what I tried.

    exoPlayer = ExoPlayer.Factory.newInstance(RENDERER_COUNT);

    Allocator allocator = new DefaultAllocator(BUFFER_SEGMENT_SIZE);
    DataSource dataSource = new DefaultUriDataSource(getApplicationContext(), null, userAgent);
    Mp3Extractor extractor = new Mp3Extractor();
    ExtractorSampleSource sampleSource = new ExtractorSampleSource(
            uri, dataSource, extractor, allocator, BUFFER_SEGMENT_COUNT * BUFFER_SEGMENT_SIZE);
     MediaCodecAudioTrackRenderer audioRenderer = new MediaCodecAudioTrackRenderer(sampleSource);
    exoPlayer.prepare(audioRenderer);

    exoPlayer.setPlayWhenReady(true);

I don't understand how to get userAgent and what is meaning of it?

Upvotes: 10

Views: 12689

Answers (3)

Mladen Rakonjac
Mladen Rakonjac

Reputation: 9812

It was problem with codec, exoPlayer doesn't support aac+

Upvotes: 0

Karim Abdell Salam
Karim Abdell Salam

Reputation: 706

This is the simplest way to stream m3u8 files using ExoPlayer Lib. check this code and don't forget to change the URL to yours and also change the content type as the blew code hope to help https://github.com/karim23/SimpleStreamPlayer/tree/master

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context = getApplicationContext();
    setContentView(R.layout.activity_main);
    //change the live streaming URL with yours. 
    contentUri = /*"Your mp3 URL.."*/;
    // contentType = DemoUtil.TYPE_MP3;
    final Intent intent = new Intent(context, VideoPlayerActivity.class).setData(Uri.parse(contentUri))
            .putExtra(VideoPlayerActivity.CONTENT_ID_EXTRA, -1)
            //Change the type according to the live streaming extension.
            .putExtra(VideoPlayerActivity.CONTENT_TYPE_EXTRA, DemoUtil.TYPE_MP3);
    liveStreamingTv =(TextView)findViewById(R.id.mainActivity_liveStreamingTv);
    liveStreamingTv.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            startActivity(intent);
        }
    });
}

Upvotes: 0

mismor
mismor

Reputation: 605

Here is a nice description of what a user agent is: user agent Also here the definition of how a user agent should look like: structure of user agent header Here you can see how your browser's user agent looks like: http://whatsmyuseragent.com/

To put it simply you can create your user agent like this:

"YourAppName/VersionCode"

Finally a description of how to use ExoPlayer to stream mp3: Stream mp3 with ExoPlayer In this example it is a local mp3 though, but the only difference should be the url of the mp3 and the missing user agent. Hope this helps!

Upvotes: 6

Related Questions