如何使用计时器录制音频文件

时间:2016-10-12 13:42:37

标签: android audio-recording countdowntimer

嗨,我是Android的新手,在我的应用程序中,我必须使用计时器录制音频,就像我的下图一样,使用我的下面我能录制音频,但如何在计时器的帮助下做这个场景请帮我一些

我的代码: -

 @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.record_button:
             startRecording()
                break;

            case R.id.stop_button:
                break;

        }
    }


    private void startRecording() {

        try {

            mRecorder = new MediaRecorder();
            mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            mRecorder.setOutputFile(mFileName);
            mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            mRecorder.prepare();
            mRecorder.start();

        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

    private void stopRecording() {

        mRecorder.stop();
        mRecorder.release();
        mRecorder = null;
    }

enter image description here

2 个答案:

答案 0 :(得分:0)

只需在startRecording中启动计时器,然后在stopRecording中停止。请参阅:https://stackoverflow.com/a/3734070/2324204

答案 1 :(得分:0)

您可以使用Android中已经存在的Chronometer

注意:Chronometer是扩展TextView的小部件,因此用Chronometer替换当前的TextView。

示例代码为:

//....
    Chronometer simpleChronometer = (Chronometer) findViewById(R.id.simpleChronometer);

//...



private void startRecording() {

    try {

        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setOutputFile(mFileName);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mRecorder.prepare();
        mRecorder.start();
        simpleChronometer.start(); // start a chronometer
        //simpleChronometer.setFormat("Time Running - %s"); // set the format  for a chronometer

    } catch (Throwable t) {
        t.printStackTrace();
    }
}

private void stopRecording() {

    mRecorder.stop();
    mRecorder.release();
    mRecorder = null;
    simpleChronometer.stop();
}

如果要手动执行操作,可以使用Stopwatch类。