2013-11-23 44 views
0

我試圖讓使用Python + PyObjC一個簡單的音樂流應用以下this blogPyObjC - 流媒體音樂與NSSound

import Foundation 
from AppKit import NSSound 
sound = NSSound.alloc() 
url = Foundation.NSURL.URLWithString_("http://206.217.213.235:8050/") 
sound.initWithContentsOfURL_byReference_(url, True) 

sound.play() 

,也未能

>>> sound.play() 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
AttributeError: cannot access attribute 'play' of NIL 'NSSound' object 

爲什麼?

回答

1

這是因爲在了AppKit的NSSound對象提供了一種發揮 AIFF及WAV聲音文件 Mac應用程序,而不是Shoutcast的。請參閱:http://nodebox.net/code/index.php/PyObjC

我很快就試圖找到一個現有的Python模塊來做你想做的事情,但它似乎沒有存在。然而,有很多應用程序,用Python來實現,你可以通過分析來弄清楚它們是如何做到的。例如:https://pypi.python.org/pypi/DeeFuzzer

我能找到最適合你的選擇是How do I capture an mp3 stream with python談到有關捕捉的本地播放的流。

1

添加到@William Denman的回答initWithContentsOfURL_byReference_返回零,這意味着創建聲音時發生錯誤。在這種情況下,我們知道它是因爲URL應該指向NSSound可以理解的有效文件(AIFF,WAVE,NeXT,SD2,AU和MP3)。

即使NSSound在這種情況下工作,它將不得不在播放之前下載整個文件;正如@William Denman指出的那樣,這不適用於流。