Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 862
Mladen Rakonjac Avatar asked Jul 24 '15 21:07

Mladen Rakonjac


People also ask

How do you play audio on ExoPlayer?

Add dependency of ExoPlayer in your application. Add ExoPlayer in your layout file. In your Java/Kotlin file make a function to initialize the player. This function will initialise the ExoPlayer in the player view and it will play the media when the player is ready.

Does YouTube use ExoPlayer?

Screenshot: The YouTube Android app, which uses ExoPlayer as its video player. ExoPlayer is an app-level media player built on top of low-level media APIs in Android. It is an open source project used by Google apps, including YouTube and Google TV.

What's the difference between exo player and VLC player?

VLC Media Player for Android is one free and open source cross-platform multimedia player. It can play almost any media files, discs, and streaming media. So it is a good choice. ExoPlayer is one open source project that plays media with minimal code.


1 Answers

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!

like image 93
mismor Avatar answered Oct 22 '22 19:10

mismor