0

I need to download a mp3 file from a URL, and then play it.

My code is as below:

public class Mp3player extends Activity {

 private Button buttonPlayStop;
 private MediaPlayer mediaPlayer;
 private SeekBar seekBar;

 private final Handler handler = new Handler();
 static final String AUDIO_PATH =
          "http://sound18.mp3slash.net/indian/abcd/[Songs.PK]%20ABCD%20-%2002%20-%20Bezubaan.mp3";
 // Here i override onCreate method.
 //
 // setContentView() method set the layout that you will see then
 // the application will starts
 //
 // initViews() method i create to init views components.
 @Override
 public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    initViews();

 }

 // This method set the setOnClickListener and method for it (buttonClick())
 private void initViews() {
    buttonPlayStop = (Button) findViewById(R.id.ButtonPlayStop);
    buttonPlayStop.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            buttonClick();

        }
    });

    try {
        playAudio(AUDIO_PATH);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    seekBar = (SeekBar) findViewById(R.id.SeekBar01);
    seekBar.setMax(mediaPlayer.getDuration());
    seekBar.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            seekChange(v);
            return false;
        }

    });

 }
 private void playAudio(String url) throws Exception
 {
    killMediaPlayer();

    mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mediaPlayer.setDataSource(url);
    mediaPlayer.prepare();
 //    mediaPlayer.start();
 }
  private void killMediaPlayer() {
        if(mediaPlayer!=null) {
            try {
                mediaPlayer.release();
            }
            catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
 public void startPlayProgressUpdater() {
    seekBar.setProgress(mediaPlayer.getCurrentPosition());

    if (mediaPlayer.isPlaying()) {
        Runnable notification = new Runnable() {
            public void run() {
                startPlayProgressUpdater();
            }
        };
        handler.postDelayed(notification, 1000);
    } else {
        mediaPlayer.pause();
        buttonPlayStop.setText(getString(R.string.app_name));
        seekBar.setProgress(0);
    }
 }

 // This is event handler thumb moving event
 private void seekChange(View v) {
     if (mediaPlayer.isPlaying()) {
        SeekBar sb = (SeekBar) v;
        mediaPlayer.seekTo(sb.getProgress());
    }
 }

 // This is event handler for buttonClick event
 private void buttonClick() {
    if (buttonPlayStop.getText() == getString(R.string.app_name)) {
        buttonPlayStop.setText(getString(R.string.hello_world));
        try {
            mediaPlayer.start();
            startPlayProgressUpdater();
        } catch (IllegalStateException e) {
            mediaPlayer.pause();
        }
    } else {
        buttonPlayStop.setText(getString(R.string.app_name));
        mediaPlayer.pause();
    }
}
}

But i get error as below:

 02-05 12:01:46.690: E/MediaPlayer(343): pause called in state 8
 02-05 12:01:46.690: E/MediaPlayer(343): error (-38, 0)
 02-05 12:01:46.761: E/MediaPlayer(343): Error (-38,0)
 02-05 12:01:50.421: E/MediaPlayer(343): start called in state 0
 02-05 12:01:50.421: E/MediaPlayer(343): error (-38, 0)
 02-05 12:01:50.421: E/MediaPlayer(343): pause called in state 0
 02-05 12:01:50.421: E/MediaPlayer(343): error (-38, 0)
 02-05 12:01:50.451: E/MediaPlayer(343): Error (-38,0)
 02-05 12:01:50.451: E/MediaPlayer(343): Error (-38,0)
rupesh
  • 2,865
  • 4
  • 24
  • 50
  • Please post your logcat here as text, not a link to a picture. – Melquiades Feb 04 '14 at 08:08
  • Think that the user doesnt know how to copy log details. For those, First select the log error lines you need and then click to Save button (next to dropdown), and save it wherever you want, and from that exported text file copy the contents and display it here. – Chintan Soni Feb 04 '14 at 08:14

2 Answers2

0

you are trying to start the playing before the preparation is complete. Use the setOnPreparedListener() method to set a preparation listener and call the start() method only after the preparation is complete.

mp.setDataSource(url); 
mp.setOnPreparedListener(this);
mp.prepareAsync();

public void onPrepared(MediaPlayer player) {
    player.start();
}

for more details check this answer.

edited: example of media player

Community
  • 1
  • 1
Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
0

I think you are wrong here

if (mediaPlayer.isPlaying()) {
    // you should pause only if its playing
 }

you are doing it in else part

Viswanath Lekshmanan
  • 9,945
  • 1
  • 40
  • 64