开发者

When i pressed the bt2 the system freez

开发者 https://www.devze.com 2023-01-13 18:21 出处:网络
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!

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号