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
}
}
}