gpt4 book ai didi

swift - 如何突出显示每个段落并使用 speechSynthesizer 滚动 textView

转载 作者:行者123 更新时间:2023-11-28 09:23:36 25 4
gpt4 key购买 nike

我想做的是按下朗读按钮,这将突出显示第一段,然后朗读该段落,然后将下一段滚动到 textView 的中心并突出显示该段落并朗读它等等。我必须添加什么代码才能做到这一点?

import UIKit
import AVFoundation


class TestViewController: UIViewController, AVSpeechSynthesizerDelegate {

let synthesizer = AVSpeechSynthesizer()


@IBOutlet weak var textViewOutlet: UITextView!


@IBAction func pauseSpeech(_ sender: Any) {
synthesizer.pauseSpeaking(at: AVSpeechBoundary.word)
}

@IBAction func stopSpeech(_ sender: Any) {
synthesizer.stopSpeaking(at: .word)
}

@IBAction func cancelButton(_ sender: Any) {
synthesizer.continueSpeaking()
}

@IBAction func speak(_ sender: AnyObject) {
let string = textViewOutlet.text!
let utterance = AVSpeechUtterance(string: string)
utterance.voice = AVSpeechSynthesisVoice(language: "en-GB")
synthesizer.speak(utterance)
}


override func viewDidLoad() {
super.viewDidLoad()
synthesizer.delegate = self
textViewOutlet.font = .systemFont(ofSize: 20)
}

func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, willSpeakRangeOfSpeechString characterRange: NSRange, utterance: AVSpeechUtterance) {
let mutableAttributedString = NSMutableAttributedString(string: utterance.speechString)
mutableAttributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.blue, range: NSMakeRange(0, (utterance.speechString as NSString).length))
mutableAttributedString.addAttribute(.foregroundColor, value: UIColor.red, range: characterRange)
textViewOutlet.attributedText = mutableAttributedString
textViewOutlet.font = .systemFont(ofSize: 20)
}

func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) {
textViewOutlet.attributedText = NSAttributedString(string: utterance.speechString)
textViewOutlet.font = .systemFont(ofSize: 20)
}

}

最佳答案

像这样:

定义属性

   let currentParagraphIndex = 0
let paragraphs = [Range<String.Index>]()

开始时

   let text = textViewOutlet.text!
paragraphs = splitOnParagraphs(text)
currentParagraphIndex = 0
highlightParagraph(paragraphs[currentParagraphIndex])
let utterance = AVSpeechUtterance(text.substringWithRange(paragraphs[currentParagraphIndex]))
utterance.voice = AVSpeechSynthesisVoice(language: "en-GB")
synthesizer.speak(utterance)

另见 Split paragraphs into Sentences

所以你开始说第一段,而不仅仅是文字。

然后,当段落结束时,在委托(delegate)中你开始说下一段:

func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) {
currentParagraphIndex = currentParagraphIndex + 1
if (currentParagraphIndex < paragraphs.count) {
let utterance = AVSpeechUtterance(text.substringWithRange(paragraphs[currentParagraphIndex]))
highlightParagraph(currentParagraphIndex)
utterance.voice = AVSpeechSynthesisVoice(language: "en-GB")
synthesizer.speak(utterance)
}
}

关于swift - 如何突出显示每个段落并使用 speechSynthesizer 滚动 textView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49537860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com