How Do I check if NSSound is paused in Swift

197 views Asked by At

I am able to play a sound in swift with NSSound:

var path ="/path/to/file.mp3"
mysound = NSSound(contentsOfFile: path, byReference: false)

mysound!.play() // works!

if (mysound!playing)
{
    mysound!.pause() //works, sort of
}

mysound!playing // is still true.

How do I check to see if it's paused?

The docs show that pause() is supposed to return false if playback already paused, but this doesn't seem to be the case, or I am doing something wrong

I'd like to be able to do this:

var path ="/path/to/file.mp3"
mysound = NSSound(contentsOfFile: path, byReference: false)

if (mysound!playing)
{
    var state = mysound!.pause() 
    if(state == false) 
    {
         mysound!.resume() //never gets executed - why?
    }

} else {
   mysound!.play() 
}

I could use some help figuring this out! thanks!

1

There are 1 answers

0
Michael On

The sound will only be paused if you called pause() and it returns true. The documentation doesn't qualify, but a paused NSSound might technically still return true for playing. This would make sense if playing is considered to be "not stopped".

Since you will know if you've called pause(), you can just store another Bool isPaused, and use that.

In your second example, you're not calling play(), so playing will always return false.