`

android 录音

 
阅读更多

必须在AndroidManifest中设置相应的权限:android:name="android.permission.RECORD_AUDIO" 

1. 首先判定是否存在SD卡,并得到相应的路径 

/* 检测是否存在SD卡 */  

if (Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))

{

/* 得到SD卡得路径 */

mRecAudioPath = Environment.getExternalStorageDirectory();

/* 更新所有录音文件到List中 */

musicList(); }

2. 录音开始

/* 创建录音文件,第一个参数是文件名前缀,第二个参数是后缀,第三个参数是SD路径 */  

mRecAudioFile = File.createTempFile(strTempFile, ".amr", mRecAudioPath);

/* 实例化MediaRecorder对象 */

mMediaRecorder = new MediaRecorder();

/* 设置麦克风 */

mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);

/* 设置输出文件的格式 */

mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);

/* 设置音频文件的编码 */

mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);

/* 设置输出文件的路径 */

mMediaRecorder.setOutputFile(mRecAudioFile.getAbsolutePath());

/* 准备 */

mMediaRecorder.prepare();

/* 开始 */

mMediaRecorder.start();

 

3. 录音关闭

mMediaRecorder.stop(); 

4. 播放录音文件

Intent intent = new Intent();

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
/* 设置文件类型 */
intent.setDataAndType(Uri.fromFile(file), "audio");
startActivity(intent);

 

5. 过滤文件类型,实现FilenameFilter

class MusicFilter implements FilenameFilter
{
public boolean accept(File dir, String name)
{
return (name.endsWith(".amr"));
}

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics