Ejemplo n.º 1
0
        void SetupRecorder()
        {
            // An extension is required otherwise the file does not save to a format that browsers support (mp4)
            // Note: output the file rather than the path as the path is calculated via PrivatePath interface in the PCL
            var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

            filename = Path.Combine(documents, DateTimeOffset.Now.ToUnixTimeSeconds().ToString() + ".3gp");

            var audioSession = AVAudioSession.SharedInstance();
            var err          = audioSession.SetCategory(AVAudioSessionCategory.PlayAndRecord);

            err = audioSession.SetActive(true);

            NSObject[] values =
            {
                NSNumber.FromFloat(8000.0f),
                NSNumber.FromInt32((int)AudioToolbox.AudioFormatType.MPEG4AAC),
                NSNumber.FromInt32(1)
            };

            NSObject[] keys =
            {
                AVAudioSettings.AVSampleRateKey,
                AVAudioSettings.AVFormatIDKey,
                AVAudioSettings.AVNumberOfChannelsKey
            };

            recorder = AVAudioRecorder.Create(
                NSUrl.FromFilename(filename),
                new AudioSettings(NSDictionary.FromObjectsAndKeys(values, keys)),
                out var error
                );
        }
Ejemplo n.º 2
0
        /// <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);
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
        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");
        }
Ejemplo n.º 5
0
        public static void Record(string url)
        {
            var parameters = HttpUtility.ParseQueryString(url.Substring(url.IndexOf('?')));

            if (parameters != null)
            {
                if (parameters.ContainsKey("callback"))
                {
                    callback = parameters ["callback"];
                }
                else
                {
                    throw new ArgumentException("Audio recording requires a callback URI.");
                }
            }

            NSObject[] values = new NSObject[]
            {
                NSNumber.FromInt32((int)AudioFormatType.MPEG4AAC),
                NSNumber.FromInt32(2),
                NSNumber.FromInt32((int)AVAudioQuality.Max)
            };

            NSObject[] keys = new NSObject[]
            {
                AVAudioSettings.AVFormatIDKey,
                AVAudioSettings.AVNumberOfChannelsKey,
                AVAudioSettings.AVEncoderAudioQualityKey
            };

            NSDictionary settings = NSDictionary.FromObjectsAndKeys(values, keys);

            string audioFilePath = Path.Combine(TouchFactory.Instance.TempPath, Guid.NewGuid().ToString() + ".aac");

            NSError error = null;

            audioRecorder = AVAudioRecorder.Create(NSUrl.FromFilename(audioFilePath), new AudioSettings(settings), out error);

            var actionSheet = new UIActionSheet(string.Empty)
            {
                TouchFactory.Instance.GetResourceString("RecordAudio"),
                TouchFactory.Instance.GetResourceString("Cancel"),
            };

            actionSheet.CancelButtonIndex = 1;
            actionSheet.Style             = UIActionSheetStyle.BlackTranslucent;
            actionSheet.ShowInView(TouchFactory.Instance.TopViewController.View);
            actionSheet.Clicked += delegate(object sender, UIButtonEventArgs args)
            {
                switch (args.ButtonIndex)
                {
                case 0:
                    StartRecording();
                    break;
                }
            };
        }
