Esempio n. 1
0
 public void StartRecording()
 {
     if (_audioSessionCreated)
     {
         _recorder.Record();
     }
 }
Esempio n. 2
0
        void StartRecording()
        {
            var     session = AVAudioSession.SharedInstance();
            NSError error   = null;

            session.SetCategory(AVAudioSession.CategoryRecord, out error);
            if (error != null)
            {
                return;
            }
            session.SetActive(true, out error);
            if (error != null)
            {
                return;
            }
            if (!PrepareAudioRecording())
            {
                return;
            }
            if (!recorder.Record())
            {
                return;
            }

            timer = NSTimer.CreateRepeatingScheduledTimer(TimeSpan.FromSeconds(.1), UpdateMeter);
        }
 public void StartRecording()
 {
     if (recorder == null)
     {
         InitializeRecorder();
     }
     recorder.Record();
 }
Esempio n. 4
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();
        }
Esempio n. 5
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);
        }
        public void Record()
        {
            if (_recorder != null)
            {
                _recorder.Record();

                if (_timer == null)
                {
                    _timer          = new Timer(100);
                    _timer.Elapsed += OnTimedEvent;
                }

                _timer.Start();
            }
        }
Esempio n. 7
0
        partial void StartRecording(UIButton sender)
        {
            Console.WriteLine("Begin Recording");

            var session = AVAudioSession.SharedInstance();

            session.RequestRecordPermission((granted) =>
            {
                Console.WriteLine($"Audio Permission: {granted}");

                if (granted)
                {
                    session.SetCategory(AVAudioSession.CategoryRecord, out NSError error);
                    if (error == null)
                    {
                        session.SetActive(true, out error);
                        if (error != null)
                        {
                            Status = Status.PreparingError;
                        }
                        else
                        {
                            var isPrepared = PrepareAudioRecording() && recorder.Record();
                            if (isPrepared)
                            {
                                stopwatch = new Stopwatch();
                                stopwatch.Start();
                                Status = Status.Recording;
                            }
                            else
                            {
                                Status = Status.PreparingError;
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine(error.LocalizedDescription);
                    }
                }
                else
                {
                    Console.WriteLine("YOU MUST ENABLE MICROPHONE PERMISSION");
                }
            });
        }
Esempio n. 8
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;
                }
            }
        }
Esempio n. 9
0
        private static void StartRecording()
        {
            audioRecorder.PrepareToRecord();
            audioRecorder.Record();
            audioPlayer = null;

            var actionSheet = new UIActionSheet(TouchFactory.Instance.GetResourceString("Recording"))
            {
                TouchFactory.Instance.GetResourceString("StopRecording"),
            };

            actionSheet.Style = UIActionSheetStyle.BlackTranslucent;
            actionSheet.ShowInView(TouchFactory.Instance.TopViewController.View);
            actionSheet.Dismissed += delegate(object sender, UIButtonEventArgs args)
            {
                DoneRecording();
            };
        }
Esempio n. 10
0
        void OnStartRecording(object sender, EventArgs e)
        {
            Console.WriteLine("Begin Recording");

            var session = AVAudioSession.SharedInstance();

            NSError error = null;

            session.SetCategory(AVAudioSession.CategoryRecord, out error);
            if (error != null)
            {
                Console.WriteLine(error);
                return;
            }

            session.SetActive(true, out error);
            if (error != null)
            {
                Console.WriteLine(error);
                return;
            }

            if (!PrepareAudioRecording())
            {
                RecordingStatusLabel.Text = "Error preparing";
                return;
            }

            if (!recorder.Record())
            {
                RecordingStatusLabel.Text = "Error preparing";
                return;
            }

            stopwatch = new Stopwatch();
            stopwatch.Start();

            LengthOfRecordingLabel.Text     = "";
            RecordingStatusLabel.Text       = "Recording";
            StartRecordingButton.Enabled    = false;
            StopRecordingButton.Enabled     = true;
            PlayRecordedSoundButton.Enabled = false;
        }
Esempio n. 11
0
        private void RecordingButtonAction(object item, EventArgs args)
        {
            if (m_isRecording == false)
            {
                m_isRecording = true;

                //UI Update
                {
                    this.ShowNavigationButton(false);

                    m_recordButton.TintColor = RecordingTintColor;
                    m_playButton.Enabled     = false;
                    m_trashButton.Enabled    = false;
                }

                if (File.Exists(m_recordingFilePath))
                {
                    File.Delete(m_recordingFilePath);
                }

                m_oldSessionCategory = AVAudioSession.SharedInstance().Category;

                AVAudioSession.SharedInstance().SetCategory(AVAudioSessionCategory.Record);
                m_audioRecorder.PrepareToRecord();
                m_audioRecorder.Record();
            }
            else
            {
                m_isRecording = false;

                //UI Update
                {
                    this.ShowNavigationButton(true);

                    m_recordButton.TintColor = NormalTintColor;
                    m_playButton.Enabled     = true;
                    m_trashButton.Enabled    = true;
                }

                m_audioRecorder.Stop();
                AVAudioSession.SharedInstance().SetCategory(new NSString(m_oldSessionCategory));
            }
        }
