Question

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

Was it helpful?

Solution

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();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top