Ejemplo n.º 6
0
        private bool PrepareVariables()
        {
            var audioSession = AVAudioSession.SharedInstance();
            var err          = audioSession.SetCategory(AVAudioSessionCategory.PlayAndRecord);

            if (err != null)
            {
                Console.WriteLine($"audioSession: {err}");
                return(false);
            }
            err = audioSession.SetActive(true);
            if (err != null)
            {
                Console.WriteLine($"audioSession: {err}");
                return(false);
            }
            //Declare string for application temp path and tack on the file extension
            string fileName = $"Myfile{DateTime.Now.ToString("yyyyMMddHHmmss")}.wav";

            audioFilePath = Path.Combine(Path.GetTempPath(), fileName);

            Console.WriteLine("Audio File Path: " + audioFilePath);

            url = NSUrl.FromFilename(audioFilePath);
            //set up the NSObject Array of values that will be combined with the keys to make the NSDictionary
            NSObject[] values =
            {
                NSNumber.FromFloat(44100.0f),                                    //Sample Rate
                NSNumber.FromInt32((int)AudioToolbox.AudioFormatType.LinearPCM), //AVFormat
                NSNumber.FromInt32(2),                                           //Channels
                NSNumber.FromInt32(16),                                          //PCMBitDepth
                NSNumber.FromBoolean(false),                                     //IsBigEndianKey
                NSNumber.FromBoolean(false)                                      //IsFloatKey
            };

            //Set up the NSObject Array of keys that will be combined with the values to make the NSDictionary
            NSObject[] keys =
            {
                AVAudioSettings.AVSampleRateKey,
                AVAudioSettings.AVFormatIDKey,
                AVAudioSettings.AVNumberOfChannelsKey,
                AVAudioSettings.AVLinearPCMBitDepthKey,
                AVAudioSettings.AVLinearPCMIsBigEndianKey,
                AVAudioSettings.AVLinearPCMIsFloatKey
            };

            //Set Settings with the Values and Keys to create the NSDictionary
            settings = NSDictionary.FromObjectsAndKeys(values, keys);

            //Set recorder parameters
            recorder = AVAudioRecorder.Create(url, new AudioSettings(settings), out error);

            //Set Recorder to Prepare To Record
            recorder.PrepareToRecord();
            return(true);
        }
Ejemplo n.º 7
0
        public void PrepareRecorder()
        {
            if (Recorder != null)
            {
                Recorder.Dispose();
            }
            var audioSession = AVAudioSession.SharedInstance();
            var err          = audioSession.SetCategory(AVAudioSessionCategory.PlayAndRecord);

            if (err != null)
            {
                Console.WriteLine("audioSession: {0}", err);
            }
            err = audioSession.SetActive(true);
            if (err != null)
            {
                Console.WriteLine("audioSession: {0}", err);
            }

            Console.WriteLine("Audio File Path: " + AudioFilePath + AudioFileName);
            Url = NSUrl.FromFilename(AudioFilePath + AudioFileName + ".mp4");

            if (File.Exists(AudioFilePath + "/" + AudioFileName + ".mp4"))
            {
                File.Delete(AudioFilePath + "/" + AudioFileName + ".mp4");
            }

            //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),
                //Sample Rate
                NSNumber.FromInt32((int)AudioToolbox.AudioFormatType.LinearPCM),
                //AVFormat
                NSNumber.FromInt32(2),
                //Channels
                NSNumber.FromInt32(16),
                //PCMBitDepth
                NSNumber.FromBoolean(false),
                //IsBigEndianKey
                NSNumber.FromBoolean(false)
                //IsFloatKey
            };
            //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.AVLinearPCMBitDepthKey,
                AVAudioSettings.AVLinearPCMIsBigEndianKey,
                AVAudioSettings.AVLinearPCMIsFloatKey
            };

            Settings = NSDictionary.FromObjectsAndKeys(values, keys);
            Recorder = AVAudioRecorder.Create(Url, new AudioSettings(Settings), out Error);
            Recorder.PrepareToRecord();
        }
Ejemplo n.º 8
0
        static void CreateRecorder()
        {
            ConfigureAudio(AVAudioSessionCategory.PlayAndRecord);

            Recorder = AVAudioRecorder.Create(NSUrl.FromFilename(Recording.FullName), GetSettings(), out var err);
            if (err != null)
            {
                throw new Exception("Could not create a recorder because: " + err.Description);
            }
        }
Ejemplo n.º 9
0
        static Background()
        {
            var settings = new AudioSettings();

            settings.AudioQuality = AVAudioQuality.Min;

            NSError error;

            audioRecorder = AVAudioRecorder.Create(new NSUrl(Manager.AudioFile), settings, out error);
        }
