玩在Python远程音频文件? [英] Playing remote audio files in Python?

查看:307
本文介绍了玩在Python远程音频文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个解决方案,轻松播放远程的 .MP3 文件。我看过pyglet模块,该模块上的本地文件的工作原理,但似乎它不能处理远程文件。我可以临时下载在 .MP3 文件,但是,这不是reccomended由于多大的 .MP3 文件可能似乎是。

我宁愿希望它是跨平台的,而不是仅Windows等。

为例,从播放的音频文件:


  

http://example.com/sound.mp3


刚刚流的文件,因为它的下载,我的想法是Python中的MP3播放器,打开的SoundCloud歌曲。


解决方案

您可以使用的GStreamer用的的: //gstreamer.freedesktop.org/modules/gst-python.html\">python绑定(需要PyGTK的)。<​​/ p>

然后就可以使用这个code:

 进口pygst
进口消费税高清on_tag(公共汽车,MSG):
    标记列表= msg.parse_tag()
    打印on_tag:
    在taglist.keys()键:
        打印'\\ T%S =%s'的%(键,标记列表[关键])#our流播放
music_stream_uri ='http://mp3channels.webradio.antenne.de/chillout#creates一个playbin(播放媒体形成一个URI)
玩家= gst.element_factory_make(playbin,播放器)#设置的URI
player.set_property(URI,music_stream_uri)#START播放
player.set_state(gst.STATE_PLAYING)#listen为消息总线上的标签;标签事件可能会被调用一次以上
总线= player.get_bus()
bus.enable_sync_message_emission()
bus.add_signal_watch()
bus.connect(消息::标签',on_tag)#WAIT,让音乐播放
的raw_input('preSS进入停止播放...')

<一个href=\"http://gstreamer.freedesktop.org/data/doc/gstreamer/head/gst-plugins-base-plugins/html/gst-plugins-base-plugins-playbin.html\">GStreamer playbin文档

更新

控制播放器:

 高清播放():
    player.set_state(gst.STATE_PLAYING)DEF暂停():
    player.set_state(gst.STATE_PAUSED)高清停止():
    player.set_state(gst.STATE_NULL)高清play_new_uri(new_uri):
    player.set_state(gst.STATE_NULL)
    player.set_property(URI,new_uri)
    玩()

I'm looking for a solution to easily play remote .mp3 files. I have looked at "pyglet" module which works on local files, but it seems it can't handle remote files. I could temporary download the .mp3 file but that's not reccomended due to how large the .mp3 files could appear to be.

I rather want it to be for cross-platform instead of Windows-only etc.

Example, playing a audio file from:

http://example.com/sound.mp3

Just stream the file as it's downloads, my idea is a MP3 player in Python which opens Soundcloud songs.

解决方案

You can use GStreamer with python bindings (requires PyGTK).

Then you can use this code:

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...')

GStreamer playbin Docs

UPDATE

Controlling the player:

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()

这篇关于玩在Python远程音频文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