Mega Code Archive

 
Categories / Android / Core Class
 

Using Intent to record audio

package app.test; import android.app.Activity; import android.content.Intent; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class Test extends Activity implements OnClickListener,     OnCompletionListener {   public static int RECORD_REQUEST = 0;   Button createRecording, playRecording;   Uri audioFileUri;   @Override   public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.main);     createRecording = (Button) this.findViewById(R.id.RecordButton);     createRecording.setOnClickListener(this);     playRecording = (Button) this.findViewById(R.id.PlayButton);     playRecording.setOnClickListener(this);     playRecording.setEnabled(false);   }   public void onClick(View v) {     if (v == createRecording) {       Intent intent = new Intent(           MediaStore.Audio.Media.RECORD_SOUND_ACTION);       startActivityForResult(intent, RECORD_REQUEST);     } else if (v == playRecording) {       MediaPlayer mediaPlayer = MediaPlayer.create(this, audioFileUri);       mediaPlayer.setOnCompletionListener(this);       mediaPlayer.start();       playRecording.setEnabled(false);     }   }   protected void onActivityResult(int requestCode, int resultCode, Intent data) {     if (resultCode == RESULT_OK && requestCode == RECORD_REQUEST) {       audioFileUri = data.getData();       playRecording.setEnabled(true);     }   }   public void onCompletion(MediaPlayer mp) {     playRecording.setEnabled(true);   } } //main.xml <?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"     > <Button android:text="Record Audio" android:id="@+id/RecordButton"  android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <Button android:text="Play Recording" android:id="@+id/PlayButton"  android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout>