首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在AVSpeechSynthesizer / AVSpeechUtterance中暂停并恢复?

如何在AVSpeechSynthesizer / AVSpeechUtterance中暂停并恢复?
EN

Stack Overflow用户
提问于 2014-09-25 12:31:47
回答 3查看 5.7K关注 0票数 12

我已经在我的应用程序中实现了文本到语音,它与我目前使用的代码工作得很好。基本上,algo会创建一个文本,然后如果用户单击UIButton,文本就会被说出来。

挑战性:如果按钮已经被点击(即当前正在发言的文本),我希望相同的UIButton能够暂停合成器,如果再次点击按钮,则恢复发言。

我知道AVFoundation引用中有一些函数,但我无法正确地实现它们。

有人知道怎么用Swift吗?

代码语言:javascript
复制
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)
}
EN

回答 3

Stack Overflow用户

发布于 2015-04-09 18:14:27

AVSpeechSynthesizer类引用

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

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

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

票数 8
EN

Stack Overflow用户

发布于 2018-09-22 21:07:02

Main.storyboard中,使两个UIElements

  • 将从其中读取文本的UITextView
  • 播放、暂停和继续阅读文本的UIButton

在下面的代码中,从UITextView创建一个UITextView@IBOutlet,从UIButton创建一个action@IBAction。下面的代码是一个有用的示例,应该是您的ViewController.swift

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2019-03-12 12:41:48

在这里,你如何做到这一点?

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

代码语言:javascript
复制
let synth = AVSpeechSynthesizer()

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

代码语言:javascript
复制
@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)
        }
    }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26038749

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档