分享

12. 录音(MediaRecorder)&播放

 黎可图书馆 2013-10-11
1. 由于最容易忘记,先加上权限。
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

2. 开始录音方法。
private void record(String path){
this.recorder = new MediaRecorder();
this.recorder.setAudioSource(MediaRecorder.AudioSource.MIC);//音频来源是麦克风
this.recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);//设置格式
this.recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
this.recorder.setOutputFile(path);
try {
this.recorder.prepare();
} catch (Exception e) {
e.printStackTrace();
}
this.recorder.start();
}

3. 停止录音方法。
private void stop(){
this.recorder.stop();
this.recorder.release();
}

4. 播放方法。
private void play(String filePath){
Uri uri = Uri.fromFile(new File(filePath));
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(uri, "audio/mp3");
startActivity(intent);
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多