How-to by Jim Bentley, Portland Maine - james.bentley@live.com
This is a quick tutorial on how to record sound using the iPhone microphone/headset microphone. There are a lot of possible variables to sound recording - File output types, bit rates, channels are just a few.
For more information on recording sound and the various formats available, you should see the following page on the AVAudiorecorder class:
Below is the 'main' class of a quick and dirty application to create sound files (uncompressed PCM Wav Files to be exact) inside the application's tmp directory.
using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using MonoTouch.AudioToolbox;
using MonoTouch.AVFoundation;
using System.IO;
namespace TestRecord
{
public class Application
{
static void Main (string[] args)
{
UIApplication.Main (args);
}
}
// The name AppDelegate is referenced in the MainWindow.xib file.
public partial class AppDelegate : UIApplicationDelegate
{
//declare AVAudioRecorder recorder
public AVAudioRecorder recorder;
// This method is invoked when the application has loaded its UI and its ready to run
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
// If you have defined a view, add it here:
// window.AddSubview (navigationController.View);
window.MakeKeyAndVisible ();
//Record Button Delegate
recordButton.TouchUpInside += delegate {
StartRecording();
};
//Stop Button Delegate
stopButton.TouchUpInside += delegate {
StopRecording();
};
return true;
}
//public void start Recording
public void StartRecording ()
{
NSObject[] values = new NSObject[]
{
NSNumber.FromFloat(44100.0f),
NSNumber.FromInt32((int)AudioFileType.WAVE),
NSNumber.FromInt32(1),
NSNumber.FromInt32((int)AVAudioQuality.Max)
};
NSObject[] keys = new NSObject[]
{
AVAudioSettings.AVSampleRateKey,
AVAudioSettings.AVFormatKey,
AVAudioSettings.AVNumberOfChannelsKey,
AVAudioSettings.AVEncoderAudioQualityKey
};
NSDictionary settings = NSDictionary.FromObjectsAndKeys (values, keys);
//Declare string for application temp path and tack on the file extension
string fileName = testTextField.Text + ".wav";
string basedir = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.Personal), "..");
string tmpdir = Path.Combine(basedir, "tmp");
string audioFilePath = Path.Combine(tmpdir, fileName);
string tempRecording = audioFilePath;
Console.WriteLine(audioFilePath);
NSUrl url = NSUrl.FromFilename(tempRecording);
NSError error = new NSError ();
//Set recorder parameters
recorder = new AVAudioRecorder (url, settings, error);
//Set Metering Enabled so you can get the time of the wav file
recorder.MeteringEnabled = true;
recorder.PrepareToRecord();
recorder.Record();
}
//public void stop Recording
public void StopRecording ()
{
recorder.Stop();
recorder.FinishedRecording += delegate {
recorder.Dispose();
Console.WriteLine("Done Recording");
};
}
// This method is required in iPhoneOS 3.0
public override void OnActivated (UIApplication application)
{
}
}
}