11

我已经在我的应用程序中实现了文本转语音,并且它适用于我当前使用的代码。基本上,算法会创建一个文本,然后如果用户单击 UIButton,则会说出文本。

挑战:如果按钮已被点击(即当前正在朗读文本),我想启用相同的 UIButton 来暂停合成器,然后如果再次点击该按钮,则从停止的地方继续朗读。

我知道 AVFoundation Reference 中有一些功能,但我无法正确实现它们。

有谁知道如何在 Swift 中做到这一点?

import UIKit
import AVFoundation


    @IBOutlet var generatedText: UILabel!

@IBAction func buttonSpeakClicked(sender: UIButton){
    var mySpeechSynthesizer:AVSpeechSynthesizer = AVSpeechSynthesizer()
    var mySpeechUtterance:AVSpeechUtterance = AVSpeechUtterance(string:generatedText.text)
    mySpeechUtterance.rate = 0.075

mySpeechSynthesizer .speakUtterance(mySpeechUtterance)
}
4

3 回答 3

7

AVSpeechSynthesizer 类参考

你试过这些方法吗? - pauseSpeakingAtBoundary:- continueSpeaking

这些是一些属性, (pausedspeaking) 可以帮助您确定合成器的状态。

像这样的代码应该可以工作: mySpeechSynthesizer.pauseSpeakingAtBoundary(AVSpeechBoundary.Immediate)

于 2015-04-09T18:14:27.777 回答
3

Main.storyboard中,制作两个UIElements

  • aUITextView从中读取文本。
  • aUIButton播放、暂停和继续阅读文本。

在下面的代码中创建一个outletfrom the UITextViewto the@IBOutlet和一个actionfrom the UIButtonto the @IBAction。以下代码是一个工作示例,应该是您的ViewController.swift

import UIKit
import AVFoundation

class ViewController: UIViewController {

    // Synth object
    let synth = AVSpeechSynthesizer()
    // Utterance object
    var theUtterance = AVSpeechUtterance(string: "")

    // Text element that the synth will read from.
    @IBOutlet weak var textView: UITextView!

    // Function that starts reading, pauses reading, and resumes
    // reading when the UIButton is pressed.
    @IBAction func textToSpeech(_ sender: UIButton) {
        // The resume functionality
        if (synth.isPaused) {
            synth.continueSpeaking();
        }
        // The pause functionality
        else if (synth.isSpeaking) {
            synth.pauseSpeaking(at: AVSpeechBoundary.immediate)
        }
        // The start functionality
        else if (!synth.isSpeaking) {
            // Getting text to read from the UITextView (textView).
            theUtterance = AVSpeechUtterance(string: textView.text)
            theUtterance.voice = AVSpeechSynthesisVoice(language: "en-GB")
            theUtterance.rate = 0.5
            synth.speak(theUtterance)
        }
    }

    // Standard function
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    // Standard function
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}
于 2018-09-22T21:07:02.887 回答
2

在这里,您如何实现这一目标

首先创建一个AVSpeechSynthesizer的类变量并初始化

let synth = AVSpeechSynthesizer()

这是按钮单击方法(用于播放音频,或者如果它已经在播放则暂停它

@IBAction func onAVButtonClicked(_ sender: Any) {     
        if synth.isSpeaking {
            // when synth is already speaking or is in paused state

            if synth.isPaused {
                synth.continueSpeaking()
            }else {
            synth.pauseSpeaking(at: AVSpeechBoundary.immediate)
            }
        }else{
            // when synth is not started yet

            let string = attrStr.string
            let utterance = AVSpeechUtterance(string: string)
            utterance.voice = AVSpeechSynthesisVoice(language: "en-US")                    
            synth.speak(utterance)
        }
    }
于 2019-03-12T12:41:48.310 回答