Tuesday, May 31, 2011

Start audio recording with intent of MediaStore.Audio.Media.RECORD_SOUND_ACTION

Start audio recording with intent of MediaStore.Audio.Media.RECORD_SOUND_ACTION
Start audio recording with intent of MediaStore.Audio.Media.RECORD_SOUND_ACTION
package com.exercise.AndroidIntentAudioRecording;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class AndroidIntentAudioRecording extends Activity {

final static int RQS_RECORDING = 1;

Uri savedUri;

Button buttonRecord;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonRecord = (Button)findViewById(R.id.record);

buttonRecord.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent =
new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
startActivityForResult(intent, RQS_RECORDING);
}});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if(requestCode == RQS_RECORDING){
savedUri = data.getData();
Toast.makeText(AndroidIntentAudioRecording.this,
"Saved: " + savedUri.getPath(),
Toast.LENGTH_LONG).show();
}
}
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/record"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Record"
/>
</LinearLayout>


Download the files.

Related:
- Play audio using MediaPlayer

8 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hi,
    I tried using the sample provided. After i record in the activity result the intent data is null. Am i missing out on something? I am testing on Android 2.2.1 (Samsung Galaxy POP).

    Thanking you,
    Regards,
    Sam

    ReplyDelete
  3. I also want to play the recorded audio. Can you tell me how to do that?

    ReplyDelete
  4. I can't find the recorded file on my device...

    ReplyDelete
  5. Hi!

    I'm using this recorder on a samsung tablet, and I have two problems with it:
    - if I tap the screen anywhere other than the recording popup, the recording stops and the popup disappears.
    - I can't define a maximum recording length (I tried "intent.putExtra("android.intent.extra.durationLimit", 120);", but it doesn't work)

    Do you know a way to solve these, or do I have to write my own voice-recorder popup+functionality?

    ReplyDelete
  6. A year later, but still. There is no extra for duration, but there is a max file size extra : http://developer.android.com/reference/android/provider/MediaStore.Audio.Media.html#EXTRA_MAX_BYTES

    ReplyDelete
  7. Dear,
    Can we get output file types before we start record.
    Thanks

    ReplyDelete
  8. Hi
    Can anybody plz guide me for How I can get this app into the cellphone ? I might be skipping a step. Please help.
    thanks,

    ReplyDelete