单击按钮进行录音

时间:2015-04-25 11:15:19

标签: android button voice-recording

我正在开发一个我正在使用录音功能的Android应用程序,为此我正在按下录音按钮,如果用户按住按钮超过3秒,那么它将允许录音。 / p>

为此我使用的是Touch监听器,但问题是如果用户只是单击按钮而不保持按住,则会调用录制或功能。我的代码如下:

mBtnSendAudio.setOnTouchListener(new OnTouchListener() {

            boolean shouldRecord = true;

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                if(!canChat) {
                    Toast.makeText(getBaseContext(), "Turned Off Chat", Toast.LENGTH_SHORT).show();
                    return true;
                }

                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    mLastDown = System.currentTimeMillis();
                    shouldRecord = true;
                    startRecording();
                    mLLAudioRecordingHover.setVisibility(View.VISIBLE);
                    rectAudioButton = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
                    mStartTime = System.currentTimeMillis();
                    mRecordHandler.removeCallbacks(mUpdateTimeTask);
                    mRecordHandler.postDelayed(mUpdateTimeTask, 100);

                } else if (event.getAction() == MotionEvent.ACTION_UP) {

                    if (shouldRecord) {
                        mLLAudioRecordingHover.setVisibility(View.GONE);
                        mLastDuration = System.currentTimeMillis() - mLastDown;
                        mRecordHandler.removeCallbacks(mUpdateTimeTask);
                        mLLAudioRecordingHover.setVisibility(View.GONE);
                        stopRecording(true, mLastDuration);
                    }
                }

                if (event.getAction() == MotionEvent.ACTION_MOVE) {

                    // finger move outside of button's bounds
                    if (!rectAudioButton.contains(v.getLeft() + (int) event.getX(), v.getTop()+ (int) event.getY()) && shouldRecord) {
                        mRecordHandler.removeCallbacks(mUpdateTimeTask);
                        shouldRecord = false;
                        stopRecording(shouldRecord, 0);
                        mLLAudioRecordingHover.setVisibility(View.GONE);
                    }
                }
                return v.onTouchEvent(event);
            }
        });

2 个答案:

答案 0 :(得分:1)

尝试如下

mBtnSendAudio.setOnTouchListener(new View.OnTouchListener() {

    @Override
    public boolean onTouch(View arg0, MotionEvent arg1) {
        switch (arg1.getAction()) {
        case MotionEvent.ACTION_DOWN:
            mRecordHandler.postDelayed(run, 3500/* OR the amount of time you want */);
            break;

        default:
            mRecordHandler.removeCallbacks(run);
            break;

        }
        return true;
    }
});

这里是Runnable来改变录音状态

Runnable run = new Runnable() {

@Override
public void run() {
     shouldRecord = true;

}

};

更新:

然后你必须使用 GestureDetector ,如下所示

private GestureDetector gestureDetector = new GestureDetector(this, new SimpleOnGestureListener {

    @Override
    public boolean onSingleTapConfirmed(MotionEvent event) {
        return true;
    }
});

然后在OnTouchListener的onTouch( - , - )

 if (gestureDetector.onTouchEvent(arg1)) {
                // single tap
              //write your code for single click
                return true;
     } 

希望它会对你有所帮助。

答案 1 :(得分:0)

你有两个选择:首先你可以忘记3秒并使用View.OnLongClickListener,它是大多数开发人员用来检测按住事件的标准监听器,或者你可以在{{{这里的条件是3秒计时器的代码:

if (event.getAction() == MotionEvent.ACTION_DOWN) {}

new CountDownTimer(3000, 1000) { public void onTick(long millisUntilFinished) { } public void onFinish() { //your caode } }.start(); 方法中使用您想要的功能。