tags:

views:

14

answers:

1

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.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();
}
A: 

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.

Ryan Ternier
thanks... i made the alarm global.... but now the sound stopp but the system also freez....
Hans Wurst
ohhh i deleted the toast and it worked.... THANKS
Hans Wurst
Glad it's working for ya.
Ryan Ternier