/**
*
* 使用意图捕获音频
*
* @time 下午12:58:03
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class MediaRecorderDemoActivity extends Activity implements OnCompletionListener, OnClickListener {
private static final int RECORD_REQUEST = 0;
private Button btn_play, btn_record;
private Uri audioFileUri;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findView();
}
/**
* 实例化控件
*/
private void findView() {
btn_record = (Button) this.findViewById(R.id.btn_record_audio);
btn_record.setOnClickListener(this);
btn_play = (Button) this.findViewById(R.id.btn_play_audio);
btn_play.setOnClickListener(this);
btn_play.setEnabled(false);
}
@Override
public void onClick(View v) {
if (v == btn_record) {
Intent intent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
startActivityForResult(intent, RECORD_REQUEST);
} else if (v == btn_play) {
MediaPlayer mediaPlayer = MediaPlayer.create(this, audioFileUri);
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.start();
btn_play.setEnabled(false);
}
}
@Override
public void onCompletion(MediaPlayer mp) {
btn_play.setEnabled(true);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
audioFileUri = data.getData();
btn_play.setEnabled(true);
}
}
}