Skip to content
Advertisement

Media player returns error and no sound plays

I have been testing my basic media player created for an app start up sound. The media is pulled from a URL contained within my firebase database. The media player works on an earlier emulators but not on any real device.

The error code is MediaPlayer start called in state 0 (Error -38, 0). I’ve read that it’s because the onprepare is not ready before playing the media, so I added a onPreparelistener and it still gives the same error. What can I try next?

public class harropMediaplayer {
MediaPlayer player;
String media;
Context c;
public harropMediaplayer(String media,Context c){
    this.media = media;
    this.c = c;
    Log.i("Sound: ","Initalized");

}

public void volumeSetting(){

    SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(c);

    boolean bnSoundMute = sharedpreferences.getBoolean("soundMute",false);

    if(bnSoundMute==true){
mutevol();
        Log.i("sound","muted");

    }else{
volmax();
        Log.i("sound","max vol");
    }
}
public void plysound() {



    player = new MediaPlayer();

    Log.i("Url", media);
    try {
        player.setDataSource(media);
    } catch (IOException e) {
        e.printStackTrace();
    }
    catch (IllegalStateException o){
        o.printStackTrace();
    }
    try {
        player.prepare();
        volumeSetting();
        player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            public void onPrepared(MediaPlayer mp) {
               if (!mp.isPlaying()){
                   mp.start();
               }

            }

        });


    } catch (IOException e) {
        e.printStackTrace();
    }

    Log.i("Sound playing", "Ok");




}

public void stopplying(){
    player.stop();

}
public void volmax(){
    player = App_Objects.mp;
    player.setVolume(1,1);
}
public void mutevol(){
    player = App_Objects.mp;
    player.setVolume(0,0);
} 

Advertisement

Answer

onPrepareListener is not the same of preparing, you need simply call:

mediaPlayer.prepare();
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement