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