本文主要是介绍android多媒体部分学习笔记六----音频录制 mediaRecorder,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
/***
* 使用意图捕获音频
*
* @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);
}
}
}
这篇关于android多媒体部分学习笔记六----音频录制 mediaRecorder的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!