/// <summary> /// Starts or resumes the current utterance. /// </summary> public void Play() { if (!Ready) { return; } _client.Play(); }
public async void Speak(string text) { TtsClient speaker = new TtsClient(); if (speaker.CurrentState == State.Created) { await Prepare(speaker); } if (speaker.CurrentState == State.Ready) { speaker.AddText(text, speaker.DefaultVoice.Language, (int)speaker.DefaultVoice.VoiceType, speaker.GetSpeedRange().Normal); speaker.Play(); } }
public async Task SpeakAsync(string text, SpeechOptions options, CancellationToken cancelToken = default) { await Initialize(); if (tcsUtterances?.Task != null) { await tcsUtterances.Task; } tcsUtterances = new TaskCompletionSource <bool>(); if (cancelToken != null) { cancelToken.Register(() => { tts?.Stop(); tcsUtterances?.TrySetResult(true); }); } var language = "en_US"; var voiceType = Voice.Auto; if (options?.Locale.Language != null) { foreach (var voice in tts.GetSupportedVoices()) { if (voice.Language == options.Locale.Language) { language = voice.Language; voiceType = voice.VoiceType; } } } var pitch = 0; if (options?.Pitch.HasValue ?? false) { pitch = (int)Math.Round(options.Pitch.Value / PitchMax * tts.GetSpeedRange().Max, MidpointRounding.AwayFromZero); } tts.AddText(text, language, (int)voiceType, pitch); tts.Play(); await tcsUtterances.Task; }
/// <summary> /// Speak back text /// </summary> /// <param name="text">Text to speak</param> /// <param name="crossLocale">Locale of voice</param> /// <param name="pitch">Pitch of voice</param> /// <param name="speakRate">Speak Rate of voice (All) (0.0 - 2.0f)</param> /// <param name="volume">Volume of voice (iOS/WP) (0.0-1.0)</param> /// <param name="cancelToken">Canelation token to stop speak</param> /// <exception cref="ArgumentNullException">Thrown if text is null</exception> /// <exception cref="ArgumentException">Thrown if text length is greater than maximum allowed</exception> public async Task Speak(string text, CrossLocale?crossLocale = null, float?pitch = null, float?speakRate = null, float?volume = null, CancellationToken cancelToken = default(CancellationToken)) { if (text == null) { throw new ArgumentNullException(nameof(text), "Text can not be null"); } try { await semaphore.WaitAsync(cancelToken); ttsInst.AddText(text, crossLocale.Value.Language, 0, 0); ttsInst.Play(); } catch (Exception ex) { Console.Write(ex.Message); } finally { semaphore.Release(); } }