Recording data using microphone for a fixed duration

天涯浪子 提交于 2019-12-23 04:32:50

问题


I would like to know if there is a way to fix the duration of recording using mobile's microphone. Like when I click a button the recording should start and it should stop after 5 seconds on its own, what method do you propose me to use :-)

Edit:

Sorry for the confusion but I am using AudioRecorder class to record data and I don't think the MediaRecorder class function works properly (/at all) for the same.


回答1:


If you just use a timer, I do not think that you can accurately control how much data is within the buffer when your app reads it.

I think they way to record 5 seconds of audio data is to use the technique from this class.

The code there carefully sets the size of the audio buffer so that it will call back after it has recorded data for a certain amount of time. Here is a snipped from that class.

public boolean startRecordingForTime(int millisecondsPerAudioClip,
            int sampleRate, int encoding)
    {
        float percentOfASecond = (float) millisecondsPerAudioClip / 1000.0f;
        int numSamplesRequired = (int) ((float) sampleRate * percentOfASecond);
        int bufferSize =
                determineCalculatedBufferSize(sampleRate, encoding,
                        numSamplesRequired);

        return doRecording(sampleRate, encoding, bufferSize,
                numSamplesRequired, DEFAULT_BUFFER_INCREASE_FACTOR);
    }

Then later on your code just does this:

while (continueRecording)
        {
            int bufferResult = recorder.read(readBuffer, 0, readBufferSize);
//do stuff
        }

since readBufferSize is just right, you will get the amount of data you want (with some slight variation)




回答2:


This is all what you need.

@Override
    public void onClick(View view)
    {
        if (view.getId() == R.id.Record)
        {
            new Timer().schedule(new TimerTask()
            {

                @Override
                public void run()
                {
                    runOnUiThread(new Runnable()
                    {

                        @Override
                        public void run()
                        {
                            mediaRecorder.stop();
                            mediaRecorder.reset();
                            mediaRecorder.release();
                            files.setEnabled(true);
                            record.setEnabled(true);
                            stop.setEnabled(false);

                        }
                    });

                }
            }, 5000);

            record.setEnabled(false);
            files.setEnabled(false);
            stop.setEnabled(true);
            try
            {
                File file = new File(Environment.getExternalStorageDirectory(),
                        "" + new Random().nextInt(50) + ".3gp");
                adapter.add(file.getAbsolutePath());
                adapter.notifyDataSetChanged();
                mediaRecorder = new MediaRecorder();
                mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                mediaRecorder
                        .setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                mediaRecorder
                        .setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                mediaRecorder.setOutputFile(file.getAbsolutePath());
                mediaRecorder.prepare();
                mediaRecorder.start();
                stop.setEnabled(true);
            } catch (IllegalStateException e)
            {
                e.printStackTrace();
            } catch (IOException e)
            {
                e.printStackTrace();
            }

        }



回答3:


Use setMaxDuration from MediaRecorder class.

alternately

When you start recording start a new thread and put it to sleep for 5 seconds. when it wakes stop the recording. or use a timertask which shall call the stop recording after 5 second delay. or



来源:https://stackoverflow.com/questions/10930853/recording-data-using-microphone-for-a-fixed-duration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!