private void OnFinishedRecording(object sender, AVStatusEventArgs e) { recorder.Dispose(); recorder = null; Console.WriteLine($"Done Recording (status: {e.Status})"); }
public void RecordStart(string fileName) { var filePath = _Storage.GetPath(fileName); System.Diagnostics.Debug.WriteLine(string.Format("*** AudioService.RecordStart - Preparing to record to: {0}.", filePath)); _OutputUrl = NSUrl.FromFilename(filePath); // apply recorder settings _Recorder = AVAudioRecorder.Create(_OutputUrl, new AudioSettings(_Settings), out _Error); // record if (!_Recorder.PrepareToRecord()) { _Recorder.Dispose(); _Recorder = null; throw new Exception("Could not prepare recording"); } _Recorder.FinishedRecording += delegate(object sender, AVStatusEventArgs e) { _Recorder.Dispose(); _Recorder = null; Console.WriteLine("Done Recording (status: {0})", e.Status); }; _Recorder.Record(); System.Diagnostics.Debug.WriteLine("*** AudioService.RecordStart - Recording started"); }
/// <summary> /// Trie to set up a recorder with CreateOutputUrl Url. /// </summary> private bool PrepareAudioRecording() { var result = false; recorder = AVAudioRecorder.Create(CreateOutputUrl(), audioSettings, out NSError error); if (error == null) { // Set Recorder to Prepare To Record if (!recorder.PrepareToRecord()) { recorder.Dispose(); recorder = null; } else { recorder.FinishedRecording += OnFinishedRecording; result = true; } } else { Console.WriteLine("creation error"); } return(result); }
bool PrepareAudioRecording() { audioFilePath = CreateOutputUrl(); var audioSettings = new AudioSettings { SampleRate = 44100, Format = AudioToolbox.AudioFormatType.MPEG4AAC, NumberChannels = 1, AudioQuality = AVAudioQuality.High }; //Set recorder parameters NSError error; recorder = AVAudioRecorder.Create(audioFilePath, audioSettings, out error); if (error != null) { Console.WriteLine(error); return(false); } //Set Recorder to Prepare To Record if (!recorder.PrepareToRecord()) { recorder.Dispose(); recorder = null; return(false); } recorder.FinishedRecording += OnFinishedRecording; return(true); }
public string StopRecord() { recorder.Stop(); recorder.Dispose(); recorder = null; return(sFileName); }
public override void ViewDidLoad() { base.ViewDidLoad(); this.RecordingStatusLabel.Text = ""; this.LengthOfRecordingLabel.Text = ""; // start recording wireup this.StartRecordingButton.TouchUpInside += (sender, e) => { Console.WriteLine("Begin Recording"); this.PrepareAudioRecording(); this.recorder.Record(); this.stopwatch = new Stopwatch(); this.stopwatch.Start(); this.LengthOfRecordingLabel.Text = ""; this.RecordingStatusLabel.Text = "Recording"; this.StartRecordingButton.Enabled = false; this.StopRecordingButton.Enabled = true; this.PlayRecordedSoundButton.Enabled = false; }; // stop recording wireup 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 = ""; this.StartRecordingButton.Enabled = true; this.StopRecordingButton.Enabled = false; this.PlayRecordedSoundButton.Enabled = true; }; // 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 static void StopRecording(Action finishedRecording) { _recorder.Stop(); _recorder.FinishedRecording += delegate { _recorder.Dispose(); finishedRecording(); }; }
bool PrepareAudioRecording() { //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); this.audioFilePath = NSUrl.FromFilename(tempRecording); //set up the NSObject Array of values that will be combined with the keys to make the NSDictionary NSObject[] values = new NSObject[] { NSNumber.FromFloat(44100.0f), NSNumber.FromInt32((int)MonoTouch.AudioToolbox.AudioFormatType.MPEG4AAC), NSNumber.FromInt32(1), NSNumber.FromInt32((int)AVAudioQuality.High) }; //Set up the NSObject Array of keys that will be combined with the values to make the NSDictionary NSObject[] keys = new NSObject[] { AVAudioSettings.AVSampleRateKey, AVAudioSettings.AVFormatIDKey, AVAudioSettings.AVNumberOfChannelsKey, AVAudioSettings.AVEncoderAudioQualityKey }; //Set Settings with the Values and Keys to create the NSDictionary settings = NSDictionary.FromObjectsAndKeys(values, keys); //Set recorder parameters NSError error; recorder = AVAudioRecorder.ToUrl(this.audioFilePath, settings, out error); if ((recorder == null) || (error != null)) { Console.WriteLine(error); return(false); } //Set Recorder to Prepare To Record 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); }
/// /// Name ClearAudioFiles /// /// <summary> Clears the audio file after saving. /// </summary> /// <returns></returns> public void ClearAudioFiles() { try { _AudioPlayer?.Stop(); _AudioPlayer?.Dispose(); recorder?.Dispose(); File.Delete(audioFilePath); } catch (Exception ex) { LogTracking.LogTrace(ex.ToString()); } }
public void StopRecording() { try { if (recorder != null) { recorder.Stop(); recorder.Dispose(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public Task <FileData> PlatformStopAsync() { if (_recorder == null) { return(Task.FromResult(default(FileData))); } _recorder.Stop(); _recorder.Dispose(); _recorder = null; AVAudioSession.SharedInstance().SetActive(false); FileInfo fi = new FileInfo(_audioFilePath); return(Task.FromResult(new FileData(_audioFilePath, fi.Name, (ulong)fi.Length))); }
public void Clear() { if (_recorder != null) { if (_recorder.Recording) { _recorder.Stop(); _timer.Stop(); } _recorder.DeleteRecording(); _recorder.Dispose(); _recorder = null; } ClearTimer(); }
bool PrepareAudioRecording() { //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 = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), fileName); Console.WriteLine(tempRecording); this.audioFilePath = NSUrl.FromFilename(tempRecording); var audioSettings = new AudioSettings() { SampleRate = 44100.0f, Format = AudioFormatType.MPEG4AAC,//MonoTouch.AudioToolbox.AudioFormatType.MPEG4AAC, NumberChannels = 1, AudioQuality = AVAudioQuality.High }; //Set recorder parameters NSError error; recorder = AVAudioRecorder.Create(this.audioFilePath, audioSettings, out error); if ((recorder == null) || (error != null)) { Console.WriteLine(error); return(false); } //Set Recorder to Prepare To Record 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); }
bool PrepareAudioRecording() { //Declare string for application temp path and tack on the file extension //string fileName = string.Format("Myfile{0}.aac", DateTime.Now.ToString("yyyyMMddHHmmss")); string fileName = "temp.aac"; string tempRecording = NSBundle.MainBundle.BundlePath + "/../tmp/" + fileName; this.audioFilePath = NSUrl.FromFilename(tempRecording); var audioSettings = new AudioSettings() { SampleRate = 44100.0f, Format = MonoTouch.AudioToolbox.AudioFormatType.MPEG4AAC, NumberChannels = 1, AudioQuality = AVAudioQuality.High }; //Set recorder parameters NSError error; recorder = AVAudioRecorder.Create(this.audioFilePath, audioSettings, out error); if ((recorder == null) || (error != null)) { return(false); } //Set Recorder to Prepare To Record if (!recorder.PrepareToRecord()) { recorder.Dispose(); recorder = null; return(false); } recorder.FinishedRecording += delegate(object sender, AVStatusEventArgs e) { recorder.Dispose(); recorder = null; }; recorder.MeteringEnabled = true; return(true); }
public void Stop(out byte[] fileData) { try { recorder.Stop(); recorder.Dispose(); //_recorder.Reset(); //recorder.Release(); using (var streamReader = new StreamReader(path)) { var bytes = default(byte[]); using (var memstream = new MemoryStream()) { streamReader.BaseStream.CopyTo(memstream); fileData = memstream.ToArray(); } } } catch (Exception e) { string error = e.ToString(); Console.WriteLine(e); fileData = null; } recorder = null; //Bitmap bmThumbnail; //bmThumbnail = ThumbnailUtils.CreateVideoThumbnail(path, ThumbnailKind.MiniKind); //imageThumbnail.setImageBitmap(bmThumbnail); //_player.SetDataSource(path); //_player.Prepare(); //_player.Start(); }
private bool PrepareAudioRecording() { var result = false; audioFilePath = CreateOutputUrl(); var audioSettings = new AudioSettings { SampleRate = 44100, NumberChannels = 1, AudioQuality = AVAudioQuality.High, Format = AudioToolbox.AudioFormatType.MPEG4AAC, }; // Set recorder parameters recorder = AVAudioRecorder.Create(audioFilePath, audioSettings, out NSError error); if (error == null) { // Set Recorder to Prepare To Record if (!recorder.PrepareToRecord()) { recorder.Dispose(); recorder = null; } else { recorder.FinishedRecording += OnFinishedRecording; result = true; } } else { Console.WriteLine(error.LocalizedDescription); } return(result); }
protected override async Task <List <Datum> > PollAsync(CancellationToken cancellationToken) { AVAudioRecorder recorder = null; string recordPath = Path.GetTempFileName(); try { AVAudioSession audioSession = AVAudioSession.SharedInstance(); NSError error = audioSession.SetCategory(AVAudioSessionCategory.Record); if (error != null) { throw new Exception("Failed to initialize iOS audio recording session: " + error.LocalizedDescription); } error = audioSession.SetActive(true); if (error != null) { throw new Exception("Failed to make audio session active: " + error.LocalizedDescription); } recorder = AVAudioRecorder.Create(NSUrl.FromFilename(recordPath), new AudioSettings(_settings), out error); if (error != null) { throw new Exception("Failed to create sound recorder: " + error.LocalizedDescription); } recorder.MeteringEnabled = true; // we need to take a meter reading while the recorder is running, so record for one second beyond the sample length if (recorder.RecordFor(SampleLengthMS / 1000d + 1)) { await Task.Delay(SampleLengthMS); recorder.UpdateMeters(); double decibels = 100 * (recorder.PeakPower(0) + 160) / 160f; // range looks to be [-160 - 0] from http://b2cloud.com.au/tutorial/obtaining-decibels-from-the-ios-microphone return(new Datum[] { new SoundDatum(DateTimeOffset.UtcNow, decibels) }.ToList()); } else { throw new Exception("Failed to start recording."); } } finally { try { File.Delete(recordPath); } catch (Exception ex) { SensusServiceHelper.Get().Logger.Log("Failed to delete sound file: " + ex.Message, LoggingLevel.Debug, GetType()); } if (recorder != null) { try { recorder.Stop(); } catch (Exception) { } try { recorder.Dispose(); } catch (Exception) { } } } }
bool PrepareAudioRecording () { audioFilePath = CreateOutputUrl (); var audioSettings = new AudioSettings { SampleRate = 44100, Format = AudioToolbox.AudioFormatType.MPEG4AAC, NumberChannels = 1, AudioQuality = AVAudioQuality.High }; //Set recorder parameters NSError error; recorder = AVAudioRecorder.Create (audioFilePath, audioSettings, out error); if (error != null) { Console.WriteLine (error); return false; } //Set Recorder to Prepare To Record if (!recorder.PrepareToRecord ()) { recorder.Dispose (); recorder = null; return false; } recorder.FinishedRecording += OnFinishedRecording; return true; }
private bool PrepareAudioRecording() { var audioSession = AVAudioSession.SharedInstance(); var err = audioSession.SetCategory(AVAudioSessionCategory.PlayAndRecord); if (err != null) { Console.WriteLine("audioSession: {0}", err); return(false); } err = audioSession.SetActive(true); if (err != null) { Console.WriteLine("audioSession: {0}", err); return(false); } NSObject[] values = new NSObject[] { NSNumber.FromFloat(44100.0f), NSNumber.FromInt32((int)AudioToolbox.AudioFormatType.MPEG4AAC), NSNumber.FromInt32(1), NSNumber.FromInt32((int)AVAudioQuality.High) }; NSObject[] keys = new NSObject[] { AVAudioSettings.AVSampleRateKey, AVAudioSettings.AVFormatIDKey, AVAudioSettings.AVNumberOfChannelsKey, AVAudioSettings.AVEncoderAudioQualityKey }; var settings = NSDictionary.FromObjectsAndKeys(values, keys); NSError error; var _url = NSUrl.FromFilename(Constants.INITIAL_AUDIO_FILE_PATH); _audioRecorder = AVAudioRecorder.Create(_url, new AudioSettings(settings), out error); if ((_audioRecorder == null) || (error != null)) { Console.WriteLine(error); return(false); } if (!_audioRecorder.PrepareToRecord()) { _audioRecorder.Dispose(); _audioRecorder = null; return(false); } _audioRecorder.FinishedRecording += delegate(object sender, AVStatusEventArgs e) { _audioRecorder.Dispose(); _audioRecorder = null; Console.WriteLine("Done Recording (status: {0})", e.Status); }; return(true); }
bool PrepareAudioRecording() { // You must initialize an audio session before trying to record var audioSession = AVAudioSession.SharedInstance (); var err = audioSession.SetCategory (AVAudioSessionCategory.PlayAndRecord); if(err != null) { Console.WriteLine ("audioSession: {0}", err); return false; } err = audioSession.SetActive (true); if(err != null ){ Console.WriteLine ("audioSession: {0}", err); return false; } // 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 = Path.Combine (Path.GetTempPath (), fileName); 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 NSObject[] values = new NSObject[] { NSNumber.FromFloat(44100.0f), NSNumber.FromInt32((int)AudioToolbox.AudioFormatType.MPEG4AAC), NSNumber.FromInt32(1), NSNumber.FromInt32((int)AVAudioQuality.High) }; //Set up the NSObject Array of keys that will be combined with the values to make the NSDictionary NSObject[] keys = new NSObject[] { AVAudioSettings.AVSampleRateKey, AVAudioSettings.AVFormatIDKey, AVAudioSettings.AVNumberOfChannelsKey, AVAudioSettings.AVEncoderAudioQualityKey }; //Set Settings with the Values and Keys to create the NSDictionary settings = NSDictionary.FromObjectsAndKeys (values, keys); //Set recorder parameters NSError error; recorder = AVAudioRecorder.Create(this.audioFilePath, new AudioSettings(settings), out error); if ((recorder == null) || (error != null)) { Console.WriteLine (error); return false; } //Set Recorder to Prepare To Record 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; }
public bool PrepareAudioRecording() { // You must initialize an audio session before trying to record var audioSession = AVAudioSession.SharedInstance(); var err = audioSession.SetCategory(AVAudioSessionCategory.PlayAndRecord); if (err != null) { Console.WriteLine("audioSession: {0}", err); return(false); } err = audioSession.SetActive(true); if (err != null) { Console.WriteLine("audioSession: {0}", err); return(false); } // Declare string for application temp path and tack on the file extension string fileName = string.Format("recording{0}.aac", DateTime.Now.ToString("yyyyMMddHHmmss")); string tempRecording = Path.Combine(Path.GetTempPath(), fileName); Console.WriteLine(tempRecording); audioFilePath = NSUrl.FromFilename(tempRecording); //set up the NSObject Array of values that will be combined with the keys to make the NSDictionary NSObject[] values = new NSObject[] { NSNumber.FromFloat(44100.0f), NSNumber.FromInt32((int)AudioToolbox.AudioFormatType.MPEG4AAC), NSNumber.FromInt32(1), NSNumber.FromInt32((int)AVAudioQuality.High) }; //Set up the NSObject Array of keys that will be combined with the values to make the NSDictionary NSObject[] keys = new NSObject[] { AVAudioSettings.AVSampleRateKey, AVAudioSettings.AVFormatIDKey, AVAudioSettings.AVNumberOfChannelsKey, AVAudioSettings.AVEncoderAudioQualityKey }; //Set Settings with the Values and Keys to create the NSDictionary settings = NSDictionary.FromObjectsAndKeys(values, keys); //Set recorder parameters NSError error; recorder = AVAudioRecorder.Create(this.audioFilePath, new AudioSettings(settings), out error); if ((recorder == null) || (error != null)) { Console.WriteLine(error); return(false); } //Set Recorder to Prepare To Record 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); }
bool PrepareAudioRecording() { //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); this.audioFilePath = NSUrl.FromFilename(tempRecording); var audioSettings = new AudioSettings() { SampleRate = 44100.0f, Format = MonoTouch.AudioToolbox.AudioFormatType.MPEG4AAC, NumberChannels = 1, AudioQuality = AVAudioQuality.High }; //Set recorder parameters NSError error; recorder = AVAudioRecorder.Create(this.audioFilePath, audioSettings, out error); if ((recorder == null) || (error != null)) { Console.WriteLine(error); return false; } //Set Recorder to Prepare To Record 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; }
public string FinishRecording() { recorder.Stop(); recorder.Dispose(); return(filename); }
bool PrepareAudioRecording () { //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); this.audioFilePath = NSUrl.FromFilename(tempRecording); //set up the NSObject Array of values that will be combined with the keys to make the NSDictionary NSObject[] values = new NSObject[] { NSNumber.FromFloat(44100.0f), NSNumber.FromInt32((int)MonoTouch.AudioToolbox.AudioFormatType.MPEG4AAC), NSNumber.FromInt32(1), NSNumber.FromInt32((int)AVAudioQuality.High) }; //Set up the NSObject Array of keys that will be combined with the values to make the NSDictionary NSObject[] keys = new NSObject[] { AVAudioSettings.AVSampleRateKey, AVAudioSettings.AVFormatIDKey, AVAudioSettings.AVNumberOfChannelsKey, AVAudioSettings.AVEncoderAudioQualityKey }; //Set Settings with the Values and Keys to create the NSDictionary settings = NSDictionary.FromObjectsAndKeys (values, keys); //Set recorder parameters NSError error; recorder = AVAudioRecorder.ToUrl(this.audioFilePath, settings, out error); if ((recorder == null) || (error != null)) { Console.WriteLine (error); return false; } //Set Recorder to Prepare To Record 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; }