Fre*_*ner 8 speech-recognition avaudioplayer ios swift3
我成功地能够使用语音(语音识别),我可以使用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
}
}
Run Code Online (Sandbox Code Playgroud)
我认为这是因为两者都使用音频.任何人都可以建议如何在同一个应用程序中使用它们?我还发现我不能在同一个应用程序中一起使用语音识别和文本到语音.
小智 6
您应该更改此行:
try audioSession.setCategory(AVAudioSessionCategoryPlayback)
Run Code Online (Sandbox Code Playgroud)
继续:
try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
Run Code Online (Sandbox Code Playgroud)
这应该工作;-)
小智 5
我碰到了同样的问题,这就是我的解决方法,
完成语音识别后,添加以下行。它所做的基本上是将音频会话设置回AVAudioSessionCategoryPlayback类别。
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryPlayback)
try audioSession.setActive(false, with: .notifyOthersOnDeactivation)
} catch {
// handle errors
}
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你。
| 归档时间: |
|
| 查看次数: |
2067 次 |
| 最近记录: |