swift 使用AVSpeechSynthesizer读取文本
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift 使用AVSpeechSynthesizer读取文本相关的知识,希望对你有一定的参考价值。
import UIKit
import AVFoundation
class ViewController: UIViewController {
private let textToRead = """
Bloodborne is an action role-playing game developed by FromSoftware and published by Sony Computer Entertainment for the PlayStation 4, which released worldwide in March 2015. Bloodborne follows the player character, the Hunter, through the decrepit Gothic, Victorian era-inspired city of Yharnam, whose inhabitants have been afflicted with an abnormal blood-borne disease, with the player character unraveling the city's intriguing mysteries while fighting beasts, ultimately attempting to find the source of the plague and stop it.
The game is played from a third-person perspective, players control a customizable protagonist, and gameplay is focused on weapons-based combat and exploration. Players battle varied enemies, including bosses, using items such as swords and firearms, and journey through the story, exploring the game's different locations, interacting with non-player characters, collecting key items involved in the story, and discovering and unraveling the world's many mysteries. Bloodborne began development in 2012 under the working title of Project Beast. Bearing many similarities to the Souls series of games by the same developer and director, Bloodborne was partially inspired by the literary works of authors H. P. Lovecraft and Bram Stoker, and the architectural design of certain real world locations in places such as Romania and the Czech Republic. The decision by game director Hidetaka Miyazaki to create a new intellectual property (IP) and not another Souls game was made because he wanted to create something "different"; at the same time, Sony wanted a new IP to be made exclusively for the PlayStation 4.
"""
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func readText(_ sender: Any) {
try? AVAudioSession.sharedInstance().setCategory(.playback, options: .duckOthers)
let utterance = AVSpeechUtterance(string: textToRead)
utterance.voice = AVSpeechSynthesisVoice(language: "en-US")
let synthesizer = AVSpeechSynthesizer()
synthesizer.delegate = self
setOutput(for: synthesizer, to: getAudioChannelNames())
synthesizer.speak(utterance)
}
private func getAudioChannelNames() -> [String] {
let route = AVAudioSession.sharedInstance().currentRoute
let outputPorts = route.outputs
var channels = [String]()
for port in outputPorts {
guard let portChannels = port.channels else {
fatalError()
}
for channel in portChannels {
channels.append(channel.channelName)
}
}
return channels
}
private func setOutput(for speechSynthesizer: AVSpeechSynthesizer, to channelNames: [String]) {
let route = AVAudioSession.sharedInstance().currentRoute
let outputPorts = route.outputs
var channels = [AVAudioSessionChannelDescription]()
for channelName in channelNames { //1
for outputPort in outputPorts { //2
guard let portChannels = outputPort.channels else { //3
fatalError()
}
for channel in portChannels {
if channel.channelName == channelName {
channels.append(channel)
}
}
}
}
if channels.count > 0 {
speechSynthesizer.outputChannels = channels
}
}
}
以上是关于swift 使用AVSpeechSynthesizer读取文本的主要内容,如果未能解决你的问题,请参考以下文章
Swift 类使用 Objective-C 类使用 Swift 类
我可以在 Swift 3 项目中使用 Swift 2.3 框架吗?
Swift : 使用多个 swift 文件或仅使用一个 viewController?保持清洁
如何使用 Swift Package Manager `swift build` 命令构建 Swift Package 的优化版本