Search code examples
swiftcocoanssound

How to make a NSSound loop


I'm attempting to make a simple application on XCode (in Swift) that plays an audio file, then keeps looping infinitely. According to Apple's website, I need to add var loops: Bool { get set } to my code; but I'm not sure how to combine that with my current code in a way that doesn't give an error.

This is what I have so far:

let song = NSSound(named: "song.mp3")?.play()
var loops: Bool { get set }

Solution

  • Try in this way:

    // create an instance    
    let song = NSSound(named: "song.mp3")
    
    // set `loops` property to be true    
    song?.loops = true
    
    // start to play   
    song?.play()