音を鳴らす方法

これを見て作った。

from AppKit import NSSound
from time import sleep

s = NSSound.alloc()
filename = "/Users/nishio/tmp/mario-sound/jump.wav"
s.initWithContentsOfFile_byReference_(filename, True)
print s
s.play()
while s.isPlaying():
    sleep(0.1)

s.stop()

できたできた。でも一度再生した後のNSSoundオブジェクトのplayをもう一回呼んでも音が鳴らない。巻き戻し的なメソッドが見つからない。うむむ。

def play(filename):
    s = NSSound.alloc()
    s.initWithContentsOfFile_byReference_(filename, True)
    print s
    s.play()
    while s.isPlaying():
        sleep(0.1)
        
    s.stop()
    s.dealloc()

filename = "/Users/nishio/tmp/mario-sound/jump.wav"

for i in range(10):
    play(filename)

とりあえず毎回ファイル名を指定してinitすれば繰り返してならすこともできるけど、正しい方法ではないような気配がする。