partial void HelloPress(NSObject sender) { labelInfo.StringValue = "Hello world!"; NSSpeechSynthesizer synth = new NSSpeechSynthesizer(); synth.StartSpeakingString("Hello world!"); }
/// <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 (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"); } var tcs = new TaskCompletionSource <object>(); var handler = new EventHandler((sender, args) => tcs.TrySetResult(null)); try { await semaphore.WaitAsync(cancelToken); void OnCancel() { speechSynthesizer.StopSpeaking(); tcs.TrySetCanceled(); } using (cancelToken.Register(OnCancel)) { if (volume.HasValue) { speechSynthesizer.Volume = NormalizeVolume(volume); } if (speakRate.HasValue) { speechSynthesizer.Rate = speakRate.Value; } if (crossLocale.HasValue) { speechSynthesizer.Voice = crossLocale.Value.Language; } sdelegate.FinishedSpeaking += handler; speechSynthesizer.StartSpeakingString(text); await tcs.Task; } } finally { sdelegate.FinishedSpeaking -= handler; if (semaphore.CurrentCount == 0) { semaphore.Release(); } } }
partial void btnSpeakHandler(Foundation.NSObject sender) { string value = textField.StringValue; // Is the string 0 length? if (value.Length == 0) { return; } mSpeechSynth.StartSpeakingString(value); btnStop.Enabled = true; btnSpeak.Enabled = false; voicesTableView.Enabled = false; textField.Enabled = false; }
/// <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, float?pitch, float?speakRate, float?volume, CancellationToken?cancelToken) { if (text == null) { throw new ArgumentNullException(nameof(text), "Text can not be null"); } var tcs = new TaskCompletionSource <object>(); var handler = new EventHandler((sender, args) => tcs.TrySetResult(null)); try { var ct = cancelToken ?? CancellationToken.None; await semaphore.WaitAsync(ct); using (ct.Register(() => { speechSynthesizer.StopSpeaking(); tcs.TrySetCanceled(); })) { speechSynthesizer.Volume = NormalizeVolume(volume); if (speakRate != null) { speechSynthesizer.Rate = speakRate.Value; } if (crossLocale != null) { speechSynthesizer.Voice = crossLocale.Value.Language; } sdelegate.FinishedSpeaking += handler; speechSynthesizer.StartSpeakingString(text); await tcs.Task; } } finally { semaphore.Release(); sdelegate.FinishedSpeaking -= handler; } }
public static void StartSpeaking(string text, SystemVoice voice) { speech.Voice = voice.Identifier; speech.StartSpeakingString(text); }