0

I am making a simple media player application. It involes a textfield (et) where user just enters the exact name of the song to be played and presses the play button (ib1) to be played. Songs are saved inside sdcard. My code is:

ib1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final String value = et.getText().toString();
            String full_path = "/mnt/media_rw/sdcard/mymusic/" + value + ".mp3";
            et.setText("");

            mp = new MediaPlayer();                     
            mp.setDataSource(full_path);
            mp.prepare();
            mp.start();                             


} });

But this produces Media Player error (-38.0). So, following Media Player called in state 0, error (-38,0) I tried to replace the mp lines with:

mp.setDataSource(full_path); 
mp.setOnPreparedListener(null);
mp.prepareAsync();
mp.start();

But it won't work either, producing error (1, -2147483648). Can someone help me or make some suggestions for the code? Thanks a lot

Community
  • 1
  • 1

1 Answers1

0

You need to call mediaPlayer.start() in the onPrepared method by using a listener. You are getting this error because you are calling mp.start() before it has reached the prepared state.

Here is how you can do it :

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

public void onPrepared(MediaPlayer player) {
player.start();
}
Vinod Kumar
  • 312
  • 4
  • 10
  • thanks for help but I still haven't understood how to do this. Should I write a method onPrepared and, after mp.prepareAsync();, to call this method by saying onPrepared(mp); ? –  Mar 02 '14 at 19:58
  • mp.setOnPreparedListener(new OnPreparedListener() { @override public void onPrepared(MediaPlayer player){ player.start();}); add these lines after mp.prepareAsync(); , i think you got the solution – Vinod Kumar Mar 04 '14 at 18:32