我能够成功地使用语音(语音识别),我可以使用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
}
}
我认为这是因为两者都使用音频。任何人都可以建议如何在同一个应用程序中使用它们?我还发现我不能在同一个应用程序中一起使用语音识别和文本到语音。
答案 0 :(得分:5)
您应该更改此行:
try audioSession.setCategory(AVAudioSessionCategoryPlayback)
开始:
try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
这应该有用; - )
答案 1 :(得分:3)
我刚遇到同样的问题,这就是我解决的问题,
语音识别完成后添加以下行。它的作用基本上是将音频会话设置回AVAudioSessionCategoryPlayback类别。
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryPlayback)
try audioSession.setActive(false, with: .notifyOthersOnDeactivation)
} catch {
// handle errors
}
希望它有所帮助。
答案 2 :(得分:1)
如果你像在Apple的语音识别示例中那样使用AVAudioPlayer
录制麦克风,似乎AVAudioSession
停止播放示例。
但是,我已设法通过使用AVCaptureSession
来捕获音频,如this answer中所述。