Sound - Swift

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

Maybe this works?

import AVFoundation

var soundEffect: [AVAudioPlayer?] =

public func initSOUNDS() {
#if !targetEnvironment(simulator)

// Configure audio session
do {
    try AVAudioSession.sharedInstance().setCategory(.ambient, mode: .default, options: [.mixWithOthers])
    try AVAudioSession.sharedInstance().setActive(true)
} catch {
    print("Audio session error: \(error)")
}

// Pre-size array to correct size
soundEffect = Array(repeating: nil, count: 11)

var soundName: String = ""
for i in 0...10 {
    switch soundID(rawValue: i) {
    case .badMove:
        soundName = "BADMOVE"
    case .goodMove:
        soundName = "CARD"
    case .tink:
        soundName = "SELECT"
    default:
        continue
    }
    
    // Fixed: removed "sound_" prefix from path
    guard let path = Bundle.main.path(forResource: "Sounds/\(soundName)", ofType: "wav") else {
        errorMessage("Could not find sound file: \(soundName).wav")
        continue
    }
    
    let url = URL(fileURLWithPath: path)
    
    do {
        let temp = try AVAudioPlayer(contentsOf: url)
        temp.prepareToPlay()
        soundEffect[i] = temp  // Store at correct index
    } catch {
        errorMessage("Could not load Sound \(path): \(error)")
    }
}
#endif

}

public func playSound(index: Int, force: Bool = false) {
#if !targetEnvironment(simulator)

if index >= 0 && index < soundEffect.count {
    if option_SOUND || force {
        if let player = soundEffect[index] {
            player.play()
        }
    }
}
#endif

}

the “sound_” prefix is in fact required as that is the actual file name “sound_BADMOVE.wav”

but I’ll check out the rest and let you know, thanks