【问题标题】:swift 3 using speech recognition and AVFoundation togetherswift 3 一起使用语音识别和 AVFoundation
【发布时间】:2016-11-16 18:31:23
【问题描述】:

我可以成功使用 Speech(语音识别),并且可以使用 AVFoundation 在 Xcode 8/IOS 10 中播放 wav 文件。我只是不能同时使用它们。我在导入语音的地方有工作语音识别代码。当我将 AVFoundation 导入同一个应用程序并使用以下代码时,没有声音,也没有产生错误:

var audioPlayer: AVAudioPlayer!
func playAudio()  {
        let path = Bundle.main.path(forResource: "file.wav", ofType: nil)!
    let url = URL(fileURLWithPath: path)

    do {
        let sound = try AVAudioPlayer(contentsOf: url)
        audioPlayer = sound
        sound.play()
    } catch {
        //handle error
    }

}

我认为这是因为两者都使用音频。谁能建议如何在同一个应用程序中使用两者?我还发现我不能在同一个应用中同时使用语音识别和文本转语音。

【问题讨论】:

  • 你找到解决办法了吗?
  • 问题解决了吗?

标签: ios swift3 speech-recognition avaudioplayer


【解决方案1】:

我遇到了同样的问题,我是这样解决的,

语音识别完成后添加以下行。它所做的基本上是将音频会话设置回 AVAudioSessionCategoryPlayback 类别。

   let audioSession = AVAudioSession.sharedInstance()
    do {
        try audioSession.setCategory(AVAudioSessionCategoryPlayback)
        try audioSession.setActive(false, with: .notifyOthersOnDeactivation)
    } catch {
        // handle errors
    }

希望对你有帮助。

【讨论】:

    【解决方案2】:

    你应该改变这一行:

    try audioSession.setCategory(AVAudioSessionCategoryPlayback)
    

    到:

    try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
    

    这应该工作;-)

    【讨论】:

    • 我更喜欢你的修复,因为它简单,但是当我实现它时,在第一次语音识别后播放音量会变得非常低。
    【解决方案3】:

    如果您使用AVAudioSession 来录制麦克风,就像在 Apple 的语音识别示例中一样,AVAudioPlayer 似乎会停止播放示例。

    但是,我已经设法通过使用AVCaptureSession 来捕获音频,如this answer 中所述。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-08
      • 1970-01-01
      • 2018-02-23
      • 2019-05-19
      • 1970-01-01
      • 2012-07-31
      • 1970-01-01
      • 2015-10-01
      相关资源
      最近更新 更多