Skip to content Skip to sidebar Skip to footer

Audio Recording

I am a newbie in Android and I was trying to build an app to record audio data. I am using Eclipse Galileo IDE with ADT plugin. And my app is targetted for the Andriod 2.1 platform

Solution 1:

It has example code for recording an audio


 b1=(Button)findViewById(R.id.button1);
    b2=(Button) findViewById(R.id.button2);
    mr=newMediaRecorder();
    b1.setOnClickListener(newOnClickListener() {

        @OverridepublicvoidonClick(View arg0) {
            // TODO Auto-generated method stubtry{
            b1.setEnabled(false);
            b2.setEnabled(true);
            b2.requestFocus();

                start();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });
    b2.setOnClickListener(newOnClickListener() {

        @OverridepublicvoidonClick(View arg0) {
            try{
            b1.setEnabled(true);
            b2.setEnabled(false);
            b1.requestFocus();

                stop();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            processaudiofile();


        }
    });

    b2.setEnabled(false);
    b1.setEnabled(true);
 }


  protectedvoidstart()throws Exception
{
   mr.setAudioSource(MediaRecorder.AudioSource.MIC);
   mr.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
   mr.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
   if (audiofile == null) {
          FilesampleDir= Environment.getExternalStorageDirectory();

          try { 
              audiofile = File.createTempFile("Record", ".mp3", sampleDir);
          } 
          catch (IOException e)
          {
              Log.e("abc","sdcard access error");
              return;
          }
  }

     mr.setOutputFile(audiofile.getAbsolutePath());

   mr.prepare();
    mr.start();


}


 protectedvoidstop()throws Exception{
mr.stop();
mr.release();
}



protectedvoidprocessaudiofile() {
    ContentValuesvalues=newContentValues(4);
    longcurrent= System.currentTimeMillis();

    values.put(MediaStore.Audio.Media.TITLE, "audio" + audiofile.getName());
    values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
    values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp");
    values.put(MediaStore.Audio.Media.DATA, audiofile.getAbsolutePath());
    ContentResolvercontentResolver= getContentResolver();

    Uribase= MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    UrinewUri= contentResolver.insert(base, values);

    // this does not always seem to work cleanly....
    sendBroadcast(newIntent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
}

Solution 2:

You can try this code, for me it works well:

MediaRecorder recorder;

voidstartRecording()throws IOException 
{
SimpleDateFormattimeStampFormat=newSimpleDateFormat(
"yyyy-MM-dd-HH.mm.ss");
StringfileName="audio_" + timeStampFormat.format(newDate())
+ ".mp4";
recorder = newMediaRecorder();  
recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
recorder.setOutputFile("/sdcard/"+fileName);
recorder.prepare(); 
recorder.start();
}

protectedvoidstopRecording() {
recorder.stop();
recorder.release();
}

Solution 3:

Post a Comment for "Audio Recording"