我正在寻找一种轻松播放远程.mp3文件的解决方案。我看过适用于本地文件的“ pyglet”模块,但似乎无法处理远程文件。我可以临时下载.mp3文件,但是由于.mp3文件看起来太大,因此不建议这样做。
我宁愿它用于跨平台而不是仅用于Windows等。
例如,播放来自以下位置的音频文件:
只需在下载文件时流式传输文件,我的想法是用Python播放MP3播放器,打开Soundcloud歌曲。
您可以将GStreamer与python绑定一起使用(需要PyGTK)。
然后,您可以使用以下代码:
import pygst
import gst
def on_tag(bus, msg):
taglist = msg.parse_tag()
print 'on_tag:'
for key in taglist.keys():
print '\t%s = %s' % (key, taglist[key])
#our stream to play
music_stream_uri = 'http://mp3channels.webradio.antenne.de/chillout'
#creates a playbin (plays media form an uri)
player = gst.element_factory_make("playbin", "player")
#set the uri
player.set_property('uri', music_stream_uri)
#start playing
player.set_state(gst.STATE_PLAYING)
#listen for tags on the message bus; tag event might be called more than once
bus = player.get_bus()
bus.enable_sync_message_emission()
bus.add_signal_watch()
bus.connect('message::tag', on_tag)
#wait and let the music play
raw_input('Press enter to stop playing...')
更新
控制播放器:
def play():
player.set_state(gst.STATE_PLAYING)
def pause():
player.set_state(gst.STATE_PAUSED)
def stop():
player.set_state(gst.STATE_NULL)
def play_new_uri( new_uri ):
player.set_state(gst.STATE_NULL)
player.set_property('uri', new_uri )
play()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句