I wrote a small class that uses AVFoundation to play audio using an array. Basically, a new element is added to the array each time "playAudio" is called. This allows you to play multiple sounds without reducing each other. In addition, so that the array does not increase the size indefinitely, I set it to return to index 0 after filling 5 slots in the array. Now everything works fine, but after the “audioPlayer” is called a bunch of times, the sound suddenly stops and I start getting an “Error” in the Catch section, but my application continues to function normally, as if it had just been disabled. Can someone tell me why this is happening?
var audioIndexA = 0
public class AudioPlayer: NSObject {
var playerA = [AVAudioPlayer]()
func playAudio(audioFile audioFile: String){
do{
let path = NSBundle.mainBundle().pathForResource(audioFile, ofType:"wav")
let fileURL = NSURL(fileURLWithPath: path!)
playerA.insert(try AVAudioPlayer(contentsOfURL: fileURL, fileTypeHint: nil), atIndex: audioIndexA)
playerA[audioIndexA].prepareToPlay()
if audioIndexA < 5{
playerA[audioIndexA].play()
audioIndexA++
} else{
playerA[audioIndexA].play()
audioIndexA = 0
}
} catch{
print("Error")
}
}
}
source
share