Ejemplo n.º 10
0
        void InitAudioRecorder()
        {
            var url = NSUrl.FromFilename(GetTempFileName());

            var settings = NSDictionary.FromObjectsAndKeys(objects, keys);

            recorder = AVAudioRecorder.Create(url, new AudioSettings(settings), out NSError error);

            recorder.PrepareToRecord();
        }
Ejemplo n.º 11
0
        bool PrepareAudioRecording(bool isLeft)
        {
            //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);

            if (isLeft)
            {
                audioLeft = this.audioFilePath.ToString();
//				detailViewController.NewSndPath (audioLeft, true);
            }
            else
            {
                audioRight = this.audioFilePath.ToString();
//				detailViewController.NewSndPath (audioRight, false);
            }

            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);
        }
Ejemplo n.º 12
0
        static Background()
        {
            var settings = new AudioSettings();

            settings.AudioQuality = AVAudioQuality.Min;

            NSError error;

            audioRecorder = AVAudioRecorder.Create(new NSUrl(Manager.AudioFile), settings, out error);
            audioPlayer   = AVAudioPlayer.FromUrl(new NSUrl(NSBundle.MainBundle.PathForResource("3", "wav")));
        }
Ejemplo n.º 13
0
        private void SetupRecorder(string sFileName)
        {
            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;
            }

            this.sFileName = sFileName;
            //Declare string for application temp path and tack on the file extension
            //string fileName = string.Format("{0}.wav", DateTime.Now.ToString("yyyyMMddHHmmss"));
            string audioFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), sFileName);

            url = NSUrl.FromFilename(audioFilePath);
            //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),                                   //Sample Rate
                NSNumber.FromInt32((int)AudioToolbox.AudioFormatType.MPEG4AAC), //AVFormat
                NSNumber.FromInt32(2),                                          //Channels
                NSNumber.FromInt32(16),                                         //PCMBitDepth
                NSNumber.FromInt32((int)AVAudioQuality.High),
                NSNumber.FromBoolean(false),                                    //IsBigEndianKey
                NSNumber.FromBoolean(false)                                     //IsFloatKey
            };

            //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,
                AVAudioSettings.AVLinearPCMBitDepthKey,
                AVAudioSettings.AVLinearPCMIsBigEndianKey,
                AVAudioSettings.AVLinearPCMIsFloatKey,
            };

            //Set Settings with the Values and Keys to create the NSDictionary
            settings = NSDictionary.FromObjectsAndKeys(values, keys);

            //Set recorder parameters
            this.recorder = AVAudioRecorder.Create(url, new AudioSettings(settings), out error);
        }
Ejemplo n.º 14
0
        public void CreateWithError()
        {
            var     url = NSUrl.FromFilename("/dev/fake.wav");
            NSError error;
            var     audioSettings = new AudioSettings(NSDictionary.FromObjectsAndKeys(Values, Keys));

            using (var recorder = AVAudioRecorder.Create(url, audioSettings, out error)) {
                Assert.Null(recorder);
                Assert.NotNull(error);
            }
        }
