[ Swift ] synthesizer.speak 怎么出声?

2022-09-29 18:29:54 +08:00
 garyox64
#!/usr/bin/env swift
import AVFoundation
// Create an utterance.
let utterance = AVSpeechUtterance(string: "The quick brown fox jumped over the lazy dog.")

// Configure the utterance.
utterance.rate = 0.57
utterance.pitchMultiplier = 0.8
utterance.postUtteranceDelay = 0.2
utterance.volume = 0.8

// Retrieve the British English voice.
let voice = AVSpeechSynthesisVoice(language: "en-GB")

// Assign the voice to the utterance.
utterance.voice = voice
// Create a speech synthesizer.
let synthesizer = AVSpeechSynthesizer()

// Tell the synthesizer to speak the utterance.
synthesizer.speak(utterance)
synthesizer.speak(AVSpeechUtterance(string: "Hello The quick brown fox jumped over the lazy dog."))

var myString = "Hello, World!"
 
print(myString)

为啥这个没有声音,synthesizer 不能用 swsh 脚本使用? 我是纯小白,也没学过 swift 今天发现 mac 的朗读功能挺有意思的就想调一下这个接口玩玩,但是发现没动静 感觉我的语法没问题啊,应该是 使用姿势不对吧,求指点要怎么用

目前就是想这个 出声音,求解

736 次点击
所在节点    Swift
1 条回复
butanediol2d
2022-10-06 13:43:49 +08:00
你在最后加一个 sleep 试一下,可能是还没等开口说话程序就退出了。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/883880

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX