Recording data using microphone for a fixed duration

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.

Answers


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)


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();
            }

        }

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


Need Your Help

Given a 2-3 tree, How can we find a possible insertion order?

algorithm data-structures insert tree

so say you are given a 2-3 tree, complete with nodes and a couple of levels. How could you find one of the several possible insertion orders/sequences that give you the resulting tree?