【Android】20.4 录音
2016-03-13 05:01
393 查看
分类:C#、Android、VS2015; 创建日期:2016-03-13
2、设计步骤 (1)AndroidManifest.xml文件 在该文件中添加下面的权限(如果有了就不用添加了)。 <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> (2)添加ch2004Main.axml文件
(3)添加ch2004MainActivity.cs文件
在模拟器中观察运行的效果,单击【录音】按钮开始录音,录音完毕后,直接单击【播放录音】就可以测试录制效果。
一、简介
利用Android提供的MediaRecorder类可直接录制音频。 1、权限要求 录制音频和视频需要下面的权限: <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 2、基本设计步骤(1)初始化和录制
录制音频的基本设计步骤如下: (1) 创建MediaRecorder对象。 (2) 调用SetAudioSource方法指定使用哪个硬件设备捕获音频输入(比如麦克风)。 (3) 调用SetOutputFormat方法指定录音输出文件的格式。 (4) 调用SetAudioEncoder方法设置音频编码类型。 (5) 调用SetOutputFile方法指定输出的音频文件。 (6) 调用Prepare方法初始化录音器。 (7) 调用Start方法开始录音。 下面的代码演示了如何实现: protected MediaRecorder recorder; void RecordAudio (String filePath) { try { if (File.Exists (filePath)) { File.Delete (filePath); } if (recorder == null) { recorder = new MediaRecorder (); // Initial state. } else { recorder.Reset (); recorder.SetAudioSource (AudioSource.Mic); recorder.SetOutputFormat (OutputFormat.ThreeGpp); recorder.SetAudioEncoder (AudioEncoder.AmrNb); // Initialized state. recorder.SetOutputFile (filePath); // DataSourceConfigured state. recorder.Prepare (); // Prepared state recorder.Start (); // Recording state. } } catch (Exception ex) { Console.Out.WriteLine( ex.StackTrace); } }(2)停止录音
调用MediaRecorder 的Stop方法停止录音: recorder.Stop();(3)清除录音占用的资源
一旦停止了MediaRecorder,调用Reset方法将其置于空闲状态(idle state): recorder.Reset(); 当不再使用MediaRecorder实例时,必须释放其占用的资源: recorder.Release();二、示例
该例子的功能是录制来自麦克风的音频,如果笔记本带有麦克风,可直接通过模拟器来观察运行效果。录制以后,可直接单击播放按钮测试录制的效果。 这同样也是一个简单的示例,没有考虑录音过程中电话打入的情况,也没有考虑其他复杂的控制。 1、运行截图2、设计步骤 (1)AndroidManifest.xml文件 在该文件中添加下面的权限(如果有了就不用添加了)。 <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> (2)添加ch2004Main.axml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/ch2004_btnRecStart" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="开始录音" /> <Button android:id="@+id/ch2004_btnRecStop" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="停止录音" /> <Button android:id="@+id/ch2004_btnPlayStart" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="播放录音" /> <Button android:id="@+id/ch2004_btnPlayStop" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="停止播放录音" /> <TextView android:textAppearance="?android:attr/textAppearanceMedium" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/ch2004_textView1" android:gravity="center_horizontal" android:layout_marginTop="30dp" /> </LinearLayout>
(3)添加ch2004MainActivity.cs文件
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Android.App; using Android.Content; using Android.OS; using Android.Runtime; using Android.Views; using Android.Widget; using Android.Media; namespace MyDemos.SrcDemos { [Activity(Label = "ch2004MainActivity")] [IntentFilter(new[] { Intent.ActionMain }, Categories = new[] { Intent.CategoryDefault, ch.MyDemosCategory })] public class ch2004MainActivity : Activity { string filePath; MediaRecorder recorder; MediaPlayer player; Button btnRecStart, btnRecStop, btnPlayStart, btnPlayStop; TextView textView1; protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.ch2004Main); //指定SD卡录制的音频文件路径 filePath = string.Format("{0}/{1}/myRecord.mp3", Android.OS.Environment.ExternalStorageDirectory.Path, Android.OS.Environment.DirectoryMusic); textView1 = FindViewById<TextView>(Resource.Id.ch2004_textView1); btnRecStart = FindViewById<Button>(Resource.Id.ch2004_btnRecStart); btnRecStart.Click += delegate { textView1.Text = "正在录音..."; try { StartRecorder(); } catch (Exception ex) { Console.Out.WriteLine(ex.StackTrace); } }; btnRecStop = FindViewById<Button>(Resource.Id.ch2004_btnRecStop); btnRecStop.Click += delegate { StopRecorder(); textView1.Text = "录音已停止。"; }; btnPlayStart = FindViewById<Button>(Resource.Id.ch2004_btnPlayStart); btnPlayStart.Click += delegate { textView1.Text = "正在播放录音..."; try { player = new MediaPlayer(); player.Reset(); player.SetDataSource(filePath); player.Prepare(); player.Start(); } catch (Exception ex) { Console.WriteLine(ex.StackTrace); } }; btnPlayStop = FindViewById<Button>(Resource.Id.ch2004_btnPlayStop); btnPlayStop.Click += delegate { player.Stop(); textView1.Text = "播放已停止。"; }; } private void StartRecorder() { try { if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } if (recorder == null) { recorder = new MediaRecorder(); // Initial state. } else { recorder.Reset(); } recorder.SetAudioSource(AudioSource.Mic); //录制来自麦克风的声音 recorder.SetOutputFormat(OutputFormat.Mpeg4); recorder.SetAudioEncoder(AudioEncoder.AmrNb); recorder.SetOutputFile(filePath); recorder.Prepare(); recorder.Start(); } catch (Exception ex) { Console.Out.WriteLine(ex.StackTrace); } } private void StopRecorder() { if (recorder != null) { recorder.Stop(); recorder.Release(); recorder = null; } } } }
在模拟器中观察运行的效果,单击【录音】按钮开始录音,录音完毕后,直接单击【播放录音】就可以测试录制效果。
相关文章推荐
- 【Android】20.3 拍照和视频录制
- 实现计时器的几种方式
- aapt 解析android apk
- Android基础-TextView用法
- Android N 多窗口模式,你需要知道的一切
- Android中定时器Timer和TimerTask的启动,停止,暂停,继续等操作实例
- Android中Activity启动模式详解
- Android_自动安装APK
- Retrofit——Java(Android) 的REST 接口封装类库(相当于IOS中的restkit)
- Android_获取当前的版本号和版本名称,获取SIM卡的信息
- android四大组件(详细总结)
- android学习 读书笔记之 android 绘图shape
- Android LineaLayout baselineAligned
- Android—将Bitmap图片保存到SD卡目录下或者指定目录
- Android 中字符串的拆分--split的使用
- AndroidStudio下加入百度地图的使用(一)——环境搭建
- android中的文件(图片)上传
- Android Activity的4种启动模式详解(示例)
- 唤醒锁: 检测 Android* 应用中的 No-Sleep(无法进入睡眠)问题
- Android selector的item里的属性说明