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?
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.
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.
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With