Ossir
Ossir

Reputation: 503

Stream radio on Android

I'm developing internet radio using android MediaPlayer. I need to update UI when the song is over and new song begins. How to find, that song is over? This is my current code. mMediaPlayer.start(); calls when user tap the button.

    mMediaPlayer = new MediaPlayer();
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    try {
        mMediaPlayer.setDataSource(streamURL);
        mMediaPlayer.prepareAsync();
    } catch (IOException e) {
        e.printStackTrace();
    }

Upvotes: 1

Views: 803

Answers (1)

4gus71n
4gus71n

Reputation: 4073

Radio streaming is endless so the streaming is never going to end, however if what you can do is track the metadata in the radio streaming for the currently playing song, and if you detect that something has changed, then do something. Something more important that I want to point is that MediaPlayer is awful slow for radio streaming, I developed a radio streaming app myself so I strongly reccomend you to use this library for this, for two reasons, one is faster and has a bigger buffer than the MediaPlayer (MediaPlayer class has at some point a hardcoded byte[] buffer = new byte[4096]; buffer that'll bring you some issues) and second because this library lets you handle the metadata in a more confortable way.

Upvotes: 3

Related Questions