Android view.playSoundEffect多次播放

时间:2014-09-27 20:44:58

标签: android onclicklistener playsound

所以我有一个onClickListener来启动计时器。我添加了代码来播放Android“点击”声音,但有时当按下视图时,声音会多次播放为“点击”。 click事件生成的信息被保存到文件中,我没有收到那里多次点击的证据,但我确实听到了它的声音。我在if语句中移动了声音,这有点解决了这个问题,但仍有一些多次点击。看起来它们通常是用我的手指垫与尖端按压时发生的。

public OnClickListener lapListener = new OnClickListener() 
{    
    @Override
    public void onClick(View view) {

       //if new timer = 0 the timer is not running. Start the clock, and make necessary changes
       if(newTimer == 0)
       {
           view.playSoundEffect(android.view.SoundEffectConstants.CLICK);
            startTime = SystemClock.uptimeMillis();
            customHandler.postDelayed(updateTimerThread, 0);
            customHandler.postDelayed(updateLapThread, 0);
            newTimer = 1;
            running = 1;
            stopButton.setText("Stop"); 
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
       }
       //if newtimer = 1 and running = 1 the timer is going and the user wishes to end the current lap
       else if (newTimer == 1 && running == 1)
       {

           view.playSoundEffect(android.view.SoundEffectConstants.CLICK);
           customHandler.removeCallbacks(updateLapThread);
           finishLap();
           customHandler.postDelayed(updateLapThread, 5000);


       }
       else
       {
             //Do nothing   
       }


   }
};

0 个答案:

没有答案