gpt4 book ai didi

ios - swift 。声音不再播放第二次

转载 作者:行者123 更新时间:2023-11-30 14:02:36 25 4
gpt4 key购买 nike

我从 Udacity 类(class)中完成了一个简单的任务 ( Pitch Perfect )

我有一个用于录制声音的屏幕。录制后,我保存文件并将其传递到第二个屏幕。

在第二个屏幕上,我有几个按钮可以以不同的方式播放声音。我有一个播放音调声音的函数。

问题是,当我第一次点击这个功能时,声音会播放,但第二次就没有声音了。

我被困住了。

func playSoundWithPitchRate(pitch: Float) {

self.audioPlayer.stop()
self.audioEngine.stop()
self.audioEngine.reset()

let audioPlayerNode = AVAudioPlayerNode()
self.audioEngine.attachNode(audioPlayerNode)

let changePitchEffect = AVAudioUnitTimePitch()
changePitchEffect.pitch = pitch
self.audioEngine.attachNode(changePitchEffect)

self.audioEngine.connect(audioPlayerNode, to: changePitchEffect, format: nil)
self.audioEngine.connect(changePitchEffect, to: self.audioEngine.outputNode, format: nil)

let audioFileBuffer = AVAudioPCMBuffer(PCMFormat: self.audioFile.processingFormat, frameCapacity: AVAudioFrameCount(self.audioFile.length) )

do{
try self.audioFile.readIntoBuffer(audioFileBuffer)
} catch let err as NSError {
print("self.audioFile.readIntoBuffer(audioFileBuffer)")
print(err.localizedDescription)
}

audioPlayerNode.scheduleBuffer(audioFileBuffer, atTime: nil, options: .Interrupts) { () -> Void in

// reminder: we're not on the main thread in here
dispatch_async(dispatch_get_main_queue()) {
self.stopPlayButton.enabled = false
self.makeSoundEffectButtonsEnabled(true)
}

}

do {
try audioEngine.start()
} catch let err as NSError {
print("audioEngine.start()")
print(err.localizedDescription)
}

audioPlayerNode.play()

}

最佳答案

已解决。我已阅读文档并弄清楚您将文件读取到缓冲区的位置

self.audioFile.readIntoBuffer(audioFileBuffer)

文件帧位置按读取的帧数提前。

所以下次就没有框架可供读取了。所以我像这样重置框架位置

self.audioFile.framePosition = 0

希望这会对某人有所帮助。

关于ios - swift 。声音不再播放第二次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32806936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com