Esempio n. 12
0
        static void RunBackground()
        {
            BackgroundTypes mode = (BackgroundTypes)(int)NSUserDefaults.StandardUserDefaults.IntForKey(UserDefaultsKeys.BackgroundModeType);

            switch (mode)
            {
            case BackgroundTypes.Microphone:
                if (!audioRecorder.Recording)
                {
                    audioRecorder.Record();
                }
                backgroundRunning = true;
                break;

            case BackgroundTypes.Music:
                audioObserver = NSNotificationCenter.DefaultCenter.AddObserver(AVAudioSession.InterruptionNotification, InteruptedAudio, AVAudioSession.SharedInstance());
                PlayAudio();
                backgroundRunning = true;
                break;
            }
        }
Esempio n. 13
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();
        }
Esempio n. 14
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));
        }
Esempio n. 15
0
        public async Task <bool> BeginCaptureAudio()
        {
            var tcs = new TaskCompletionSource <bool> ();

            AVAudioSession.SharedInstance().RequestRecordPermission(granted => tcs.TrySetResult(granted));
            if (!await tcs.Task)
            {
                return(false);
            }

            AVAudioSession.SharedInstance().SetCategory(AVAudioSessionCategory.Record);
            AVAudioSession.SharedInstance().SetActive(true);

            NSError error;
            var     settings = new AudioSettings
            {
                Format             = AudioFormatType.LinearPCM,
                SampleRate         = 8000,
                NumberChannels     = 1,
                LinearPcmBitDepth  = 16,
                LinearPcmFloat     = false,
                LinearPcmBigEndian = false,
            };
            var recordFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".wav");

            _audioRecorder = AVAudioRecorder.Create(NSUrl.FromFilename(recordFile), settings, out error);
            if (_audioRecorder != null && _audioRecorder.PrepareToRecord())
            {
                _audioRecorder.Record();
            }
            else
            {
                _audioRecorder = null;
                return(false);
            }
            return(true);
        }
Esempio n. 16
0
        public static void StartRecording(string FileName)
        {
            NSObject[] values = new NSObject[]
            {
                NSNumber.FromFloat(44100.0f),
                NSNumber.FromInt32((int)AudioFormatType.LinearPCM),
                NSNumber.FromInt32(1),
                NSNumber.FromInt32((int)AVAudioQuality.High)
            };

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

            NSDictionary settings = NSDictionary.FromObjectsAndKeys(values, keys);
            NSUrl        url      = NSUrl.FromFilename(FileName);

            // Set recorder parameters
            NSError error;

            _recorder = AVAudioRecorder.ToUrl(url, settings, out error);
            if (_recorder == null)
            {
                Console.WriteLine(error);
                return;
            }

            // Set Metering Enabled so you can get the time of the wav file
            _recorder.MeteringEnabled = true;
            _recorder.PrepareToRecord();
            _recorder.Record();
        }
        public void StartRecording()
        {
            NSError error;
            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 documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            var temp = Path.Combine(documents, "..", "tmp");
            _recordedFileName = Path.Combine(temp, Guid.NewGuid() + ".mp4");

            using (var url = new NSUrl(_recordedFileName))
            {

                //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.MPEG4AAC), //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
                var 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();
        }
Esempio n. 18
0
        public static void StartRecording(string FileName)
        {
            NSObject[] values = new NSObject[]
            {
                NSNumber.FromFloat(44100.0f),
                NSNumber.FromInt32((int)AudioFormatType.LinearPCM),
                NSNumber.FromInt32(1),
                NSNumber.FromInt32((int)AVAudioQuality.High)
            };

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

            NSDictionary settings = NSDictionary.FromObjectsAndKeys (values, keys);
            NSUrl url = NSUrl.FromFilename(FileName);

            // Set recorder parameters
            NSError error;
            _recorder = AVAudioRecorder.ToUrl (url, settings, out error);
            if (_recorder == null){
                Console.WriteLine (error);
                return;
            }

            // Set Metering Enabled so you can get the time of the wav file
            _recorder.MeteringEnabled = true;
            _recorder.PrepareToRecord();
            _recorder.Record();
        }