在 iOS 7+ 的 UIWebView 中使用语音合成 API 设置速率、音高、音量

Setting rate, pitch, volume with speechSynthesis API in UIWebView for iOS 7+

本文关键字:设置 API 速率 语音合成 音高 音量 iOS UIWebView      更新时间:2023-09-26

我正在尝试使用UIWebView在我的iOS应用程序中执行一些简单的TTS(文本到语音转换)。据我了解,iOS 7 WebKit现在支持它,因此以下工作如下:

   - (void) speakThis: (NSString*) text {
        [webview stringByEvaluatingJavaScriptFromString:
        [NSString stringWithFormat:
           @"speechSynthesis.speak(new SpeechSynthesisUtterance('"%@'"));",
              text]];
    }

但是,我还想在javascript中设置语速,音高和音量。我将如何在一条简单的行中做到这一点。

我知道我可以设置如下属性:

var speech = new SpeechSynthesisUtterance();
speech.text = "Hello";
speech.volume = 1; // 0 to 1
speech.rate = 1; // 0.1 to 9
speech.pitch = 1; // 0 to 2, 1=normal
speech.lang = "en-US";
speechSynthesis.speak(speech);

但是我想在我打电话时在一个初始化中传递音高、音量和速率

new SpeechSynthesisUtterance(")

有人,请协助吗?

你不需要使用 UIWebView。我只是在调查这个,并找到了 API 文档:https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVSpeechSynthesizer_Ref/Reference/Reference.html#//apple_ref/occ/cl/AVSpeechSynthesizer