標籤:
分類:C#、Android、VS2015;
建立日期:2016-03-13 一、簡介
利用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.4 錄音