Anyone get sound working in Swift for iphone or tvos?
my app preloads all the sounds,and puts them into an array pre-prepared
var soundEffect : [AVAudioPlayer?] = []```
public func initSOUNDS() {
#if !targetEnvironment(simulator)
var soundName : String = ""
for i in(0...10) {
switch soundID(rawValue: i) {
case .badMove : soundName = "BADMOVE.wav"
case .goodMove : soundName = "CARD.wav"
case .tink : soundName = "SELECT.wav"
// case .ping : soundName = "PING.aiff"
default : continue
}
let path = Bundle.main.path(forResource: "Sounds/sound_\(soundName)", ofType:nil)!
let url = URL(fileURLWithPath: path)
do {
let temp = try AVAudioPlayer(contentsOf: url)
temp.prepareToPlay()
soundEffect.append(temp)
} catch {
errorMessage("Could not load Sound \(path)")
}
}
#endif
}
then calls another function as required to play the sounds
public func playSound(index: Int,force : Bool = false) {
#if !targetEnvironment(simulator)
if index>=0 && index<soundEffect.count {
if option_SOUND || force {
if soundEffect[index] != nil {
print("C")
print(soundEffect[index]!.play())
}
}
}
#endif
}
everything gets called at the right times, no errors seem to occur, volume is set , yet silence