2

Possible Duplicate:
Improve Audio Recording quality in android?

I want to record audio to a 3gp file. I can do it without any problem and can hear the voice. But the voice i can hear is very slow and not clear. To increase voice quality i wrote a program but get java.lang.RuntimeException: start failed

public void onClick(View arg0) 
        {
            root=Environment.getExternalStorageDirectory();
            audiofile=new File(root,"sound.3gp");
            if(!audiofile.exists())
            {
                Log.w(TAG, "File doesn't exists");
                try 
                {
                    audiofile.createNewFile();

                } catch (IOException e) 
                {
                    Log.w(TAG, "Unable to create audio file",e);
                }                   
            }

            recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);           
            recorder.setOutputFile(audiofile.getAbsolutePath());

            try 
            {
                recorder.setAudioSamplingRate(10);
                recorder.setAudioEncodingBitRate(20);
                recorder.prepare();

            } catch (IllegalStateException e) 
            {
                Log.w(TAG, "This is IllegalStateException");
            } catch (IOException e) 
            {
                Log.w(TAG, "This is IoException");
            }
            recorder.start();
        }

what is wrong in my code? thanks.

Community
  • 1
  • 1
Tauhidul Alam
  • 103
  • 3
  • 4
  • 9

2 Answers2

1

As per my Existing Answer on Stackoverflow I provided you the below code to improve your sound quality.

recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setAudioEncoder(MediaRecorder.getAudioSourceMax());
recorder.setAudioEncodingBitRate(16);
recorder.setAudioSamplingRate(44100);
recorder.setOutputFile(path);
recorder.prepare();
recorder.start();
Community
  • 1
  • 1
Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
  • now i get an exception java.lang.IllegalArgumentException: Invalid audio encoder. – Tauhidul Alam Jun 11 '12 at 15:10
  • Without using my code. Is that working? – Praveenkumar Jun 11 '12 at 15:15
  • Okay. Just remove `recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);` And, try to run – Praveenkumar Jun 11 '12 at 15:23
  • Okay just increase the size of your `setAuidoEncodingBitRate()` and `setAudioSamplingRate()` as in my answer. – Praveenkumar Jun 12 '12 at 04:17
  • 2
    The problem with @Paraveen's code is this line `recorder.setAudioEncoder(MediaRecorder.getAudioSourceMax());` There was already a call to `recorder.setAudioEncoder()` on the previous line. It doesn't make sense to call it with this parameter. – pents90 Feb 18 '15 at 22:44
0

i wrote a program but get java.lang.RuntimeException: start failed

Did u make sure at first run of your application you called in onStop

recorder.stop();
recorder.reset();
recorder.release();

I am pretty sure recorder is already in used when you are trying to run this program.thats why you are getting this error.always make sure you call above methods in onStop.Same goes true for Camera also.

Vipul
  • 27,808
  • 7
  • 60
  • 75