Ejemplo n.º 15
0
        void InitializeRecorder()
        {
            var audioSession = AVAudioSession.SharedInstance();
            var err          = audioSession.SetCategory(AVAudioSessionCategory.PlayAndRecord);

            if (err != null)
            {
                Console.WriteLine("audioSession: {0}", err);
                return;
            }
            err = audioSession.SetActive(true);
            if (err != null)
            {
                Console.WriteLine("audioSession: {0}", err);
                return;
            }

            var    localStorage  = PCLStorage.FileSystem.Current.LocalStorage.Path;
            string audioFilePath = localStorage + "/SmartCoffee.wav";

            Console.WriteLine("Audio File Path: " + audioFilePath);

            url = NSUrl.FromFilename(audioFilePath);

            //set up the NSObject Array of values that will be combined with the keys to make the NSDictionary
            var values = new NSObject[]
            {
                NSNumber.FromFloat(44100.0f),                                    //Sample Rate
                NSNumber.FromInt32((int)AudioToolbox.AudioFormatType.LinearPCM), //AVFormat
                NSNumber.FromInt32(2),                                           //Channels
                NSNumber.FromInt32(16),                                          //PCMBitDepth
                NSNumber.FromBoolean(false),                                     //IsBigEndianKey
                NSNumber.FromBoolean(false)                                      //IsFloatKey
            };

            //Set up the NSObject Array of keys that will be combined with the values to make the NSDictionary
            var keys = new NSObject[]
            {
                AVAudioSettings.AVSampleRateKey,
                AVAudioSettings.AVFormatIDKey,
                AVAudioSettings.AVNumberOfChannelsKey,
                AVAudioSettings.AVLinearPCMBitDepthKey,
                AVAudioSettings.AVLinearPCMIsBigEndianKey,
                AVAudioSettings.AVLinearPCMIsFloatKey
            };

            //Set Settings with the Values and Keys to create the NSDictionary
            settings = NSDictionary.FromObjectsAndKeys(values, keys);

            //Set recorder parameters
            recorder = AVAudioRecorder.Create(url, new AudioSettings(settings), out error);
            //Set Recorder to Prepare To Record
            recorder.PrepareToRecord();
        }
Ejemplo n.º 16
0
        void InitializeRecorder()
        {
            var audioSession = AVAudioSession.SharedInstance();
            var err          = audioSession.SetCategory(AVAudioSessionCategory.PlayAndRecord);

            if (err != null)
            {
                Console.WriteLine("audioSession: {0}", err);
                return;
            }
            err = audioSession.SetActive(true);
            if (err != null)
            {
                Console.WriteLine("audioSession: {0}", err);
                return;
            }

            var audioFilePath = PortablePath.Combine(_localStorage, $"{_recordId}.wav");

            Console.WriteLine("Audio File Path: " + audioFilePath);

            _currentRecordUrl = NSUrl.FromFilename(audioFilePath);

            //set up the NSObject Array of values that will be combined with the keys to make the NSDictionary
            var values = new NSObject[]
            {
                NSNumber.FromFloat(_deviceService.AudioSampleRate),              //Sample Rate
                NSNumber.FromInt32((int)AudioToolbox.AudioFormatType.LinearPCM), //AVFormat
                NSNumber.FromInt32(2),                                           //Channels
                NSNumber.FromInt32(16),                                          //PCMBitDepth
                NSNumber.FromBoolean(false),                                     //IsBigEndianKey
                NSNumber.FromBoolean(false)                                      //IsFloatKey
            };

            //Set up the NSObject Array of keys that will be combined with the values to make the NSDictionary
            var keys = new NSObject[]
            {
                AVAudioSettings.AVSampleRateKey,
                AVAudioSettings.AVFormatIDKey,
                AVAudioSettings.AVNumberOfChannelsKey,
                AVAudioSettings.AVLinearPCMBitDepthKey,
                AVAudioSettings.AVLinearPCMIsBigEndianKey,
                AVAudioSettings.AVLinearPCMIsFloatKey
            };

            //Set Settings with the Values and Keys to create the NSDictionary
            _settings = NSDictionary.FromObjectsAndKeys(values, keys);

            //Set recorder parameters
            _recorder = AVAudioRecorder.Create(_currentRecordUrl, new AudioSettings(_settings), out _error);
            //Set Recorder to Prepare To Record
            _recorder.PrepareToRecord();
        }
