【mediarecorder】
mediarecorder_state_diagram: Initial 》Initialed 》DataSourceConfiged 》Prepared 》Recording 》Released 》 Error
录音界面
播放界面
开始录音(步骤)
停止录音(步骤)
播放实现(步骤)
1 检测SD卡存在,并更新SD卡中已存在的文件.
2 点击列表时,播放被点击的文件
3 播放录音文件
完整源码
JAVA源码
1 public class mediarecorder1
extends ListActivity
2 {
3 private Button mAudioStartBtn;
4 private Button mAudioStopBtn;
5 private File mRecAudioFile;
// 录制的音频文件
6 private File mRecAudioPath;
// 录制的音频文件路徑
7 private MediaRecorder mMediaRecorder;
// MediaRecorder对象
8 private List
mMusicList = new ArrayList();// 录音文件列表
9 private String strTempFile = "recaudio_";// 零时文件的前缀
10
11 @Override
12 protected void onCreate(Bundle savedInstanceState)
13 {
14 // TODO Auto-generated method stub
15 super.onCreate(savedInstanceState);
16 setContentView(R.layout.mymultimedia_mediarecorder1);
17
18 mAudioStartBtn = (Button) findViewById(R.id.mediarecorder1_AudioStartBtn);
19 mAudioStopBtn = (Button) findViewById(R.id.mediarecorder1_AudioStopBtn);
20
21 /*按钮状态*/
22 mAudioStartBtn.setEnabled(true);
23 mAudioStopBtn.setEnabled(false);
24
25 /* 检测是否存在SD卡 */
26 if (Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
27 {
28 mRecAudioPath = Environment.getExternalStorageDirectory();// 得到SD卡得路径
29 musicList();// 更新所有录音文件到List中
30 } else
31 {
32 Toast.makeText(mediarecorder1.this, "没有SD卡", Toast.LENGTH_LONG).show();
33 }
34
35 /* 开始按钮事件监听 */
36 mAudioStartBtn.setOnClickListener(new Button.OnClickListener()
37 {
38 @Override
39 public void onClick(View arg0)
40 {
41 try
42 {
43 /* ①Initial:实例化MediaRecorder对象 */
44 mMediaRecorder = new MediaRecorder();
45 /* ②setAudioSource/setVedioSource*/
46 mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);//设置麦克风
47 /* ②设置输出文件的格式:THREE_GPP/MPEG-4/RAW_AMR/Default
48 * THREE_GPP(3gp格式,H263视频/ARM音频编码)、MPEG-4、RAW_AMR(只支持音频且音频编码要求为AMR_NB)
49 * */
50 mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
51 /* ②设置音频文件的编码:AAC/AMR_NB/AMR_MB/Default */
52 mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
53 /* ②设置输出文件的路径 */
54 try
55 {
56 mRecAudioFile = File.createTempFile(strTempFile, ".amr", mRecAudioPath);
57
58 } catch (Exception e)
59 {
60 e.printStackTrace();
61 }
62 mMediaRecorder.setOutputFile(mRecAudioFile.getAbsolutePath());
63 /* ③准备 */
64 mMediaRecorder.prepare();
65 /* ④开始 */
66 mMediaRecorder.start();
67 /*按钮状态*/
68 mAudioStartBtn.setEnabled(false);
69 mAudioStopBtn.setEnabled(true);
70 } catch (IOException e)
71 {
72 e.printStackTrace();
73 }
74 }
75 });
76 /* 停止按钮事件监听 */
77 mAudioStopBtn.setOnClickListener(new Button.OnClickListener()
78 {
79 @Override
80 public void onClick(View arg0)
81 {
82 // TODO Auto-generated method stub
83 if (mRecAudioFile != null)
84 {
85 /* ⑤停止录音 */
86 mMediaRecorder.stop();
87 /* 将录音文件添加到List中 */
88 mMusicList.add(mRecAudioFile.getName());
89 ArrayAdapter musicList = new ArrayAdapter(mediarecorder1.this,
90 R.layout.list, mMusicList);
91 setListAdapter(musicList);
92 /* ⑥释放MediaRecorder */
93 mMediaRecorder.release();
94 mMediaRecorder = null;
95 /* 按钮状态 */
96 mAudioStartBtn.setEnabled(true);
97 mAudioStopBtn.setEnabled(false);
98 }
99 }
100 });
101 }
102
103 /* 播放录音文件 */
104 private void playMusic(File file)
105 {
106 Intent intent = new Intent();
107 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
108 intent.setAction(android.content.Intent.ACTION_VIEW);
109 /* 设置文件类型 */
110 intent.setDataAndType(Uri.fromFile(file), "audio");
111 startActivity(intent);
112 }
113
114 @Override
115 /* 当我们点击列表时,播放被点击的音乐 */
116 protected void onListItemClick(ListView l, View v, int position, long id)
117 {
118 /* 得到被点击的文件 */
119 File playfile = new File(mRecAudioPath.getAbsolutePath() + File.separator
120 + mMusicList.get(position));
121 /* 播放 */
122 playMusic(playfile);
123 }
124
125 /* 播放列表 */
126 public void musicList()
127 {
128 // 取得指定位置的文件设置显示到播放列表
129 File home = mRecAudioPath;
130 if (home.listFiles(new MusicFilter()).length > 0)
131 {
132 for (File file : home.listFiles(new MusicFilter()))
133 {
134 mMusicList.add(file.getName());
135 }
136 ArrayAdapter musicList = new ArrayAdapter(mediarecorder1.this,
137 R.layout.list, mMusicList);
138 setListAdapter(musicList);
139 }
140 }
141 }
142
143 /* 过滤文件类型 */
144 class MusicFilter implements FilenameFilter
145 {
146 public boolean accept(File dir, String name)
147 {
148 return (name.endsWith(".amr"));
149 }
150 }
XML布局文件
1 xml version="1.0" encoding="utf-8"?> 2 <AbsoluteLayout xmlns:android=""
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="vertical" > 6 7 <ListView
8 android:id="@id/android:list"
9 android:layout_width="fill_parent"
10 android:layout_height="fill_parent"
11 android:layout_weight="1"
12 android:drawSelectorOnTop="false" />13 14 <Button
15 android:id="@+id/mediarecorder1_AudioStartBtn"
16 android:layout_width="170px"
17 android:layout_height="wrap_content"
18 android:layout_x="30px"
19 android:layout_y="100px"
20 android:text="开始录制音频" />21 22 <Button
23 android:id="@+id/mediarecorder1_AudioStopBtn"
24 android:layout_width="170px"
25 android:layout_height="wrap_content"
26 android:layout_x="210px"
27 android:layout_y="100px"
28 android:text="停止音频录制" /> 29 30 AbsoluteLayout>
Refs:
1 http://developer.android.com/reference/android/media/MediaRecorder.html
2 http://blog.csdn.net/iamlazybone/article/details/5764118 关于Android音频、视频录制类MediaRecorder
3 http://blog.csdn.net/chenjie19891104/article/details/6332746
4 Android的MediaRecorder架构介绍 http://blog.csdn.net/xyz_lmn/article/details/6104314
5
-----------------------------------------------------------------------------------------------------------------------------------------------------------
阅读(524) | 评论(0) | 转发(0) |