设置按钮声音开始前的等待时间

时间:2017-05-29 14:26:55

标签: android-studio

检查按钮时设置了声音

    public void Button(View v) {  

        final MediaPlayer mpStart = MediaPlayer.create(this, R.raw.startsound); 
        mpStart.start();
        }

我想在声音开始前等一段时间。 我该怎么办?

1 个答案:

答案 0 :(得分:1)

一种解决方案是使用Handler.postDelayed()方法。

@Override 
public void onClick(View v) {

// This solution will leak memory!  Actually don't use!!!
Handler handler = new Handler(); 
handler.postDelayed(new Runnable() {
     @Override 
     public void run() { 

          final MediaPlayer mpStart = MediaPlayer.create(this, R.raw.startsound); 
          mpStart.start();
     } 
}, 2000); //Here change the time to wait in milliseconds 
}

有关详细信息,请点击此处:Pause a process