Пример #1
0
        private void RecordButton_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (!IsHitTestVisible)
            {
                return;
            }
            var microphoneState = _microphone != null ? _microphone.State : (MicrophoneState?)null;

            if (microphoneState == MicrophoneState.Started)
            {
                return;
            }
            if (_isHintStoryboardPlaying)
            {
                return;
            }

            Log(string.Format("microphone_state={0} storyboard_state={1}", microphoneState, _isHintStoryboardPlaying));

            if (Component == null)
            {
                return;
            }
            if (_asyncDispatcher == null)
            {
                _asyncDispatcher = new XnaAsyncDispatcher(TimeSpan.FromMilliseconds(33), OnTimerTick);
            }

            if (SliderPanel == null)
            {
                SliderPanel = CreateSliderPanel();
                LayoutRoot.Children.Add(SliderPanel);
            }
            if (TimerPanel == null)
            {
                TimerPanel = CreateTimerPanel();
                LayoutRoot.Children.Add(TimerPanel);
            }

            if (_microphone == null)
            {
                _microphone = Microphone.Default;

                ShellViewModel.WriteTimer("AudioRecorderControl ctor microphone");

                if (_microphone == null)
                {
                    RecordButton.Visibility = Visibility.Collapsed;
                    Visibility       = Visibility.Collapsed;
                    IsHitTestVisible = false;

                    return;
                }

                try
                {
                    _microphone.BufferDuration = TimeSpan.FromMilliseconds(240);
                    _duration = _microphone.BufferDuration;
                    _buffer   = new byte[_microphone.GetSampleSizeInBytes(_microphone.BufferDuration)];
                }
                catch (Exception ex)
                {
                    TLUtils.WriteException(ex);

                    RecordButton.Visibility = Visibility.Collapsed;
                    Visibility       = Visibility.Collapsed;
                    IsHitTestVisible = false;

                    return;
                }

                _microphone.BufferReady += Microphone_OnBufferReady;
            }

            _skipBuffersCount = 0;
            _fileId           = TLLong.Random();
            _fileName         = _fileId.Value + ".mp3";
            _isPartReady      = true;
            _uploadingLength  = 0;
            _uploadableParts.Clear();

            _isSliding       = true;
            _stopRequested   = false;
            _cancelRequested = false;

            RaiseRecordStarted();

            if (Duration != null)
            {
                Duration.Text = "00:00.00";
            }
            if (Slider != null)
            {
                ((TranslateTransform)Slider.RenderTransform).X = 0.0;
            }
            Component.StartRecord(ApplicationData.Current.LocalFolder.Path + "\\" + _fileName);

            _stream    = new MemoryStream();
            _startTime = DateTime.Now;
            VibrateController.Default.Start(TimeSpan.FromMilliseconds(25));

            Execute.BeginOnUIThread(TimeSpan.FromMilliseconds(25.0), () =>
            {
                if (!_isSliding)
                {
                    if (_stopRequested)
                    {
                        _stopRequested   = false;
                        _cancelRequested = false;

                        _isHintStoryboardPlaying = true;
                        HintStoryboard.Begin();
                        return;
                    }
                    Log("_isSliding=false return");
                    return;
                }

                if (Slider != null)
                {
                    Slider.Visibility = Visibility.Visible;
                }
                if (TimerPanel != null)
                {
                    TimerPanel.Visibility = Visibility.Visible;
                }

                _asyncDispatcher.StartService(null);
                _microphone.Start();

                StartRecordingStoryboard();
            });
        }
Пример #2
0
        private void Microphone_OnBufferReady(object sender, System.EventArgs e)
        {
            const int skipStartBuffersCount = 1;

            if (Component == null)
            {
                return;
            }

            var dataLength = _microphone.GetData(_buffer);

            if (_skipBuffersCount < skipStartBuffersCount)
            {
                _skipBuffersCount++;
                return;
            }

            const int frameLength = 1920;
            var       partsCount  = dataLength / frameLength;

            _stream.Write(_buffer, 0, _buffer.Length);
            for (var i = 0; i < partsCount; i++)
            {
                var count  = frameLength * (i + 1) > _buffer.Length ? _buffer.Length - frameLength * i : frameLength;
                var result = Component.WriteFrame(_buffer.SubArray(frameLength * i, count), count);
            }

            if (_stopRequested || _cancelRequested)
            {
                _microphone.Stop();
                _asyncDispatcher.StopService();
                Component.StopRecord();

                if (UploadFileDuringRecording)
                {
                    UploadAudioFileAsync(true);
                }

                if (_stopRequested)
                {
                    if ((DateTime.Now - _startTime).TotalMilliseconds < 1000.0)
                    {
                        _stopRequested   = false;
                        _cancelRequested = false;
                        //Log("HintStoryboard_OnCompleted._stopRequested=false");

                        _isHintStoryboardPlaying = true;
                        HintStoryboard.Begin();
                        return;
                    }

                    RaiseAudioRecorded(_stream, (DateTime.Now - _startTime - TimeSpan.FromTicks(_microphone.BufferDuration.Ticks * skipStartBuffersCount)).TotalSeconds, _fileName, _fileId, _uploadableParts);
                    return;
                }

                if (_cancelRequested)
                {
                    RaiseRecordCanceled();
                    return;
                }
            }
            else
            {
                var now = DateTime.Now;
                if (!_lastTypingTime.HasValue ||
                    _lastTypingTime.Value.AddSeconds(1.0) < now)
                {
                    _lastTypingTime = DateTime.Now;
                    RaiseRecordingAudio();
                }

                if (UploadFileDuringRecording)
                {
                    UploadAudioFileAsync(false);
                }
            }
        }