Android录音应用
来源:互联网 发布:nginx ssl 配置证书链 编辑:程序博客网 时间:2024/05/17 01:00
首先是xml布局文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
xmlns:tools=
"http://schemas.android.com/tools"
android:layout_width=
"fill_parent"
android:layout_height=
"fill_parent"
android:layout_gravity=
"center"
android:gravity=
"center"
android:orientation=
"vertical"
android:paddingBottom=
"@dimen/activity_vertical_margin"
android:paddingLeft=
"@dimen/activity_horizontal_margin"
android:paddingRight=
"@dimen/activity_horizontal_margin"
android:paddingTop=
"@dimen/activity_vertical_margin"
tools:context=
".MainActivity"
>
<Button
android:id=
"@+id/btn_talk"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_gravity=
"center"
android:enabled=
"false"
android:text=
"TALK"
android:textSize=
"30dp"
android:textStyle=
"bold"
/>
</LinearLayout>
MainActivity中定义按钮的点击监听器,按下按钮时开始录音,松开按钮时停止录音,类似于微信的操作方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// 获得控件
public
void
get_con(){
btn_talk = (Button)findViewById(R.id.btn_talk);
btn_talk.setOnTouchListener(
new
OnTouchListener(){
@Override
public
boolean
onTouch(View v, MotionEvent e) {
if
(e.getAction() == MotionEvent.ACTION_DOWN){
// 开始录音
start_record();
}
else
if
(e.getAction() == MotionEvent.ACTION_UP){
// 停止录音
stop_record();
}
return
false
;
}
});
}
开始录音的方法,使用了android.media.MediaRecorder录音。首先判断SD卡是否存在,如果存在根据当前时间给创建一个录音文件,保存到预定的目录中,用MediaRecorder类开始录音。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// 开始录音
public
void
start_record(){
if
(!Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)){
show_status(
"SD卡不存在,请插入SD卡!"
);
}
else
{
try
{
// 获取当前时间
cur_date =
new
Date(System.currentTimeMillis());
str_file = formatter.format(cur_date);
// 创建保存录音的音频文件
send_sound_file =
new
File(Environment
.getExternalStorageDirectory()
.getCanonicalFile() +
"/talk/send"
);
// 如果目录不存在
if
(!send_sound_file.exists()){
send_sound_file.mkdirs();
}
send_sound_file =
new
File(Environment
.getExternalStorageDirectory()
.getCanonicalFile() +
"/talk/send/"
+ str_file +
".amr"
);
recorder =
new
MediaRecorder();
// 设置录音的声音来源
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
// 设置录制的声音的输出格式(必须在设置声音编码格式之前设置)
recorder.setOutputFormat(MediaRecorder
.OutputFormat.THREE_GPP);
// 设置声音编码的格式
recorder.setAudioEncoder(MediaRecorder
.AudioEncoder.AMR_NB);
recorder.setOutputFile(send_sound_file.getAbsolutePath());
recorder.prepare();
// 开始录音
recorder.start();
}
catch
(Exception e)
{
show_status(e.toString());
}
}
}
停止录音的方法,相对简单。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 停止录音
public
void
stop_record(){
if
(send_sound_file !=
null
&& send_sound_file.exists())
{
ses_id = ses_id +
1
;
// 停止录音
recorder.stop();
// 释放资源
recorder.release();
recorder =
null
;
}
super
.onDestroy();
}
经过实验,录制的3gp文件可以正常播放。
- Android录音应用
- Android应用开发-----------来电录音(去电录音)
- 录音项目源代码 ----- Android 应用开发揭秘
- Android --- 多媒体应用(MediaRecorder 录音)
- Android 录音
- android 录音
- Android 录音
- android 录音
- android 录音
- android 录音
- android 录音
- android 录音
- Android 录音
- Android录音
- android 录音
- Android录音
- 在Android模拟器(BlueStacks)中调试录音应用【2012
- Android多媒体应用开发系列(三) 使用MediaRecorder录音
- 人的分类
- 接纳自己 accept yourself
- John Hopcroft在"21世纪的计…
- 工大机试第二题
- 工大机试第三题 韩老师版
- Android录音应用
- 工大机试第四题 韩老师版
- 工大机试第五题 韩老师版
- 举头望明月,低头敲程序
- 新浪等级划分
- 关于开展2009年暑期大学生社会实践…
- android 开发: 生成签名文件 and 对app签名
- 搞笑段子
- 情人节告白台词