Ejemplo n.º 1
0
 private void IsAudioAlertEnabled_Click(object sender, RoutedEventArgs e)
 {
     if (IsEmailAlertsEnabled.IsChecked == true && SmtpServer.Text.Length == 0)
     {
         AudioFilePath.Focus();
     }
 }
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            View.BackgroundColor = UIColor.White;

            var image = new UIImageView();

            View.AddSubview(image);
            image.Image = UIImage.FromBundle("microphone.png");

            var recordingStatusLabel = new UILabel
            {
                Text      = "",
                TextColor = Constants.MainColor,
            };

            View.AddSubview(recordingStatusLabel);

            var lengthOfRecordingLabel = new UILabel
            {
                Text      = "",
                TextColor = Constants.MainColor,
            };

            View.AddSubview(lengthOfRecordingLabel);

            var startRecordingButton = new UIButton
            {
            };

            View.AddSubview(startRecordingButton);
            startRecordingButton.SetTitle("Start", UIControlState.Normal);
            startRecordingButton.SetTitleColor(Constants.MainColor, UIControlState.Normal);

            var stopRecordingButton = new UIButton
            {
            };

            View.AddSubview(stopRecordingButton);
            stopRecordingButton.SetTitle("Stop", UIControlState.Normal);
            stopRecordingButton.SetTitleColor(Constants.MainColor, UIControlState.Normal);

            var playRecordedSoundButton = new UIButton
            {
            };

            View.AddSubview(playRecordedSoundButton);
            playRecordedSoundButton.SetTitle("Play", UIControlState.Normal);
            playRecordedSoundButton.SetTitleColor(Constants.MainColor, UIControlState.Normal);

            // start recording wireup
            startRecordingButton.TouchUpInside += (sender, e) =>
            {
                AudioSession.Category = AudioSessionCategory.RecordAudio;
                AudioSession.SetActive(true);

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

                if (!Recorder.Record())
                {
                    recordingStatusLabel.Text = "Error recording";
                    return;
                }

                Stopwatch = new Stopwatch();
                Stopwatch.Start();
                lengthOfRecordingLabel.Text  = "";
                recordingStatusLabel.Text    = "Recording";
                startRecordingButton.Enabled = false;
                stopRecordingButton.Enabled  = true;
            };

            // stop recording wireup
            stopRecordingButton.TouchUpInside += (sender, e) =>
            {
                Recorder.Stop();

                lengthOfRecordingLabel.Text = string.Format("{0:hh\\:mm\\:ss}", Stopwatch.Elapsed);
                Stopwatch.Stop();
                recordingStatusLabel.Text    = "";
                startRecordingButton.Enabled = true;
                stopRecordingButton.Enabled  = false;
            };

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

            // play recorded sound wireup
            playRecordedSoundButton.TouchUpInside += (sender, e) =>
            {
                try
                {
                    Console.WriteLine("Playing Back Recording " + 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;

                    Player = new AVPlayer(AudioFilePath);
                    Player.Play();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("There was a problem playing back audio: ");
                    Console.WriteLine(ex.Message);
                }
            };

            #region Layout
            var topPad = (float)NavigationController.NavigationBar.Frame.Size.Height + 40f;

            View.ConstrainLayout(() =>
                                 image.Frame.Top == View.Frame.Top + topPad &&
                                 image.Frame.GetCenterX() == View.Frame.GetCenterX() &&
                                 image.Frame.Height == 100f &&
                                 image.Frame.Width == 100f &&

                                 startRecordingButton.Frame.Top == image.Frame.Bottom + 30f &&
                                 startRecordingButton.Frame.Left == View.Frame.Left + 30f &&
                                 startRecordingButton.Frame.Height == Constants.ControlsHeight &&
                                 startRecordingButton.Frame.Width == 100f &&

                                 recordingStatusLabel.Frame.Top == image.Frame.Bottom + 30f &&
                                 recordingStatusLabel.Frame.Left == startRecordingButton.Frame.Right + 30f &&
                                 recordingStatusLabel.Frame.Height == Constants.ControlsHeight &&
                                 recordingStatusLabel.Frame.Width == 100f &&

                                 stopRecordingButton.Frame.Top == startRecordingButton.Frame.Bottom + 30f &&
                                 stopRecordingButton.Frame.Left == View.Frame.Left + 30f &&
                                 stopRecordingButton.Frame.Height == Constants.ControlsHeight &&
                                 stopRecordingButton.Frame.Width == 100f &&

                                 lengthOfRecordingLabel.Frame.Top == startRecordingButton.Frame.Bottom + 30f &&
                                 lengthOfRecordingLabel.Frame.Left == stopRecordingButton.Frame.Right + 30f &&
                                 lengthOfRecordingLabel.Frame.Height == Constants.ControlsHeight &&
                                 lengthOfRecordingLabel.Frame.Width == 100f &&

                                 playRecordedSoundButton.Frame.Top == stopRecordingButton.Frame.Bottom + 30f &&
                                 playRecordedSoundButton.Frame.Left == View.Frame.Left + 30f &&
                                 playRecordedSoundButton.Frame.Height == Constants.ControlsHeight &&
                                 playRecordedSoundButton.Frame.Width == 100f
                                 );
            #endregion
        }