如何在录制时在语音便笺上设置计时器(与WhatsApp语音消息录制功能相同)?

时间:2019-06-18 05:34:30

标签: java android

我正在构建一个具有语音消息功能的聊天应用程序,但在录制时无法获得“ WhatsApp”之类的感觉或计时器,因此任何人都可以使用此解决方案 我尝试了开发人员Android的代码,但可以找到这样的解决方案。

btn_voice.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction()==MotionEvent.ACTION_DOWN)
{
 startRecording();
}
else if(event.getAction()==MotionEvent.ACTION_UP)
{
    stopRecording();
}
return false;
}
});

2 个答案:

答案 0 :(得分:1)

创建处理程序

 private Handler customHandler = new Handler();

然后在startRecording(){starttimer();}

然后

 private void starttimer() {
        startTime = SystemClock.uptimeMillis();
        customHandler.postDelayed(updateTimerThread, 0);
    //    customHandler.removeCallbacks(updateTimerThread);

    }

 private Runnable updateTimerThread = new Runnable() {
        @SuppressLint({"SetTextI18n", "DefaultLocale"})
        public void run() {
            timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
            updatedTime = timeSwapBuff + timeInMilliseconds;
            int secs = (int) (updatedTime / 1000);
            int mins = secs / 60;
            secs = secs % 60;
            int milliseconds = (int) (updatedTime % 1000);
            dialTimer.setText("" + mins + ":"+String.format("%02d", secs) + ":"+String.format("%03d", milliseconds));
            customHandler.postDelayed(this, 0);
        }
    };

并且要停止计时器

customHandler.removeCallbacks(updateTimerThread);

答案 1 :(得分:0)

您也可以参考这些.. Linear Timer是Android的自定义视图,可启用循环进度动画 https://github.com/krtkush/LinearTimer