3

I have created a Sound class and placed a method in my scene file to call the audio when the view begins.

I have also linked the AudioToolbox.framework in the Build Phase.

All runs fine on my simulator but the sound does not play on an actual device.

static let sharedInstance = Sound()

private let music = "filename.mp3"

private var player = AVAudioPlayer()
private var initialized = false

func playMusic() {
    let url = URL(fileURLWithPath: Bundle.main.path(forResource: "filename", ofType: nil)!)

    do {
        player = try AVAudioPlayer(contentsOf: url)
    } catch let error as NSError {
        NSLog("Error playing music: %@", error)
    }

    player.numberOfLoops = -1
    player.prepareToPlay()
    player.play()

    initialized = true

}

michiel
  • 31
  • 3

0 Answers0