/// <summary> /// Updates the meters. /// </summary> private void UpdateMeters() { if (m_audioRecorder.Recording) { m_audioRecorder.UpdateMeters(); var normalizedValue = Math.Pow(10, m_audioRecorder.AveragePower(0) / 20); mMusicFlowView.WaveColor = RecordingTintColor; mMusicFlowView.UpdateWithLevel((nfloat)normalizedValue); this.NavigationItem.Title = NSStringExtensions.TimeStringForTimeInterval(m_audioRecorder.currentTime); } else if (m_audioPlayer != null && m_audioPlayer.Playing) { m_audioPlayer.UpdateMeters(); var normalizedValue = Math.Pow(10, m_audioPlayer.AveragePower(0) / 20); mMusicFlowView.WaveColor = PlayingTintColor; mMusicFlowView.UpdateWithLevel((nfloat)normalizedValue); } else { mMusicFlowView.WaveColor = NormalTintColor; mMusicFlowView.UpdateWithLevel(0); } }
/// <summary> /// Updates the play progress. /// </summary> private void UpdatePlayProgress() { m_labelCurrentTime.Text = NSStringExtensions.TimeStringForTimeInterval(m_audioPlayer.CurrentTime); m_labelRemainingTime.Text = NSStringExtensions.TimeStringForTimeInterval((m_ShouldShowRemainingTime) ? (m_audioPlayer.Duration - m_audioPlayer.CurrentTime) : m_audioPlayer.Duration); m_playerSlider.SetValue((float)m_audioPlayer.CurrentTime, true); }
/// <summary> /// Plaies the action. /// </summary> /// <param name="item">Item.</param> /// <param name="args">Arguments.</param> private void PlayAction(object item, EventArgs args) { m_oldSessionCategory = AVAudioSession.SharedInstance().Category; AVAudioSession.SharedInstance().SetCategory(AVAudioSessionCategory.Playback); // m_audioPlayer = AVAudioPlayer.FromUrl(NSUrl.FromFilename(m_recordingFilePath)); m_audioPlayer.WeakDelegate = this; m_audioPlayer.MeteringEnabled = true; m_audioPlayer.PrepareToPlay(); m_audioPlayer.Play(); //UI Update { this.SetToolbarItems(new UIBarButtonItem[] { m_pauseButton, m_flexItem1, m_recordButton, m_flexItem2, m_trashButton }, true); this.ShowNavigationButton(false); m_recordButton.Enabled = false; m_trashButton.Enabled = false; } //Start regular update { m_playerSlider.Value = (float)m_audioPlayer.CurrentTime; m_playerSlider.MaxValue = (float)m_audioPlayer.Duration; m_viewPlayerDuration.Frame = this.NavigationController.NavigationBar.Bounds; m_labelCurrentTime.Text = NSStringExtensions.TimeStringForTimeInterval(m_audioPlayer.CurrentTime); m_labelRemainingTime.Text = NSStringExtensions.TimeStringForTimeInterval((m_ShouldShowRemainingTime) ? (m_audioPlayer.Duration - m_audioPlayer.CurrentTime): m_audioPlayer.Duration); m_viewPlayerDuration.SetNeedsLayout(); m_viewPlayerDuration.LayoutIfNeeded(); this.NavigationItem.TitleView = m_viewPlayerDuration; if (mplayProgressDisplayLink != null) { mplayProgressDisplayLink.Invalidate(); } mplayProgressDisplayLink = CADisplayLink.Create(UpdatePlayProgress); mplayProgressDisplayLink.AddToRunLoop(NSRunLoop.Current, NSRunLoopMode.Common); } }
/// <summary> /// Views the did load. /// </summary> public override void ViewDidLoad() { base.ViewDidLoad(); // m_navigationTitle = @"Audio Recorder"; // this.View.TintColor = NormalTintColor; mMusicFlowView.BackgroundColor = this.View.BackgroundColor; mMusicFlowView.IdleAmplitude = 0; //Unique recording URL var fileName = NSProcessInfo.ProcessInfo.GloballyUniqueString; var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); var tmp = Path.Combine(documents, "..", "tmp"); m_recordingFilePath = Path.Combine(tmp, String.Format("{0}.m4a", fileName)); { m_flexItem1 = new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace, null, null); m_flexItem2 = new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace, null, null); var img = UIImage.FromBundle("audio_record"); m_recordButton = new UIBarButtonItem(img, UIBarButtonItemStyle.Plain, RecordingButtonAction); m_playButton = new UIBarButtonItem(UIBarButtonSystemItem.Play, PlayAction); m_pauseButton = new UIBarButtonItem(UIBarButtonSystemItem.Pause, PauseAction); m_trashButton = new UIBarButtonItem(UIBarButtonSystemItem.Trash, DeleteAction); this.SetToolbarItems(new UIBarButtonItem[] { m_playButton, m_flexItem1, m_recordButton, m_flexItem2, m_trashButton }, false); m_playButton.Enabled = false; m_trashButton.Enabled = false; } // Define the recorder setting { var audioSettings = new AudioSettings() { Format = AudioFormatType.MPEG4AAC, SampleRate = 44100.0f, NumberChannels = 2, }; NSError err = null; m_audioRecorder = AVAudioRecorder.Create(NSUrl.FromFilename(m_recordingFilePath), audioSettings, out err); // Initiate and prepare the recorder m_audioRecorder.WeakDelegate = this; m_audioRecorder.MeteringEnabled = true; mMusicFlowView.PrimaryWaveLineWidth = 3.0f; mMusicFlowView.SecondaryWaveLineWidth = 1.0f; } //Navigation Bar Settings { this.NavigationItem.Title = @"Audio Recorder"; m_cancelButton = new UIBarButtonItem(UIBarButtonSystemItem.Cancel, CancelAction); this.NavigationItem.LeftBarButtonItem = m_cancelButton; m_doneButton = new UIBarButtonItem(UIBarButtonSystemItem.Done, DoneAction); } //Player Duration View { m_viewPlayerDuration = new UIView(); m_viewPlayerDuration.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight; m_viewPlayerDuration.BackgroundColor = UIColor.Clear; m_labelCurrentTime = new UILabel(); m_labelCurrentTime.Text = NSStringExtensions.TimeStringForTimeInterval(0); m_labelCurrentTime.Font = UIFont.BoldSystemFontOfSize(14.0f); m_labelCurrentTime.TextColor = NormalTintColor; m_labelCurrentTime.TranslatesAutoresizingMaskIntoConstraints = false; m_playerSlider = new UISlider(new CGRect(0, 0, this.View.Bounds.Size.Width, 64)); m_playerSlider.MinimumTrackTintColor = PlayingTintColor; m_playerSlider.Value = 0; m_playerSlider.TouchDown += SliderStart; m_playerSlider.ValueChanged += SliderMoved; m_playerSlider.TouchUpInside += SliderEnd; m_playerSlider.TouchUpOutside += SliderEnd; m_playerSlider.TranslatesAutoresizingMaskIntoConstraints = false; m_labelRemainingTime = new UILabel(); m_labelCurrentTime.Text = NSStringExtensions.TimeStringForTimeInterval(0); m_labelRemainingTime.UserInteractionEnabled = true; m_labelRemainingTime.AddGestureRecognizer(new UITapGestureRecognizer(TapRecognizer)); m_labelRemainingTime.Font = m_labelCurrentTime.Font; m_labelRemainingTime.TextColor = m_labelCurrentTime.TextColor; m_labelRemainingTime.TranslatesAutoresizingMaskIntoConstraints = false; m_viewPlayerDuration.Add(m_labelCurrentTime); m_viewPlayerDuration.Add(m_playerSlider); m_viewPlayerDuration.Add(m_labelRemainingTime); NSLayoutConstraint constraintCurrentTimeLeading = NSLayoutConstraint.Create(m_labelCurrentTime, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, m_viewPlayerDuration, NSLayoutAttribute.Leading, 1.0f, 10.0f); NSLayoutConstraint constraintCurrentTimeTrailing = NSLayoutConstraint.Create(m_playerSlider, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, m_labelCurrentTime, NSLayoutAttribute.Trailing, 1.0f, 10); NSLayoutConstraint constraintRemainingTimeLeading = NSLayoutConstraint.Create(m_labelRemainingTime, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, m_playerSlider, NSLayoutAttribute.Trailing, 1.0f, 10.0f); NSLayoutConstraint constraintRemainingTimeTrailing = NSLayoutConstraint.Create(m_viewPlayerDuration, NSLayoutAttribute.Trailing, NSLayoutRelation.Equal, m_labelRemainingTime, NSLayoutAttribute.Trailing, 1.0f, 10.0f); NSLayoutConstraint constraintCurrentTimeCenter = NSLayoutConstraint.Create(m_labelCurrentTime, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, m_viewPlayerDuration, NSLayoutAttribute.CenterY, 1.0f, 0.0f); NSLayoutConstraint constraintSliderCenter = NSLayoutConstraint.Create(m_playerSlider, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, m_viewPlayerDuration, NSLayoutAttribute.CenterY, 1.0f, 0.0f); NSLayoutConstraint constraintRemainingTimeCenter = NSLayoutConstraint.Create(m_labelRemainingTime, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, m_viewPlayerDuration, NSLayoutAttribute.CenterY, 1.0f, 0.0f); m_viewPlayerDuration.AddConstraints(new NSLayoutConstraint[] { constraintCurrentTimeLeading, constraintCurrentTimeTrailing, constraintRemainingTimeLeading, constraintRemainingTimeTrailing, constraintCurrentTimeCenter, constraintSliderCenter, constraintRemainingTimeCenter }); } }