1

I am making a simple audio recording application, I want the all audio file to have the same duration that's why I followed this article using post delayed handler to make stopRecording automatically active after 3000 milisecond. Here is my current code to start recording:

@Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btnStart: {
                    AppLog.logString("Start Recording");
                    startRecording();

                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            stopRecording();
                            enableButtons(false);
                            AppLog.logString("Stop Recording");
                            Toast.makeText(MainActivity.this, "File name: " + getFilename(),
                                    Toast.LENGTH_SHORT).show();
                        }
                    }, 3000);

                    break;
                }


            }
        }

all audio files are stored in internal memory, this is a picture for the all audio files that I recorded: sample sound

my question is: are all the audio files (sampletest1.wav - sampletest6.wav) have a same duration? even though the size of the audio file is different? and why did this happen?

1 Answers1

1

The simple answer is, you won't get the accuracy you're expecting using timers such as postDelayed. The files you've shown are different in length; the difference between the longest (519 KB) and shortest (512 KB) is about 40 milliseconds.

Why are they different? Because the processor, which is measuring the 3000 ms and calling your handler, is doing a lot of other work too, servicing the operating system and other applications.

Incidentally, given the 44.1 kHz sample rate and the sizes shown, I guess the sample size is 32 bits. Exactly 3 seconds of audio would have a data size of:

44100 x 4 x 3 = 529,200 bytes

(ignoring the WAV header, which is normally only about 44 bytes). This is 516.8 KB.

Ian
  • 1,221
  • 1
  • 18
  • 30