Tuesday, May 31, 2011

Play audio using MediaPlayer

To play audio in Android, we can use MediaPlayer. Base on the audio file from last exercise "Start audio recording with intent of MediaStore.Audio.Media.RECORD_SOUND_ACTION", modify to add a Play button to start audio playback using MediaPlayer. Additionally, we have to implement MediaPlayer.OnCompletionListener() method to handle the OnCompletion event.

Play audio using MediaPlayer

package com.exercise.AndroidIntentAudioRecording;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
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, buttonPlay;

/** 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);
buttonPlay = (Button)findViewById(R.id.play);
buttonPlay.setEnabled(false);

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);
}});

buttonPlay.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
MediaPlayer mediaPlayer = MediaPlayer.create(AndroidIntentAudioRecording.this, savedUri);
mediaPlayer.setOnCompletionListener(completionListener);
mediaPlayer.start();
buttonRecord.setEnabled(false);
buttonPlay.setEnabled(false);
}});
}

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

}
}

MediaPlayer.OnCompletionListener completionListener
= new MediaPlayer.OnCompletionListener(){

@Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
buttonRecord.setEnabled(true);
buttonPlay.setEnabled(true);
}};

}


<?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"
/>
<Button
android:id="@+id/play"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Play"
/>
</LinearLayout>



Download the files.

1 comment:

chilltime said...

Nice tutorial. This question I have is regarding to the Oncompletion listener. I am using it for a video view. I need to add a condition statement within a oncompletionlistener which will allow me start a new activity when one of the button is pressed. This is what I have so far


Button using = (Button) findViewById(R.id.use);
Button retaking = (Button) findViewById(R.id.retake);



videoView.setOnCompletionListener(new OnCompletionListener() { // <------- What to do when playback finishes

public void onCompletion(MediaPlayer mp) {

if(using.isClickable()){
using.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
finish();
startActivity(new Intent("com.apapa.vrsixty.main"));
}
});

}


else
if
(retaking.isClickable()){
retaking.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
finish();
startActivity(new Intent("com.apapa.vrsixty.record"));
}
});
}
}

});