Catrobat.IDE.Core.ViewModels.Editor.Sounds.SoundRecorderViewModel.PlayPauseAction C# (CSharp) Method

PlayPauseAction() private method

private PlayPauseAction ( ) : void
return void
        private void PlayPauseAction()
        {
            lock (ServiceLocator.SoundRecorderService)
            {
                ServiceLocator.SoundRecorderService.InitializeSound();

                if (IsPlaying)
                {
                    IsPlaying = false;
                    ServiceLocator.SoundRecorderService.StopRecording();
                    ServiceLocator.SoundRecorderService.StopPlayingRecordedSound();
                }
                else
                {
                    ServiceLocator.SoundRecorderService.StopRecording();
                    _playerStartTime = DateTime.UtcNow;
                    IsPlaying = true;

                    _playerTimeUpdateTask = new Task(delegate()
                        {
                            try
                            {
                                while (IsPlaying)
                                {
                                    _playerTimeGoneBy = DateTime.UtcNow - _playerStartTime;
                                    ServiceLocator.DispatcherService.RunOnMainThread(() =>
                                        {
                                            if (_playerTimeGoneBy.TotalSeconds < RecordingTime)
                                            {
                                                PlayingTime = _playerTimeGoneBy.TotalSeconds;
                                            }
                                            else
                                            {
                                                lock (ServiceLocator.SoundRecorderService)
                                                {
                                                    PlayingTime = RecordingTime;
                                                    IsPlaying = false;
                                                    ServiceLocator.SoundRecorderService.StopPlayingRecordedSound();
                                                }
                                            }
                                        });

                                    Task.Delay(45).Wait();
                                    //Thread.Sleep(45);
                                }
                                IsPlaying = false;
                            }
                            catch
                            {
                                /* Nothing here */
                            }
                        });

                    _playerTimeUpdateTask.Start();
                    ServiceLocator.SoundRecorderService.PlayRecordedSound();
                }
            }
        }