问题描述
我正在尝试使用AVAu@R_502_6230@Engine播放按钮声音。但不幸的是,声音文件只能播放一次。
这个想法是,用户点击按钮,播放声音并开始录制。用户再次点击该按钮后,应该会播放第二声,表明录制会话已经结束。
到目前为止,出现第一个声音,并开始录音。 不幸的是,第二个声音(结束声音)不会播放。
我发现,当我使用与录音功能相同的Au@R_502_6230@Engine时,根本不会播放声音。
由于我是AVFoundation框架的新手,所以我不确定这里的问题是什么。
谢谢。
var StartSoundEnginescene1 = AVAu@R_502_6230@Engine()
var StartSoundNodeScene1 = AVAu@R_502_6230@PlayerNode()
func SetupAu@R_502_6230@(Au@R_502_6230@Engine: AVAu@R_502_6230@Engine,SoundNode: AVAu@R_502_6230@PlayerNode,FileURL: URL) {
guard let Au@R_502_6230@File = try? AVAu@R_502_6230@File(forReading: FileURL) else{ return }
let Au@R_502_6230@Session = AVAu@R_502[email protected]()
Au@R_502[email protected](SoundNode)
Au@R_502[email protected](SoundNode,to: Au@R_502[email protected]mixerNode,format: Au@R_502[email protected])
Au@R_502[email protected]()
}
override func viewDidLoad() {
super.viewDidLoad()
SetupAu@R_502_6230@(Au@R_502_6230@Engine: StartSoundEnginescene1,SoundNode: StartSoundNodeScene1,FileURL: StartRecsound)
}
func ButtonSound (Au@R_502_6230@Engine: AVAu@R_502_6230@Engine,FileURL: URL){
try? Au@R_502[email protected]()
guard let au@R_502_6230@File = try? AVAu@R_502_6230@File(forReading: FileURL) else{ return }
SoundNode.scheduleFile(au@R_502_6230@File,at: nil,completionHandler: nil)
SoundNode.volume = 0.16
SoundNode.play()
}
func StartRecording(){
ButtonSound(Au@R_502_6230@Engine: StartSoundEnginescene1,FileURL: StartRecsound)
Timer.scheduledTimer(withTimeInterval: 0.7,repeats: false) { timer in
if au@R_502[email protected] {
au@R_502[email protected]()
recognitionRequest?.endAu@R_502_6230@()
} else {
print("Rercording Started")
if let recognitionTask = self.recognitionTask {
recognitionTask.cancel()
self.recognitionTask = nil
}
self.recordedMessage = ""
let au@R_502_6230@Session = AVAu@R_502[email protected]()
do {
try au@R_502[email protected](AVAu@R_502[email protected])
try au@R_502[email protected](AVAu@R_502[email protected])
}catch {
print(error)
}
recognitionRequest = SFSpeechAu@R_502_6230@BufferRecognitionRequest()
guard let recognitionRequest = self.recognitionRequest else {
fatalError("Unable to create a speech au@R_502_6230@ buffer")
}
recognitionRequest.shouldReportPartialResults = true
recognitionRequest.requiresOnDeviceRecognition = true
recognitionTask = speechRecognizer?.recognitionTask(with: recognitionRequest,resultHandler: { (result,error) in
var isFinal = false
if let result = result {
let sentence = result.bestTranscription.formattedString
self.recordedMessage = sentence
print (self.recordedMessage)
isFinal = result.isFinal
}
if error != nil || isFinal {
self.au@R_502[email protected]()
self.au@R_502[email protected](onBus: 0)
self.recognitionRequest = nil
self.recognitionTask = nil
self.RecordBtn.isEnabled = true
}
})
let recordingFormat = au@R_502[email protected](forBus: 0)
au@R_502[email protected](onBus: 0,bufferSize: 1024,format: recordingFormat) { (buffer,when) in
self.recognitionRequest?.append(buffer)
}
au@R_502[email protected]()
do{
try au@R_502[email protected]()
}catch {
print(error)
}
}
}
}
func StopRecording(){
if au@R_502[email protected]{
au@R_502[email protected]()
ButtonSound(Au@R_502_6230@Engine: StartSoundEnginescene1,FileURL: StopRecsound)
recognitionRequest?.endAu@R_502_6230@()
au@R_502[email protected](onBus: 0)
}
}
解决方法
您将AVAudioSessionCategory设置为记录。
try audioSession.setCategory(AVAudioSession.Category.record)
如果要同时播放和录制,则应将此类别设置为playAndRecord
然后...如果在播放或录制过程中更改了AVAudioSession,则将更改AVAudioEngine的配置,然后将触发AVAudioEngineConfigurationChange通知。