/* Cordova Text-to-Speech Plugin https://github.com/vilic/cordova-plugin-tts by VILIC VANE https://github.com/vilic MIT License */ using System; using System.Diagnostics; using System.Runtime.Serialization; using System.Threading.Tasks; using Windows.Phone.Speech.Synthesis; using WPCordovaClassLib.Cordova; using WPCordovaClassLib.Cordova.Commands; using WPCordovaClassLib.Cordova.JSON; namespace Cordova.Extension.Commands { [DataContract] class Options { [DataMember] public string text; [DataMember] public string locale; [DataMember] public double? rate; } class TTS : BaseCommand { SpeechSynthesizer synth = new SpeechSynthesizer(); string lastCallbackId; public async void speak(string argsJSON) { if (lastCallbackId != null) { DispatchCommandResult(new PluginResult(PluginResult.Status.OK), lastCallbackId); lastCallbackId = null; synth.CancelAll(); } var args = JsonHelper.Deserialize(argsJSON); var options = JsonHelper.Deserialize(args[0]); lastCallbackId = args[1]; var locale = options.locale != null ? options.locale : "en-US"; var rate = options.rate != null ? options.rate : 1.0; var ssml = @" " + xmlEncode(options.text) + @" "; try { await synth.SpeakSsmlAsync(ssml); lastCallbackId = null; DispatchCommandResult(new PluginResult(PluginResult.Status.OK)); } catch (OperationCanceledException) { // do nothing } catch (Exception e) { Debug.WriteLine(e.Message); lastCallbackId = null; DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message)); } } string xmlEncode(string text) { return text .Replace("&", "&") .Replace("<", "<") .Replace(">", ">"); } } }