嗨,我正在用SpriteKit做一场SpriteKit游戏,决定用AvAudioPlayerNode和引擎代替常规的AvAudioPlayer,这样我就可以改变球场和回放速度,但是我遇到了一个问题,在比赛结束后AvAudioPlayerNode不会重复。
我已经使用了来自堆栈溢出的许多答案,但是它们非常过时,如果您确实解决了这个问题,可以确保它与我的代码一起工作,否则它是无用的。
//
let engine = AVAudioEngine()
let speedControl = AVAudioUnitVarispeed()
let pitchControl = AVAudioUnitTimePitch()
let audioplayer = AVAudioPlayerNode()
let audioPath = Bundle.main.path(forResource: "loop", ofType: "wav")
func play(_ url: URL) throws {
let file = try AVAudioFile(forReading: url)
engine.attach(audioplayer)
engine.attach(pitchControl)
engine.attach(speedControl)
engine.connect(audioplayer, to: speedControl, format: nil)
engine.connect(speedControl, to: pitchControl, format: nil)
engine.connect(pitchControl, to: engine.mainMixerNode, format: nil)
audioplayer.scheduleFile(file, at: nil)
try engine.start()
audioplayer.play()
}‘
//此操作只需在游戏视图控制器中运行
let audioPath = Bundle.main.path(forResource: "loop", ofType: "wav") ?? ""
do
{
try? play(URL(fileURLWithPath: audioPath))
}
catch {
}实际上,我不知道从哪里开始,我对Xcode相当陌生,所以任何帮助都是有用的,请不要把我15岁的事情复杂化。
发布于 2019-09-05 19:57:41
您希望将文件读入缓冲区,然后将播放机安排为循环。
let file = try AVAudioFile(forReading: url)
let audioFileBuffer = AVAudioPCMBuffer(PCMFormat: file.fileFormat, frameCapacity: file.length)
try? read(into buffer: audioFileBuffer )
audioFilePlayer.scheduleBuffer(audioFileBuffer, atTime: nil, options:.Loops, completionHandler: nil)
audioFilePlayer.play()https://stackoverflow.com/questions/57811997
复制相似问题