gpt4 book ai didi

ios - swift:如何在单击按钮上对所有单元格进行 TTS

转载 作者:行者123 更新时间:2023-11-28 15:04:49 27 4
gpt4 key购买 nike

我在多个单元格的 UITableViewCell 中有一个 UITextView:单击 UIButton 时,需要所有单元格 textView 的文本才能将表单文本转换为语音 (TTS)。UIButton 在 UITableViewCell 中。现在所有单元格的 TextView 的文本都不会在单击按钮时从文本转换为语音。只有一个 textView 的文本在单击按钮时被转换

UITableViewCell

class HomeTableViewCell: UITableViewCell {

var homeViewCon: HomeTableViewController?
let synth = AVSpeechSynthesizer()

@IBAction func ttsBtnClick(_ sender: Any) {

guard let ttsBtnCheck = sender as? UIButton else {
return
}

if ttsBtnCheck.isSelected {
ttsBtn.setImage(UIImage(named: "tts"), for: .normal)
ttsBtnCheck.isSelected = false
synth.stopSpeaking(at: AVSpeechBoundary.immediate)
} else{
print("ttsBtnClick false ")
ttsBtn.setImage(UIImage(named: "ttsBlue"), for: .selected)
ttsBtnCheck.isSelected = true

textToSpeech()

let point = (sender as AnyObject).convert(CGPoint.zero, to: self.homeViewCon?.homeTableView)
let indexPath = self.homeViewCon?.homeTableView.indexPathForRow(at: point)!

let indexPathRow = indexPath?.row ?? 0
let indexPathSection = self.homeViewCon?.homeTableView.numberOfRows(inSection: (indexPath?.section)!)

if indexPathRow < indexPathSection! {
let newIndexPath = NSIndexPath(row:(indexPath?.row)! + 1, section:(indexPath?.section)!)
self.homeViewCon?.homeTableView.scrollToRow(
at: newIndexPath as IndexPath, at:.bottom, animated: true)
}
}

}


func textToSpeech(){


let name: String = newsTextView.text
let endIndex = name.index(name.endIndex, offsetBy: -3)
let truncated = name.substring(to: endIndex)



let index2 = truncated.range(of: ".", options: .backwards)?.lowerBound

var substring3 = index2.map(truncated.substring(to:))


let utterance = AVSpeechUtterance(string: titleLabel.text!+". "+substring3!)

utterance.voice = AVSpeechSynthesisVoice(language: "en-US")


synth.speak(utterance)

}
}

最佳答案

请找到工作解决方案以及 Storyboard UI。

swift 4

    //
// TableViewVC.swift
//
// Created by Test User on 06/02/18.
// Copyright © 2018. All rights reserved.
//

import UIKit
import AVFoundation

class customCell: UITableViewCell {

@IBOutlet weak var lblTTS: UILabel!
@IBOutlet weak var btnTTS: UIButton!

override func awakeFromNib() {
super.awakeFromNib()
}

override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}

class TableViewVC: UIViewController {

@IBOutlet weak var tblTTS: UITableView!


var arrTTS = [
"This is first text",
"This is second text",
"This is third text",
"This is fourth text",
"This is fifth text",
"This is sixth text",
"This is seventh text",
"This is eighth text",
"This is ninth text",
"This is tenth text",
"This is eleventh text",
"This is twelfth text",
]

let speechSynthesizer = AVSpeechSynthesizer()
var previousSelectedIndexPath : IndexPath?


override func viewDidLoad() {
super.viewDidLoad()
speechSynthesizer.delegate = self
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

@IBAction func btnTTSClicked(sender: UIButton) {

let point = sender.convert(CGPoint.zero, to: self.tblTTS)
let indexPath = self.tblTTS.indexPathForRow(at: point)

if previousSelectedIndexPath == indexPath {

let cell = self.tblTTS.cellForRow(at: indexPath!) as! customCell
speechSynthesizer.stopSpeaking(at: .immediate)
cell.btnTTS.setTitle("TTS", for: .normal)
previousSelectedIndexPath = nil

} else {

if previousSelectedIndexPath == nil {

previousSelectedIndexPath = indexPath

let cell = self.tblTTS.cellForRow(at: indexPath!) as! customCell
cell.btnTTS.setTitle("Speaking", for: .normal)

if speechSynthesizer.isSpeaking {

speechSynthesizer.stopSpeaking(at: .immediate)

} else {

let speechUtterance = AVSpeechUtterance(string: self.arrTTS[indexPath!.row])

DispatchQueue.main.async {
self.speechSynthesizer.speak(speechUtterance)
}
}

} else {

let oldCell = self.tblTTS.cellForRow(at: previousSelectedIndexPath!) as! customCell
oldCell.btnTTS.setTitle("TTS", for: .normal)

let cell = self.tblTTS.cellForRow(at: indexPath!) as! customCell
cell.btnTTS.setTitle("Speaking", for: .normal)

previousSelectedIndexPath = indexPath

if speechSynthesizer.isSpeaking {

speechSynthesizer.stopSpeaking(at: .immediate)

} else {

let speechUtterance = AVSpeechUtterance(string: self.arrTTS[indexPath!.row])

DispatchQueue.main.async {
self.speechSynthesizer.speak(speechUtterance)
}
}

}
}
}
}

extension TableViewVC: UITableViewDataSource, UITableViewDelegate {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrTTS.count
}

//----------------------------------------------------------------

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "customCell") as! customCell

cell.lblTTS.text = self.arrTTS[indexPath.row]
cell.btnTTS.addTarget(self, action: #selector(btnTTSClicked(sender:)), for: .touchUpInside)

return cell
}

//----------------------------------------------------------------

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50
}
}

extension TableViewVC: AVSpeechSynthesizerDelegate {

func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) {

speechSynthesizer.stopSpeaking(at: .word)

if previousSelectedIndexPath != nil {
let speechUtterance = AVSpeechUtterance(string: self.arrTTS[previousSelectedIndexPath!.row])
DispatchQueue.main.async {
self.speechSynthesizer.speak(speechUtterance)
}
}
}
}

enter image description here

关于ios - swift:如何在单击按钮上对所有单元格进行 TTS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48640287/

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