swift 3一起使用语音识别和AVFoundation [英] swift 3 using speech recognition and AVFoundation together

查看:194
本文介绍了swift 3一起使用语音识别和AVFoundation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am successfully able to use Speech (speech recognition) and I can use AVFoundation to play wav files in Xcode 8/IOS 10. I just can't use them both together. I have working speech recognition code where I import Speech. When I import AVFoundation into the same app and use the following code, there is no sound and no errors are generated:

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
    }

}

我认为这是因为两者都使用音频。任何人都可以建议如何在同一个应用程序中使用它们?我还发现我不能在同一个应用程序中一起使用语音识别和文本到语音。

I assume it is because both use audio. Can anyone suggest how to use both in the same app? I also find that I cannot use speech recognition and text-to-speech together in the same app.

推荐答案

你应该改变这个line:

You should change this line:

try audioSession.setCategory(AVAudioSessionCategoryPlayback)

到:

try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord)

这应该有用; - )

This should work ;-)

这篇关于swift 3一起使用语音识别和AVFoundation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