Audio recording

I am new to Android and I tried to create an application for recording audio data. I am using the Eclipse Galileo IDE with the ADT plugin. And my application is for the Andriod 2.1 platform.

Unfortunately, the example provided in the Dev manual raises many exceptions.

For example: to get the MIME type, the code uses recorder.getMimeContentType(). But this method does not exist in my version of the class MediaRecorder.

I searched online as well as this forum and came up with one or two alternatives that show how to record audio and put it in an EXISTING file. But ideally, I want the code in the developer guide to work.

Or is it even better if I can record audio and store it directly in an array of bytes?

I spent a lot of time trying to get this to work, but with little success :(

I would really appreciate if someone would show me how to achieve sound recording.

+3
source share
4 answers

Check this article . It has sample code for recording an audio fragment.

+2
source

It has sample code for recording audio.


 b1=(Button)findViewById(R.id.button1);
    b2=(Button) findViewById(R.id.button2);
    mr=new MediaRecorder();
    b1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            try{
            b1.setEnabled(false);
            b2.setEnabled(true);
            b2.requestFocus();

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

        @Override
        public void onClick(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);
 }


  protected  void start() throws Exception
{
   mr.setAudioSource(MediaRecorder.AudioSource.MIC);
   mr.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
   mr.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
   if (audiofile == null) {
          File sampleDir = 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();


}


 protected void stop() throws Exception{
mr.stop();
mr.release();
}



protected void processaudiofile() {
    ContentValues values = new ContentValues(4);
    long current = 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());
    ContentResolver contentResolver = getContentResolver();

    Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    Uri newUri = contentResolver.insert(base, values);

    // this does not always seem to work cleanly....
    sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
}
+2
source

, :

MediaRecorder recorder;

void startRecording() throws IOException 
{
SimpleDateFormat timeStampFormat = new SimpleDateFormat(
"yyyy-MM-dd-HH.mm.ss");
String fileName = "audio_" + timeStampFormat.format(new Date())
+ ".mp4";
recorder = new MediaRecorder();  
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();
}

protected void stopRecording() {
recorder.stop();
recorder.release();
}
+1

Source: https://habr.com/ru/post/1779160/


All Articles