Ejemplo n.º 1
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 = 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);
        }
Ejemplo n.º 2
0
        public void StartRecordingFroMic(string FilePath)
        {
            // set some default values for recording settings
            NSObject[] keys = new NSObject[]
            {
                AVAudioSettings.AVSampleRateKey,
                AVAudioSettings.AVFormatIDKey,
                AVAudioSettings.AVNumberOfChannelsKey,
                AVAudioSettings.AVEncoderAudioQualityKey,
            };
            NSObject[] values = new NSObject[]
            {
                NSNumber.FromFloat(44100.0f),
                NSNumber.FromInt32((int)AudioFileType.WAVE),
                NSNumber.FromInt32(1),
                NSNumber.FromInt32((int)AVAudioQuality.Max),
            };
            NSDictionary settings = NSDictionary.FromObjectsAndKeys(values, keys);

            // define a filename and location for the output file
            string fileName = FilePath;
            string path     = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), fileName);

            outputFileUrl = NSUrl.FromFilename(path);

            // pass the configured url to the AVAudioRecorder object
            NSError error = new NSError();

            _mic = AVAudioRecorder.ToUrl(outputFileUrl, settings, out error);

            if (error != null)
            {
                // prepare and start recording
                _mic.PrepareToRecord();
                _mic.Record();
            }
            else
            {
                throw new Exception("Error loading mic: " + error.ToString());
            }
        }
Ejemplo n.º 3
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();
        }