Android录音应用实例教程
2023-06-13 09:15:45 时间
本文以实例形式较为详细的展示了Android录音的实现方法,分享给大家供大家参考之用。具体方法如下:
首先是xml布局文件:
<LinearLayoutxmlns: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中定义按钮的点击监听器,按下按钮时开始录音,松开按钮时停止录音,类似于微信的操作方法。
//获得控件 publicvoidget_con(){ btn_talk=(Button)findViewById(R.id.btn_talk); btn_talk.setOnTouchListener(newOnTouchListener(){ @Override publicbooleanonTouch(Viewv,MotionEvente){ if(e.getAction()==MotionEvent.ACTION_DOWN){ //开始录音 start_record(); } elseif(e.getAction()==MotionEvent.ACTION_UP){ //停止录音 stop_record(); } returnfalse; } }); }
开始录音的方法,使用了android.media.MediaRecorder录音。首先判断SD卡是否存在,如果存在根据当前时间给创建一个录音文件,保存到预定的目录中,用MediaRecorder类开始录音。
//开始录音 publicvoidstart_record(){ if(!Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)){ show_status("SD卡不存在,请插入SD卡!"); } else{ try { //获取当前时间 cur_date=newDate(System.currentTimeMillis()); str_file=formatter.format(cur_date); //创建保存录音的音频文件 send_sound_file=newFile(Environment.getExternalStorageDirectory().getCanonicalFile()+"/talk/send"); //如果目录不存在 if(!send_sound_file.exists()){ send_sound_file.mkdirs(); } send_sound_file=newFile(Environment.getExternalStorageDirectory().getCanonicalFile()+"/talk/send/"+str_file+".amr"); recorder=newMediaRecorder(); //设置录音的声音来源 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(Exceptione) { show_status(e.toString()); } } }
停止录音的方法,相对简单。
//停止录音 publicvoidstop_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 okio使用方法,Android 开源框架 Okio 原理剖析「建议收藏」
- 工厂方法模式及在Android源码中的应用
- android sdk根目录,Android SDK位置
- Android中dex文件的加载与优化流程
- countdown timer plus_android studio计时器
- 从零开始用android studio
- 【Android应用开发】EasyDialog 源码解析
- 【Android应用开发】Android Studio 简介 (Android Studio Overview)
- 【Android 高性能音频】AAudio 音频流 数据回调细节 ( 数据回调函数优先级 | 数据回调函数 | 采样率 | 采样数 | 缓冲区调整 | 线程不安全 )
- 【Android 应用开发】Android 返回堆栈 与 任务
- 【Android NDK 开发】Android.mk 配置静态库 ( Android Studio 配置静态库 | 配置动态库与静态库区别 | 动态库与静态库打包对比 )
- 【Android 应用开发】Google 官方 EasyPermissions 权限申请库 ( 简介 | 权限申请处理细节 | 添加依赖 | 界面权限申请结果处理 | 权限申请结果回调接口 )
- 【错误记录】Android 应用 release 打包报错处理 ( 关闭语法检查 | 日志处理 | release 配置 )
- 【Android 插件化】VirtualApp 源码分析 ( 添加应用源码分析 | LaunchpadAdapter 适配器 | 适配器添加元素 | PackageAppData 元素 )
- 【错误记录】Android 应用运行报错 ( java.lang.VerifyError: Verifier rejected class androidx. | 逆向中遇到的问题 )
- 【Android Gradle 插件】自定义 Gradle 任务 ⑪ ( Gradle 任务分组 | 为自定义 Gradle 任务指定分组 | 为自定义 Gradle任务指定任务描述 | 代码示例 )
- Android控制文字水平间距android:letterSpacing详解手机开发
- 使用Android实现连接MySQL数据库:实现快速数据交互与管理(android连接mysql数据库)
- 在Android中访问WebService接口的方法
- Android开发笔记之:HandlerRunnable与Thread的区别详解
- Android应用开发之代码混淆