Ejemplo n.º 17
0
        public void Start(string fileName, out string fileFullName)
        {
            //  var documentsPath = Path.GetTempPath();
            //  string libraryPath = Path.Combine(documentsPath, "MindCorners"); // Library folder
            // var filePath = Path.Combine(libraryPath, fileName);

            //var filePath = System.IO.Path.Combine(documentsPath,fileName);


            InitAudio();

            //Declare string for application temp path and tack on the file extension
            //string fileName = string.Format("Myfile{0}.wav", DateTime.Now.ToString("yyyyMMddHHmmss"));
            string audioFilePath = Path.Combine(Path.GetTempPath(), fileName);

            path         = audioFilePath;
            fileFullName = audioFilePath;
            Console.WriteLine("Audio File Path: " + audioFilePath);

            url = NSUrl.FromFilename(audioFilePath);
            //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),                                    //Sample Rate
                NSNumber.FromInt32((int)AudioToolbox.AudioFormatType.LinearPCM), //AVFormat
                NSNumber.FromInt32(2),                                           //Channels
                NSNumber.FromInt32(16),                                          //PCMBitDepth
                NSNumber.FromBoolean(false),                                     //IsBigEndianKey
                NSNumber.FromBoolean(false)                                      //IsFloatKey
            };

            //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.AVLinearPCMBitDepthKey,
                AVAudioSettings.AVLinearPCMIsBigEndianKey,
                AVAudioSettings.AVLinearPCMIsFloatKey
            };

            //Set Settings with the Values and Keys to create the NSDictionary
            settings = NSDictionary.FromObjectsAndKeys(values, keys);

            //Set recorder parameters
            recorder = AVAudioRecorder.Create(url, new AudioSettings(settings), out error);
            //Set Recorder to Prepare To Record
            recorder.PrepareToRecord();

            recorder.Record();
        }
Ejemplo n.º 18
0
        public Task <AudioRecordResult> Record(AudioRecordOptions options = null)
        {
            _options = options ?? AudioRecordOptions.Empty;

            _tcs = new TaskCompletionSource <AudioRecordResult>();

            var audioSession = AVAudioSession.SharedInstance();

            var err = audioSession.SetCategory(AVAudioSessionCategory.PlayAndRecord);

            if (err != null)
            {
                return(Task.FromResult(new AudioRecordResult($"AVAudioSession.SetCategory returned error '{err}'")));
            }

            err = audioSession.SetActive(true);
            if (err != null)
            {
                return(Task.FromResult(new AudioRecordResult($"AVAudioSession.SetActive returned error '{err}'")));
            }

            _audioFilePath = Path.Combine(Path.GetTempPath(), $"audiorec_{DateTime.Now.Ticks}.wav");

            Console.WriteLine("Audio File Path: " + _audioFilePath);

            var url = NSUrl.FromFilename(_audioFilePath);

            var config = new Dictionary <NSObject, NSObject>
            {
                { AVAudioSettings.AVSampleRateKey, NSNumber.FromFloat((float)_options.SampleRate) },
                { AVAudioSettings.AVFormatIDKey, NSNumber.FromInt32((int)AudioToolbox.AudioFormatType.LinearPCM) },
                { AVAudioSettings.AVNumberOfChannelsKey, NSNumber.FromInt32(1) },
                { AVAudioSettings.AVLinearPCMBitDepthKey, NSNumber.FromInt32(16) },
                { AVAudioSettings.AVLinearPCMIsBigEndianKey, NSNumber.FromBoolean(false) },
                { AVAudioSettings.AVLinearPCMIsFloatKey, NSNumber.FromBoolean(false) }
            };

            var settings = NSDictionary.FromObjectsAndKeys(config.Keys.ToArray(), config.Values.ToArray());

            _recorder = AVAudioRecorder.Create(url, new AudioSettings(settings), out err);
            if (err != null)
            {
                return(Task.FromResult(new AudioRecordResult($"AVAudioRecorder.Create returned error '{err}'")));
            }

            _recorder.PrepareToRecord();
            _recorder.Record();

            Task.Run(() => Timeout());

            return(_tcs.Task);
        }
