android mediaRecorder详细讲解及使用
来源:互联网 发布:厦门seo分享 编辑:程序博客网 时间:2024/06/06 03:57
public class mediarecorder1 extends ListActivity { private Button mAudioStartBtn; private Button mAudioStopBtn; private File mRecAudioFile; // 录制的音频文件 private File mRecAudioPath; // 录制的音频文件路徑 private MediaRecorder mMediaRecorder;// MediaRecorder对象 private List<String> mMusicList = new ArrayList<String>();// 录音文件列表 private String strTempFile = "recaudio_";// 零时文件的前缀 @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.mymultimedia_mediarecorder1); mAudioStartBtn = (Button) findViewById(R.id.mediarecorder1_AudioStartBtn); mAudioStopBtn = (Button) findViewById(R.id.mediarecorder1_AudioStopBtn); /*按钮状态*/ mAudioStartBtn.setEnabled(true); mAudioStopBtn.setEnabled(false); /* 检测是否存在SD卡 */ if (Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) { mRecAudioPath = Environment.getExternalStorageDirectory();// 得到SD卡得路径 musicList();// 更新所有录音文件到List中 } else { Toast.makeText(mediarecorder1.this, "没有SD卡", Toast.LENGTH_LONG).show(); } /* 开始按钮事件监听 */ mAudioStartBtn.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View arg0) { try { /* ①Initial:实例化MediaRecorder对象 */ mMediaRecorder = new MediaRecorder(); /* ②setAudioSource/setVedioSource*/ mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);//设置麦克风 /* ②设置输出文件的格式:THREE_GPP/MPEG-4/RAW_AMR/Default * THREE_GPP(3gp格式,H263视频/ARM音频编码)、MPEG-4、RAW_AMR(只支持音频且音频编码要求为AMR_NB) * */ mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); /* ②设置音频文件的编码:AAC/AMR_NB/AMR_MB/Default */ mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); /* ②设置输出文件的路径 */ try { mRecAudioFile = File.createTempFile(strTempFile, ".amr", mRecAudioPath); } catch (Exception e) { e.printStackTrace(); } mMediaRecorder.setOutputFile(mRecAudioFile.getAbsolutePath()); /* ③准备 */ mMediaRecorder.prepare(); /* ④开始 */ mMediaRecorder.start(); /*按钮状态*/ mAudioStartBtn.setEnabled(false); mAudioStopBtn.setEnabled(true); } catch (IOException e) { e.printStackTrace(); } } }); /* 停止按钮事件监听 */ mAudioStopBtn.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub if (mRecAudioFile != null) { /* ⑤停止录音 */ mMediaRecorder.stop(); /* 将录音文件添加到List中 */ mMusicList.add(mRecAudioFile.getName()); ArrayAdapter<String> musicList = new ArrayAdapter<String>(mediarecorder1.this, R.layout.list, mMusicList); setListAdapter(musicList); /* ⑥释放MediaRecorder */ mMediaRecorder.release(); mMediaRecorder = null; /* 按钮状态 */ mAudioStartBtn.setEnabled(true); mAudioStopBtn.setEnabled(false); } } }); } /* 播放录音文件 */ private void playMusic(File file) { 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); } @Override /* 当我们点击列表时,播放被点击的音乐 */ protected void onListItemClick(ListView l, View v, int position, long id) { /* 得到被点击的文件 */ File playfile = new File(mRecAudioPath.getAbsolutePath() + File.separator + mMusicList.get(position)); /* 播放 */ playMusic(playfile); } /* 播放列表 */ public void musicList() { // 取得指定位置的文件设置显示到播放列表 File home = mRecAudioPath; if (home.listFiles(new MusicFilter()).length > 0) { for (File file : home.listFiles(new MusicFilter())) { mMusicList.add(file.getName()); } ArrayAdapter<String> musicList = new ArrayAdapter<String>(mediarecorder1.this, R.layout.list, mMusicList); setListAdapter(musicList); } } } /* 过滤文件类型 */ class MusicFilter implements FilenameFilter { public boolean accept(File dir, String name) { return (name.endsWith(".amr")); } }//布局文件<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:drawSelectorOnTop="false" />
<Button
android:id="@+id/mediarecorder1_AudioStartBtn"
android:layout_width="170px"
android:layout_height="wrap_content"
android:layout_x="30px"
android:layout_y="100px"
android:text="开始录制音频" />
<Button
android:id="@+id/mediarecorder1_AudioStopBtn"
android:layout_width="170px"
android:layout_height="wrap_content"
android:layout_x="210px"
android:layout_y="100px"
android:text="停止音频录制" />
</AbsoluteLayout>
0 0
- android mediaRecorder详细讲解及使用
- Android使用MediaRecorder实现录音及播放
- Android四大组件之一ContentProvider 的详细讲解及使用
- android-->>>mediarecorder的使用
- Android使用MediaRecorder录像
- Android中AudioManager和MediaRecorder讲解
- EventBus的详细讲解及使用
- android使用MediaRecorder实现录音
- Android 使用mediaRecorder进行录音
- Android:使用MediaRecorder录制音频
- Android Studio 使用MediaRecorder 录音
- Android Studio 使用SVN的详细讲解
- Android使用MediaRecorder和Camera实现视频录制及播放功能整理
- Android使用MediaRecorder和Camera实现视频录制及播放功能整理
- Android使用MediaRecorder和Camera实现视频录制及播放功能整理
- android ViewTreeObserver详细讲解
- android ViewTreeObserver详细讲解
- android task详细讲解
- 快速幂取模
- bochs安装
- 一个简单的判断三角形形状的C程序
- 一个说明
- hasOwnProperty方法的使用
- android mediaRecorder详细讲解及使用
- Aptana Studio 打开报 Could not launch the product the specified workspace cannot becarated.
- Powerdesigner 连接 sqlserver2008 一些问题及解决汇总
- java JFrame去掉标题栏后如何实现鼠标移动到边框可以进行拖拉以放大缩小
- 如何防止android Toast重复显示
- OFBIZ分享:库存
- 电脑文件丢失后还能恢复吗
- 利用用户自己的服务器、tomcat下的解决iOS7.1企业应用无法安装应用程序 因为证书无效的问题
- sql where 1=1和 0=1 的作用