async Task SpeakUtterance(AVSpeechUtterance speechUtterance, CancellationToken cancelToken) { AVSpeechSynthesizer speechSynthesizer = new AVSpeechSynthesizer(); try { currentSpeak = new TaskCompletionSource <object>(); speechSynthesizer.DidFinishSpeechUtterance += OnFinishedSpeechUtterance; speechSynthesizer.SpeakUtterance(speechUtterance); using (cancelToken.Register(TryCancel)) { await currentSpeak.Task; } } finally { speechSynthesizer.DidFinishSpeechUtterance -= OnFinishedSpeechUtterance; speechSynthesizer.Dispose(); } void OnFinishedSpeechUtterance(object sender, AVSpeechSynthesizerUteranceEventArgs args) => currentSpeak?.TrySetResult(null); void TryCancel() { speechSynthesizer?.StopSpeaking(AVSpeechBoundary.Word); currentSpeak?.TrySetCanceled(); } }
public override void ViewDidDisappear(bool animated) { base.ViewDidDisappear(animated); // Check if sample is being closed. if (NavigationController?.ViewControllers == null) { // Stop the location data source. _myMapView.LocationDisplay?.DataSource?.StopAsync(); // Stop the speech synthesizer. _speechSynthesizer.StopSpeaking(AVSpeechBoundary.Word); _speechSynthesizer.Dispose(); // Stop the tracker. if (_tracker != null) { _tracker.TrackingStatusChanged -= TrackingStatusUpdated; _tracker.NewVoiceGuidance -= SpeakDirection; _tracker.RerouteStarted -= RerouteStarted; _tracker.RerouteCompleted -= RerouteCompleted; _tracker = null; } } // Unsubscribe from events, per best practice. _navigateButton.Clicked -= StartNavigation; _recenterButton.Clicked -= RecenterButton_Click; }
partial void OnDispose(bool disposing) { if (disposing) { speechSynthesizer?.Dispose(); } }
public void Dispose() { synthesizer.DidFinishSpeechUtterance -= OnFinishedSpeechUtterance; utterance.Dispose(); synthesizer.Dispose(); utterance = null; synthesizer = null; }
/// <summary> /// Dispose of TTS /// </summary> public void Dispose() { if (speechSynthesizer != null) { speechSynthesizer.Dispose(); speechSynthesizer = null; } }
public IObservable<Unit> SpeakAsync(string speechString, CancellationToken cancellationToken = default(CancellationToken)) { speechString.AssertNotNull(nameof(speechString)); var utterance = new AVSpeechUtterance(speechString) { Voice = voice, Rate = 0.55f }; var synthesizer = new AVSpeechSynthesizer(); var finishedUtterance = Observable .FromEventPattern<AVSpeechSynthesizerUteranceEventArgs>(x => synthesizer.DidFinishSpeechUtterance += x, x => synthesizer.DidFinishSpeechUtterance -= x) .Select(_ => Unit.Default) .Publish(); finishedUtterance .Subscribe( _ => { utterance.Dispose(); synthesizer.Dispose(); }); if (cancellationToken.CanBeCanceled) { cancellationToken.Register(() => synthesizer.StopSpeaking(AVSpeechBoundary.Immediate)); Observable .FromEventPattern<AVSpeechSynthesizerUteranceEventArgs>(x => synthesizer.DidCancelSpeechUtterance += x, x => synthesizer.DidCancelSpeechUtterance -= x) .Select(_ => Unit.Default) .Subscribe( _ => { utterance.Dispose(); synthesizer.Dispose(); }); } synthesizer.SpeakUtterance(utterance); finishedUtterance.Connect(); return finishedUtterance .FirstAsync() .RunAsync(cancellationToken); }
public IObservable <Unit> SpeakAsync(string speechString, CancellationToken cancellationToken = default(CancellationToken)) { speechString.AssertNotNull(nameof(speechString)); var utterance = new AVSpeechUtterance(speechString) { Voice = voice, Rate = 0.55f }; var synthesizer = new AVSpeechSynthesizer(); var finishedUtterance = Observable .FromEventPattern <AVSpeechSynthesizerUteranceEventArgs>(x => synthesizer.DidFinishSpeechUtterance += x, x => synthesizer.DidFinishSpeechUtterance -= x) .Select(_ => Unit.Default) .Publish(); finishedUtterance .Subscribe( _ => { utterance.Dispose(); synthesizer.Dispose(); }); if (cancellationToken.CanBeCanceled) { cancellationToken.Register(() => synthesizer.StopSpeaking(AVSpeechBoundary.Immediate)); Observable .FromEventPattern <AVSpeechSynthesizerUteranceEventArgs>(x => synthesizer.DidCancelSpeechUtterance += x, x => synthesizer.DidCancelSpeechUtterance -= x) .Select(_ => Unit.Default) .Subscribe( _ => { utterance.Dispose(); synthesizer.Dispose(); }); } synthesizer.SpeakUtterance(utterance); finishedUtterance.Connect(); return(finishedUtterance .FirstAsync() .RunAsync(cancellationToken)); }
public override void Dispose() { if (synth != null) { synth.DidFinishSpeechUtterance -= Synth_DidFinishSpeechUtterance; } synth?.Dispose(); synth = null; utterances.ClearAsync(a => a.Value?.TrySetCanceled()); }
public IObservable <Unit> Speak(string speechString) { Ensure.ArgumentNotNull(speechString, nameof(speechString)); return(Observable .Create <Unit>( observer => { var disposables = new CompositeDisposable(); var utterance = new AVSpeechUtterance(speechString) { Voice = voice, Rate = 0.55f } .AddTo(disposables); var synthesizer = new AVSpeechSynthesizer() .AddTo(disposables); var finishedUtterance = Observable .FromEventPattern <AVSpeechSynthesizerUteranceEventArgs>(x => synthesizer.DidFinishSpeechUtterance += x, x => synthesizer.DidFinishSpeechUtterance -= x) .ToSignal() .Publish(); finishedUtterance .SubscribeSafe( _ => { utterance.Dispose(); synthesizer.Dispose(); }) .AddTo(disposables); finishedUtterance .FirstAsync() .Subscribe(observer) .AddTo(disposables); finishedUtterance .Connect() .AddTo(disposables); synthesizer.SpeakUtterance(utterance); return disposables; })); }
public IObservable <Unit> Speak(string speechString) { Ensure.ArgumentNotNull(speechString, nameof(speechString)); var utterance = new AVSpeechUtterance(speechString) { Voice = voice, Rate = 0.55f }; var synthesizer = new AVSpeechSynthesizer(); var finishedUtterance = Observable .FromEventPattern <AVSpeechSynthesizerUteranceEventArgs>(x => synthesizer.DidFinishSpeechUtterance += x, x => synthesizer.DidFinishSpeechUtterance -= x) .Select(_ => Unit.Default) .Publish(); finishedUtterance .Subscribe( _ => { utterance.Dispose(); synthesizer.Dispose(); }); //if (cancellationToken.CanBeCanceled) //{ // cancellationToken.Register(() => synthesizer.StopSpeaking(AVSpeechBoundary.Immediate)); // Observable // .FromEventPattern<AVSpeechSynthesizerUteranceEventArgs>(x => synthesizer.DidCancelSpeechUtterance += x, x => synthesizer.DidCancelSpeechUtterance -= x) // .Select(_ => Unit.Default) // .Subscribe( // _ => // { // utterance.Dispose(); // synthesizer.Dispose(); // }); //} synthesizer.SpeakUtterance(utterance); finishedUtterance.Connect(); return(finishedUtterance .FirstAsync()); }
public IObservable<Unit> Speak(string speechString) { Ensure.ArgumentNotNull(speechString, nameof(speechString)); var utterance = new AVSpeechUtterance(speechString) { Voice = voice, Rate = 0.55f }; var synthesizer = new AVSpeechSynthesizer(); var finishedUtterance = Observable .FromEventPattern<AVSpeechSynthesizerUteranceEventArgs>(x => synthesizer.DidFinishSpeechUtterance += x, x => synthesizer.DidFinishSpeechUtterance -= x) .Select(_ => Unit.Default) .Publish(); finishedUtterance .Subscribe( _ => { utterance.Dispose(); synthesizer.Dispose(); }); //if (cancellationToken.CanBeCanceled) //{ // cancellationToken.Register(() => synthesizer.StopSpeaking(AVSpeechBoundary.Immediate)); // Observable // .FromEventPattern<AVSpeechSynthesizerUteranceEventArgs>(x => synthesizer.DidCancelSpeechUtterance += x, x => synthesizer.DidCancelSpeechUtterance -= x) // .Select(_ => Unit.Default) // .Subscribe( // _ => // { // utterance.Dispose(); // synthesizer.Dispose(); // }); //} synthesizer.SpeakUtterance(utterance); finishedUtterance.Connect(); return finishedUtterance .FirstAsync(); }
public override void Speak(string text, bool gender, int pitch) { var speechSynthesizer = new AVSpeechSynthesizer(); var voci = AVSpeechSynthesisVoice.GetSpeechVoices(); var choices = voci.Where(x => x.Description.Contains(gender ? "female" : "male")); //prefer us AVSpeechSynthesisVoice voice; voice = choices.FirstOrDefault(x => x.Language.ToLowerInvariant().Contains("en-gb")); if (voice == null) { voice = choices.FirstOrDefault(x => x.Language.ToLowerInvariant().Contains("en-gb")); } if (voice == null && voci.Length > 0) { voice = voci[0]; } if (voice == null) { return; } var speechUtterance = new AVSpeechUtterance(text) { Rate = AVSpeechUtterance.DefaultSpeechRate, Voice = voice, Volume = 0.5f, PitchMultiplier = pitch / 100f }; speechSynthesizer.SpeakUtterance(speechUtterance); speechSynthesizer.DidFinishSpeechUtterance += (sender, e) => { speechSynthesizer.Dispose(); }; }
/// <summary> /// Dispose of TTS /// </summary> public void Dispose() => speechSynthesizer?.Dispose();