Atharva Muley Atharva Muley - 2 months ago 18
Android Question

Error on Android Media player

Currently I am working on a music Player. Please help me out with these errors

public class MainActivity extends Activity {

Button playb,pauseb,stopb;

SeekBar seeker;
Handler seekhandler;
MediaPlayer player;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

player=new MediaPlayer();//.create(MainActivity.this, R.raw.spaceman);

//Interface between code and xml
playb=(Button)findViewById(R.id.play_button);
stopb=(Button)findViewById(R.id.stop_button);
pauseb=(Button)findViewById(R.id.pause_button);
seeker=(SeekBar)findViewById(R.id.seekBar);


try {
player.prepare();
} catch (IOException e) {
Toast.makeText(getApplicationContext(),(CharSequence) e,Toast.LENGTH_SHORT ).show();
}
//Code for Play Button
playb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view){
player.stop();
player.reset();
seeker.setProgress(0);
try {
player.prepare();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), (CharSequence) e,Toast.LENGTH_SHORT).show();

}
player.start();
seeker.setMax(player.getDuration());
}
});


//Code for Pause button
pauseb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
player.pause();
}
});

//code for Stop button
stopb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

player.reset();
}
});


//Code for seekbar change listner
seeker.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

}
});

Runnable runnable =new Runnable() {
@Override
public void run() {
seeker.setProgress(player.getCurrentPosition());
seekhandler.postDelayed(this,1000);

}
};
}
}


Errors:


09-19 20:16:15.342 25242-25242/main.deviac.com.seekbar E/AndroidRuntime: FATAL EXCEPTION: main
Process: main.deviac.com.seekbar, PID: 25242
java.lang.RuntimeException: Unable to start activity ComponentInfo{main.deviac.com.seekbar/main.deviac.com.seekbar.MainActivity}: java.lang.IllegalStateException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2452)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:152)
at android.app.ActivityThread.main(ActivityThread.java:5497)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalStateException
at android.media.MediaPlayer._prepare(Native Method)
at android.media.MediaPlayer.prepare(MediaPlayer.java:1158)
at main.deviac.com.seekbar.MainActivity.onCreate(MainActivity.java:42)
at android.app.Activity.performCreate(Activity.java:6285)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2405)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535) 
at android.app.ActivityThread.access$900(ActivityThread.java:154) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:152) 
at android.app.ActivityThread.main(ActivityThread.java:5497) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
09-19 20:16:15.344 25242-25242/main.deviac.com.seekbar D/AppTracker: App Event: crash
09-19 20:16:28.590 25409-25409/main.deviac.com.seekbar W/System: ClassLoader referenced unknown path: /data/app/main.deviac.com.seekbar-2/lib/arm64
09-19 20:16:28.669 25409-25409/main.deviac.com.seekbar W/System: ClassLoader referenced unknown path: /data/app/main.deviac.com.seekbar-2/lib/arm64


I have kept mp3 file(spaceman) in raw folder which resides in res directory

Answer

Welcome to Android's media player, or as I call it, "state-based hell".

Your original error was caused by preparing the media player, you don't prepare the media player if you play audio files locally, as the create() method (if you use this method to create the media player) automatically prepares. There were additional media player state-based errors but the code below should fix them.

Disregarding the seekbar code right now (I would get this basic play, pause, and stop code working before adding in the seekbar functionality), your code for the play, pause, and stop buttons should be like this:

    public class MainActivity extends Activity {

    Button playb, pauseb, stopb;

    MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Interface between code and xml
        playb = (Button) findViewById(R.id.play_button);
        stopb = (Button) findViewById(R.id.stop_button);
        pauseb = (Button) findViewById(R.id.pause_button);

        //Code for Play Button
        playb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.file_to_play.mp3);                
                mediaPlayer.start();

            }
        });


        //Code for Pause button
        pauseb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mediaPlayer != null && mediaPlayer.isPlaying()) {
                    mediaPlayer.pause();
                } else {
                    mediaPlayer.start();
                }

            }
        });

        //code for Stop button
        stopb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (mediaPlayer != null && mediaPlayer.isPlaying()) {
                    mediaPlayer.stop();
                    mediaPlayer.reset();
                }
            }
        });
}