用Python播放远程音频文件?

Jac*_*ack 7 python audio python-module audio-streaming python-2.7

我正在寻找一种轻松播放远程.mp3文件的解决方案.我看过"pyglet"模块,该模块适用于本地文件,但它似乎无法处理远程文件.我可以临时下载.mp3文件,但由于.mp3文件看起来有多大,所以不推荐使用.mp3文件.

我更希望它是跨平台而不是仅限Windows等.

例如,播放音频文件:

http://example.com/sound.mp3

只要在下载时传输文件,我的想法就是用Python打开Soundcloud歌曲的MP3播放器.

kel*_*nss 11

你可以使用GStreamerpython绑定(需要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...')
Run Code Online (Sandbox Code Playgroud)

GStreamer playbin文档

UPDATE

控制播放器:

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()
Run Code Online (Sandbox Code Playgroud)