When i pressed the bt2 the system freez
When i press bt on the Player plays the song in a loop like normal. But when i pressed bt2 (Stop Button) the System Freez!
Main Class:
final Alarm alarm = new Alarm();
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Alarm alarm = new Alarm();
new MediaPlayer();
MediaPlayer mp = MediaPlayer.create(ParkTicketAlarm.this, R.raw.alarm);
Vibrator vi = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
alarm.execute(mp);
}
});
bt2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
alarm.开发者_Go百科turnoff();
}
})
AsyncTask Class:
MediaPlayer mp;
@Override
protected String doInBackground(MediaPlayer... params) {
mp = (MediaPlayer)params[0];
mp.setLooping(true);
mp.start();
return null;
}
void turnoff()
{
mp.reset();
Toast toast = new Toast(null);
toast.setText("go");
toast.show();
}
In the OnCLick change:
Alarm alarm = new Alarm();
to
alarm = new Alarm();
Don't instantiate a new instance, which could override the global variable you have set.
精彩评论