Ejemplo n.º 19
0
        public void Create()
        {
            TestRuntime.RequestMicrophonePermission();

            var     url = NSUrl.FromFilename("/dev/null");
            NSError error;
            var     audioSettings = new AudioSettings(NSDictionary.FromObjectsAndKeys(Values, Keys));

            using (var recorder = AVAudioRecorder.Create(url, audioSettings, out error)) {
                Assert.NotNull(recorder);
                Assert.Null(error);
            }
        }
Ejemplo n.º 20
0
        void InitializeRecorder()
        {
            var audioSession = AVAudioSession.SharedInstance();
            var err          = audioSession.SetCategory(AVAudioSessionCategory.PlayAndRecord);

            if (err != null)
            {
                Console.WriteLine("audioSession: {0}", err);
                return;
            }

            err = audioSession.SetActive(true);
            if (error != null)
            {
                Console.WriteLine("audioSession: {0}", err);
                return;
            }

            var    localStorage  = PCLStorage.FileSystem.Current.LocalStorage.Path;
            string audioFilePath = localStorage + "/PatientNotes.wav";

            Console.WriteLine("Audio file path: " + audioFilePath);

            url = NSUrl.FromFilename(audioFilePath);

            var values = new NSObject[]
            {
                NSNumber.FromFloat(8000.0f),                                     // Sample Rate
                NSNumber.FromInt32((int)AudioToolbox.AudioFormatType.LinearPCM), // AVFormat
                NSNumber.FromInt32(1),                                           // Channels
                NSNumber.FromInt32(16),                                          // PCMBitDepth
                NSNumber.FromBoolean(false),                                     // IsBigEndianKey
                NSNumber.FromBoolean(false)                                      // IsFloatKey
            };

            var keys = new NSObject[]
            {
                AVAudioSettings.AVSampleRateKey,
                AVAudioSettings.AVFormatIDKey,
                AVAudioSettings.AVNumberOfChannelsKey,
                AVAudioSettings.AVLinearPCMBitDepthKey,
                AVAudioSettings.AVLinearPCMIsBigEndianKey,
                AVAudioSettings.AVLinearPCMIsFloatKey
            };

            settings = NSDictionary.FromObjectsAndKeys(values, keys);
            recorder = AVAudioRecorder.Create(url, new AudioSettings(settings), out error);
            recorder.PrepareToRecord();
        }
Ejemplo n.º 21
0
        public Task PlatformRecordAsync()
        {
            InitAudioSession();

            _audioFilePath = Path.Combine(Kit.CachePath, Kit.NewGuid + ".m4a");
            var url = NSUrl.FromFilename(_audioFilePath);

            _recorder = AVAudioRecorder.Create(url, new AudioSettings(_settings), out var error);
            if (error != null)
            {
                ThrowNSError(error);
            }
            _recorder.Record();
            return(Task.CompletedTask);
        }
Ejemplo n.º 22
0
        private void StartRecord()
        {
            if (!isRecording)
            {
                PerformSegue("showPopover", null);
                l_Result.Text            = "結果";
                iv_ResultFrogImage.Image = null;
                var    DocUrl   = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                string FileName = "Frog.wav";
                string FilePath = System.IO.Path.Combine(DocUrl, FileName);
                var    url      = new NSUrl(FilePath);

                NSError error = AVAudioSession.SharedInstance().SetCategory(AVAudioSessionCategory.PlayAndRecord);
                if (error != null)
                {
                    Console.WriteLine(error.LocalizedDescription);
                }

                var settings = new AudioSettings()
                {
                    Format                    = AudioToolbox.AudioFormatType.LinearPCM,
                    NumberChannels            = 2,
                    EncoderAudioQualityForVBR = AVAudioQuality.Max,
                    SampleRate                = 44100f,
                    LinearPcmBitDepth         = 16,
                    LinearPcmBigEndian        = true,
                    LinearPcmFloat            = true
                };

                audioRecorder = AVAudioRecorder.Create(url, settings, out error);
                if (error != null)
                {
                    audioRecorder = null;
                    Console.WriteLine(error.LocalizedDescription);
                }
                else
                {
                    audioRecorder.Delegate = new MyAVAudioRecorderDelegate(this);
                    audioRecorder.PrepareToRecord();
                    audioRecorder.Record();
                    bt_Record.SetImage(UIImage.FromBundle("recordingbtn"), UIControlState.Normal);
                    isRecording = true;
                }
            }
        }
