I am trying to make a simple application of a music player with simple ImageButtons for play and pause.
I tried doing it but from the emulator, at run-time, an error appears “unfortunately not able to open the appication.”
I tried with only the code for play button and it worked but when the code for pause button is added the run-time error occurs. Can anyone tell me where I am going wrong?
Thank you.
public class Audio1Activity extends Activity {
private static final String TAG = "AudioDemo";
private static final String isPlaying = "Media is Playing";
private static final String notPlaying = "Media has stopped Playing";
private MediaPlayer player;
ImageButton playbtn,pausebtn;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
player= MediaPlayer.create(this , R.raw.a1);
playbtn=(ImageButton) this.findViewById(R.id.image_button1);
player.setLooping(false);
playbtn.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
demoPlay();
}
});
pausebtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
demoPause();
}
});
@Override
public void onPause() {
super.onPause();
player.pause();
}
private void demoPause(){
player.pause();
Toast.makeText(this, notPlaying, Toast.LENGTH_LONG).show();
Log.d(TAG, notPlaying);
}
private void demoPlay(){
player.start();
Toast.makeText(this, isPlaying, Toast.LENGTH_LONG).show();
Log.d(TAG, isPlaying);
}
}
The error is you are not setting the
pausebtnto be of theImageButtonwidget.Something like this should do it: