コード例 #1
0
ファイル: Voice.cs プロジェクト: danielrinconr/BatteryMonitor
        private Task SpeakMsgs()
        {
            CancellationToken = new CancellationTokenSource();
            var token = CancellationToken.Token;

            TskSpeakMsgs = Task.Run(() =>
            {
                Prompt prompt = null;
                try
                {
                    if (_synth.State != SynthesizerState.Paused)
                    {
                        while (Msgs.Count > 0)
                        {
                            prompt = _synth.SpeakAsync(Msgs.Dequeue());
                            Thread.Sleep(100);
                            while (_synth.State == SynthesizerState.Speaking)
                            {
                                token.ThrowIfCancellationRequested();
                                Thread.Sleep(100);
                            }
                        }
                        SpkCompleted();
                    }
                    DefaultPlaybackDevice?.SetVolumeAsync(PrevVol, token);
                    if (WasMuted)
                    {
                        DefaultPlaybackDevice?.SetMuteAsync(true, token);
                    }
                }
                catch (Exception canceledException)
                {
                    if (prompt != null)
                    {
                        _synth.SpeakAsyncCancel(prompt);
                    }
                    else
                    {
                        _synth.SpeakAsyncCancelAll();
                    }
                    Debug.WriteLine("Task was cancelled");
                    Debug.WriteLine(canceledException.Message);
                }
            }, token);
            return(TskSpeakMsgs);
        }
コード例 #2
0
ファイル: Voice.cs プロジェクト: danielrinconr/BatteryMonitor
        public async void AddMessage(string msg)
        {
            try
            {
                // Wait until the Volume Controller is loaded.
                if (TskLoadVolController.Status == TaskStatus.Running)
                {
                    TskLoadVolController.Wait();
                }
                Msgs.Enqueue(msg);
                if (DefaultPlaybackDevice == null)
                {
                    throw new Exception("There is no device to play alert.");
                }

                // ReSharper disable once AssignmentInConditionalExpression
                if (WasMuted = DefaultPlaybackDevice.IsMuted)
                {
                    await DefaultPlaybackDevice.SetMuteAsync(false);
                }

                PrevVol = DefaultPlaybackDevice.Volume;
                if (PrevVol <= 5)
                {
                    PrevVol = 5;
                }
                await DefaultPlaybackDevice.SetVolumeAsync(AuxNotVolume);

                await SpeakMsgs();

                Debug.WriteLine($"Launching new thread with the message: {msg}");
            }
            catch (Exception exc)
            {
                throw new Exception(exc.Message);
            }
        }