8

I have an Android app that streams an MP3 file and plays this file in player, but the problem is mediaPlayer.prepare(); takes a long time buffering and the app freezes so I tried to use prepareAsync();, but with this function I can't make the player play the next file.

It just plays a single file online; if I need to play another file I have to close and restart the activity when play ends. This is my code:

public void playMp3(String _link)
{
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
    public void onPrepared(MediaPlayer mp) {
        if(!mediaPlayer.isPlaying()){
        mediaPlayer.start();
        Progressbar.setVisibility(View.INVISIBLE);
        play.setVisibility(View.GONE);
        stop.setVisibility(View.VISIBLE);
        songProgressBar.setProgress(0);
        songProgressBar.setMax(100);
        }
        updateProgressBar();      
    }
});
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

    @Override
    public void onCompletion(MediaPlayer mp) {
        // TODO Auto-generated method stub
        mediaPlayer.reset();
        songProgressBar.setProgress(0);
        songProgressBar.setSecondaryProgress(0);
        play.setVisibility(View.VISIBLE);
        stop.setVisibility(View.GONE);
        link = "http://server11.mp3quran.net/hawashi/002.mp3";
        playMp3(link);

    }
});
mediaPlayer.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
    @Override
    public void onBufferingUpdate(MediaPlayer mp, int percent) {
       // Toast.makeText(getApplicationContext(), "n" + percent, Toast.LENGTH_LONG).show();
        songProgressBar.setSecondaryProgress(percent);

         if(percent==100)
         {
             Progressbar.setVisibility(View.INVISIBLE);

         }else if(percent > songProgressBar.getProgress())
         {
             Progressbar.setVisibility(View.INVISIBLE);
         }else
         {
             Progressbar.setVisibility(View.VISIBLE);
         }

    }
});

mediaPlayer.reset();
Progressbar.setVisibility(View.VISIBLE);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

try {
    mediaPlayer.setDataSource(_link);
    //mediaPlayer.prepare(); // might take long! (for buffering, etc)   //@@
    mediaPlayer.prepareAsync();
} catch (IllegalArgumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (SecurityException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IllegalStateException e) {
    // TODO Auto-generated catch block///
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}}
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Bahaa Odeh
  • 583
  • 1
  • 7
  • 16

1 Answers1

9

Actually I don't know where is error in your code but I will explain how I did that in my app

public void playMp3(String _link){

                mediaPlayer.reset();
                Progressbar.setVisibility(View.VISIBLE);
                mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

            try {
                mediaPlayer.setDataSource(_link);
                mediaPlayer.setOnBufferingUpdateListener(this);
                mediaPlayer.setOnPreparedListener(this);
                //mediaPlayer.prepare(); // might take long! (for buffering, etc)   //@@
                mediaPlayer.prepareAsync();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block///
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

Then implements OnCompletionListener, OnPreparedListener and OnBufferingUpdateListener in your class

public class PlayerActivity extends Activity implements OnCompletionListener, OnPreparedListener, OnBufferingUpdateListener{
.
.
.

and implements all methodes

public void onPrepared(MediaPlayer mediaplayer) {
    if(!mediaPlayer.isPlaying()){
        mediaPlayer.start();
        Progressbar.setVisibility(View.INVISIBLE);
        play.setVisibility(View.GONE);
        stop.setVisibility(View.VISIBLE);
        songProgressBar.setProgress(0);
        songProgressBar.setMax(100);
        }
        updateProgressBar();      
}

@Override
public void onCompletion(MediaPlayer mediaPlayer) {
    //mediaPlayer.reset();
        songProgressBar.setProgress(0);
        songProgressBar.setSecondaryProgress(0);
        play.setVisibility(View.VISIBLE);
        stop.setVisibility(View.GONE);
        link = "http://server11.mp3quran.net/hawashi/002.mp3";
        playMp3(link);
}

@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
     songProgressBar.setSecondaryProgress(percent);

         if(percent==100)
         {
             Progressbar.setVisibility(View.INVISIBLE);

         }else if(percent > songProgressBar.getProgress())
         {
             Progressbar.setVisibility(View.INVISIBLE);
         }else
         {
             Progressbar.setVisibility(View.VISIBLE);
         }
}

I wish this helped you.

AwadKab
  • 3,054
  • 2
  • 17
  • 17
  • thank you this work perfect with me , i forget to check if mediaPlayer.isPlaying :) onPrepared – Bahaa Odeh Apr 03 '13 at 10:49
  • this for streaming url (RTSP (RTP, SDP) HTTP/HTTPS progressive streaming ) or I can use this for any mp3 download url ? say I have direct download url _http://blabla.com/example.mp3_ (if we put this url in firefox it will download) – LOG_TAG Dec 11 '13 at 07:41
  • I have a little problem with this code. mediaPlayer only plays first file in my ArrayList. After first file is played the onCompletionListener is being called multiple times (endless) – artouiros Mar 24 '14 at 06:01
  • @AwadKab how did u design your front end can u just share with us it will be helpful to all of them to check the code – Sandeep V Apr 23 '14 at 12:11
  • How can I save the stream that is just buffered into a file? Is there anyway to do this with this library only? or I need to download it separately? – Pejman Dec 20 '17 at 13:23