Reputation: 2722
As the title suggests, I'm trying to figure out how to count the number of times an NSSound
loops (or, more accurately, have a function run at the beginning and/or end of each loop). I've looked at all the documentation, but I can't find anything. Any help would be greatly appreciated.
UPDATE: I've tried using func sound(sound: NSSound, didFinishPlaying aBool: Bool)
, but it doesn't seem to trigger after a loop completes. The code:
func sound(sound: NSSound, didFinishPlaying aBool: Bool) {
pbNowPlaying.doubleValue = sound.currentTime
if aBool == true {
self.btnPlay.title = NSLocalizedString("titlePlay", comment: "Play")
self.btnPlay.state = NSOffState
self.pbNowPlaying.doubleValue = 0
self.btnPlay.needsDisplay = true
}
else {
Swift.print("Sound Looping")
}
self.needsDisplay = true
}
Upvotes: 1
Views: 379
Reputation: 162
Have you tried the didFinishPlaying function? It is called every time the sound file finishes its clip.
optional func sound(_ sound: NSSound, didFinishPlaying aBool: Bool)
What about creating a variable that will increase each time the function is called forth? Something easy that will keep track of a count? The count is what your looking for correct?
var count = 0
func sound(sound: NSSound, didFinishPlaying aBool: Bool) {
pbNowPlaying.doubleValue = sound.currentTime
if aBool == true {
self.btnPlay.title = NSLocalizedString("titlePlay", comment: "Play")
self.btnPlay.state = NSOffState
self.pbNowPlaying.doubleValue = 0
self.btnPlay.needsDisplay = true
//Start playing sound again after it has ended.
count + 1
}
else {
Swift.print("Sound Looping")
}
self.needsDisplay = true
}
Upvotes: 1
Reputation: 17544
loops Property
A Boolean that indicates whether the sound restarts playback when it reaches the end of its content.
Declaration
SWIFT var loops: Bool
OBJECTIVE-C @property BOOL loops
Discussion
When the value of this property is YES, the sounds restarts playback when it finishes and does not send sound:didFinishPlaying: to its delegate when it reaches the end of its content and restarts playback. The default value of this property is NO.
i recommend you set it to false and restart play 'manualy' when you receive didFinishPlaying
another option is using duration and currentTime and calculate number of loops
Upvotes: 0