【问题标题】:How do I pause and resume AVSpeechSynthesizer / AVSpeechUtterance in swift?如何快速暂停和恢复 AVSpeechSynthesizer / AVSpeechUtterance?
【发布时间】:2014-11-20 05:40:28
【问题描述】:

我已经在我的应用程序中实现了文本转语音,它与我目前使用的代码配合得很好。基本上,算法会创建一个文本,然后如果用户单击 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)
}

【问题讨论】:

  • 你发现了吗?我也需要这方面的帮助。
  • 怕不是,如果你发现了,请告诉我????
  • @webmagnets 我目前专注于其他事情并且在截止日期之前。你有吗,你能测试一下下面建议的解决方案吗?

标签: swift avfoundation text-to-speech avspeechsynthesizer


【解决方案1】:

AVSpeechSynthesizer Class Reference

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

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

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

【讨论】:

  • 如果有人可以编辑答案,最好有一个完整的 Swift 示例,我会接受答案。
【解决方案2】:

Main.storyboard中,做两个UIElements

  • 一个UITextView,将从中读取文本。
  • UIButton 播放、暂停和继续阅读文本。

在下面的代码中创建一个从 UITextView@IBOutletoutlet 和一个从 UIButton@IBActionaction。以下代码是一个工作示例,应该是您的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()
    }
}

【讨论】:

    【解决方案3】:

    这里是如何实现的

    首先创建一个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)
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2015-06-07
      • 1970-01-01
      • 1970-01-01
      • 2019-06-28
      • 2020-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多