Skip to content
This repository has been archived by the owner on Apr 29, 2024. It is now read-only.

Commit

Permalink
Allow multiple record and play in the Sound sample. Fix bug #6150
Browse files Browse the repository at this point in the history
  • Loading branch information
spouliot committed Sep 4, 2012
1 parent 1a7e7d0 commit 13fef1d
Showing 1 changed file with 39 additions and 19 deletions.
58 changes: 39 additions & 19 deletions Sound/Sound/SoundViewController.cs
Expand Up @@ -18,6 +18,7 @@ public partial class SoundViewController : UIViewController
NSDictionary settings;
Stopwatch stopwatch = null;
NSUrl audioFilePath = null;
NSObject observer;

static bool UserInterfaceIdiomIsPhone {
get { return UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone; }
Expand All @@ -26,6 +27,7 @@ public partial class SoundViewController : UIViewController
public SoundViewController ()
: base (UserInterfaceIdiomIsPhone ? "SoundViewController_iPhone" : "SoundViewController_iPad", null)
{
AudioSession.Initialize ();
}

public override void ViewDidLoad ()
Expand All @@ -39,8 +41,18 @@ public override void ViewDidLoad ()
this.StartRecordingButton.TouchUpInside += (sender, e) => {
Console.WriteLine("Begin Recording");
this.PrepareAudioRecording();
this.recorder.Record();
AudioSession.Category = AudioSessionCategory.RecordAudio;
AudioSession.SetActive (true);
if (!PrepareAudioRecording ()) {
RecordingStatusLabel.Text = "Error preparing";
return;
}
if (!recorder.Record ()) {
RecordingStatusLabel.Text = "Error preparing";
return;
}
this.stopwatch = new Stopwatch();
this.stopwatch.Start();
Expand All @@ -55,11 +67,6 @@ public override void ViewDidLoad ()
this.StopRecordingButton.TouchUpInside += (sender, e) => {
this.recorder.Stop();
recorder.FinishedRecording += delegate {
recorder.Dispose();
Console.WriteLine("Done Recording");
};
this.LengthOfRecordingLabel.Text = string.Format("{0:hh\\:mm\\:ss}", this.stopwatch.Elapsed);
this.stopwatch.Stop();
this.RecordingStatusLabel.Text = "";
Expand All @@ -68,31 +75,34 @@ public override void ViewDidLoad ()
this.PlayRecordedSoundButton.Enabled = true;
};

observer = NSNotificationCenter.DefaultCenter.AddObserver (AVPlayerItem.DidPlayToEndTimeNotification, delegate (NSNotification n) {
player.Dispose ();
player = null;
});

// play recorded sound wireup
this.PlayRecordedSoundButton.TouchUpInside += (sender, e) => {
try {
Console.WriteLine("Playing Back Recording " + this.audioFilePath.ToString());
// The following line prevents the audio from stopping
// when the device autolocks. will also make sure that it plays, even
// if the device is in mute
AudioSession.Category = AudioSessionCategory.MediaPlayback;
AudioSession.RoutingOverride = AudioSessionRoutingOverride.Speaker;
this.player = new AVPlayer (this.audioFilePath);
this.player.Play();
} catch (Exception ex) {
Console.WriteLine("There was a problem playing back audio: ");
Console.WriteLine(ex.Message);
}
};
}

public override void ViewDidUnload ()
{
NSNotificationCenter.DefaultCenter.RemoveObserver (observer);

base.ViewDidUnload ();

// Clear any references to subviews of the main view in order to
Expand All @@ -108,13 +118,13 @@ public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientat
return true;
}

protected void PrepareAudioRecording()
bool PrepareAudioRecording ()
{
//Declare string for application temp path and tack on the file extension
//Declare string for application temp path and tack on the file extension
string fileName = string.Format ("Myfile{0}.aac", DateTime.Now.ToString ("yyyyMMddHHmmss"));
string tempRecording = NSBundle.MainBundle.BundlePath + "/../tmp/" + fileName;

Console.WriteLine(tempRecording);
Console.WriteLine (tempRecording);
this.audioFilePath = NSUrl.FromFilename(tempRecording);

//set up the NSObject Array of values that will be combined with the keys to make the NSDictionary
Expand All @@ -139,15 +149,25 @@ protected void PrepareAudioRecording()
//Set recorder parameters
NSError error;
recorder = AVAudioRecorder.ToUrl(this.audioFilePath, settings, out error);
if (recorder == null){
if ((recorder == null) || (error != null)) {
Console.WriteLine (error);
return;
return false;
}

//Set Recorder to Prepare To Record
recorder.PrepareToRecord();
if (!recorder.PrepareToRecord ()) {
recorder.Dispose ();
recorder = null;
return false;
}

recorder.FinishedRecording += delegate (object sender, AVStatusEventArgs e) {
recorder.Dispose ();
recorder = null;
Console.WriteLine ("Done Recording (status: {0})", e.Status);
};

return true;
}
}
}

}

0 comments on commit 13fef1d

Please sign in to comment.