Ejemplo n.º 23
0
        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);
        }
Ejemplo n.º 24
0
        private void prepareToRecord()
        {
            if (_audioSessionCreated)
            {
                //Declare string for application temp path and tack on the file extension
                _audioFileName = string.Format("{0}{1}.wav", _songName, DateTime.Now.ToString("yyyyMMddHHmmss"));
                _audioFilePath = Path.Combine(Path.GetTempPath(), _audioFileName);

                Console.WriteLine("Audio File Path: " + _audioFilePath);

                _url = NSUrl.FromFilename(_audioFilePath);
                //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),                                    //Sample Rate
                    NSNumber.FromInt32((int)AudioToolbox.AudioFormatType.LinearPCM), //AVFormat
                    NSNumber.FromInt32(2),                                           //Channels
                    NSNumber.FromInt32(16),                                          //PCMBitDepth
                    NSNumber.FromBoolean(false),                                     //IsBigEndianKey
                    NSNumber.FromBoolean(false)                                      //IsFloatKey
                };

                //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.AVLinearPCMBitDepthKey,
                    AVAudioSettings.AVLinearPCMIsBigEndianKey,
                    AVAudioSettings.AVLinearPCMIsFloatKey
                };

                //Set Settings with the Values and Keys to create the NSDictionary
                _settings = NSDictionary.FromObjectsAndKeys(values, keys);

                //Set recorder parameters
                _recorder = AVAudioRecorder.Create(_url, new AudioSettings(_settings), out _error);

                //Set Recorder to Prepare To Record
                _recorder.PrepareToRecord();
            }
        }
Ejemplo n.º 25
0
        public static string startRecordAudio()
        {
            if (settings == null)
            {
                initialize();
            }

            string fileName      = string.Format("MyRecording{0}.wav", DateTime.Now.ToString("yyyyMMddHHmmss"));
            string audioFilePath = Path.Combine(FilePaths.allAudiosPath, fileName);

            Console.WriteLine("Audio File Path: " + audioFilePath);

            url = NSUrl.FromFilename(audioFilePath);

            //Set recorder parameters
            recorder = AVAudioRecorder.Create(url, new AudioSettings(settings), out error);
            recorder.Record();

            return(audioFilePath);
        }
Ejemplo n.º 26
0
        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);
        }
Ejemplo n.º 27
0
        public void StartRecording(string fileName)
        {
            InitializeAudioSession();

            _fileName     = fileName + ".wav";
            _fullFilePath = Path.Combine(Path.GetTempPath(), _fileName);

            Console.WriteLine("Audio File Path: " + _fullFilePath);

            _url = NSUrl.FromFilename(_fullFilePath);
            //set up the NSObject Array of values that will be combined with the keys to make the NSDictionary
            NSObject[] values =
            {
                NSNumber.FromFloat(44100.0f),                                    //Sample Rate
                NSNumber.FromInt32((int)AudioToolbox.AudioFormatType.LinearPCM), //AVFormat
                NSNumber.FromInt32(2),                                           //Channels
                NSNumber.FromInt32(16),                                          //PCMBitDepth
                NSNumber.FromBoolean(false),                                     //IsBigEndianKey
                NSNumber.FromBoolean(false)                                      //IsFloatKey
            };

            //Set up the NSObject Array of keys that will be combined with the values to make the NSDictionary
            NSObject[] keys =
            {
                AVAudioSettings.AVSampleRateKey,
                AVAudioSettings.AVFormatIDKey,
                AVAudioSettings.AVNumberOfChannelsKey,
                AVAudioSettings.AVLinearPCMBitDepthKey,
                AVAudioSettings.AVLinearPCMIsBigEndianKey,
                AVAudioSettings.AVLinearPCMIsFloatKey
            };

            //Set Settings with the Values and Keys to create the NSDictionary
            _settings = NSDictionary.FromObjectsAndKeys(values, keys);

            //Set recorder parameters
            _recorder = AVAudioRecorder.Create(_url, new AudioSettings(_settings), out _);
            _recorder.PrepareToRecord();
            _recorder.Record();
        }
