Hi I am making an iOS game in SpriteKit and decided to use an AvAudioPlayerNode and engine instead off the regular AvAudioPlayer so I could change the pitch and playbackspeed but I have come across a problem in which the AvAudioPlayerNode doesn't repeat after being played
I have already used many answers from stack overflow but they are very out dated if you do happen to solve this can u make sure it works with my code otherwise its useless
//
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()
}
'
//This simply gets run in the game view controller by doing this
let audioPath = Bundle.main.path(forResource: "loop", ofType: "wav") ?? ""
do
{
try? play(URL(fileURLWithPath: audioPath))
}
catch {
}
I actually have no idea where to start I am fairly new to Xcode so any help would be useful, please don't over complicate things I am 15
You want to read your file into a buffer, then schedule the player to loop
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()