Ejemplo n.º 28
0
        private void InitialiseAudioRecorder()
        {
            var outputFilePath = CrossStorage.FileSystem.LocalStorage
                                 .CreateFile(this.outputFilename, CreationCollisionOption.ReplaceExisting)
                                 .FullPath;

            var url = NSUrl.FromFilename(outputFilePath);

            var values = new NSObject[]
            {
                NSNumber.FromFloat(this.SAMPLE_RATE),
                NSNumber.FromInt32(this.AUDIO_FORMAT_TYPE),
                NSNumber.FromInt32(this.CHANNEL),
                NSNumber.FromInt32(this.BITS_PER_SAMPLE),
                NSNumber.FromBoolean(this.IS_BIG_ENDIAN_KEY),
                NSNumber.FromBoolean(this.IS_FLOAT_KEY)
            };

            var keys = new NSObject[]
            {
                AVAudioSettings.AVSampleRateKey,
                AVAudioSettings.AVFormatIDKey,
                AVAudioSettings.AVNumberOfChannelsKey,
                AVAudioSettings.AVLinearPCMBitDepthKey,
                AVAudioSettings.AVLinearPCMIsBigEndianKey,
                AVAudioSettings.AVLinearPCMIsFloatKey
            };

            var settingsDictionary = NSDictionary.FromObjectsAndKeys(values, keys);
            var audioSettings      = new AudioSettings(settingsDictionary);

            this._audioRecorder = AVAudioRecorder.Create(url, audioSettings, out NSError error);

            if (error != null)
            {
                throw new MicrophoneServiceException("AudioRecorder creation error !");
            }
        }
Ejemplo n.º 29
0
        protected override RecordingResult NativeStartRecording(string fileName)
        {
            var documentsPath = NSSearchPath.GetDirectories(NSSearchPathDirectory.DocumentDirectory,
                                                            NSSearchPathDomain.User, true);

            var path         = $"{documentsPath[0]}/{fileName}.caf";
            var soundFileURL = new NSUrl(path);

            NSError error;

            AudioRecorder = AVAudioRecorder.Create(soundFileURL, audioSettings, out error);

            if (AudioRecorder == null)
            {
                Mvx.Trace(MvxTraceLevel.Error, "Error trying to create AVAudioRecorder. Got Exception: {0}", error.ToString());
                return(false);
            }

            AudioRecorder.PrepareToRecord();
            AudioRecorder.Record();

            return(new RecordingResult(true, path));
        }
Ejemplo n.º 30
0
        private void InitializeRecordService()
        {
            var audioSession = AVAudioSession.SharedInstance();
            var err          = audioSession.SetCategory(AVAudioSessionCategory.PlayAndRecord);

            if (err != null)
            {
                throw new RecorderException(err);
            }

            err = audioSession.SetActive(true);

            if (err != null)
            {
                throw new RecorderException(err);
            }

            this._url = NSUrl.FromFilename(this._path);
            var settings = this.GetAudioSettings();

            if (this._recorder == null)
            {
                this._recorder = AVAudioRecorder.Create(this._url, settings, out var error);

                if (error != null)
                {
                    throw new RecorderException(error);
                }
            }

            var ready = this._recorder.PrepareToRecord();

            if (!ready)
            {
                throw new RecorderException("PrepareToRecord() returned false");
            }
        }