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

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

问题描述

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

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.

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

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

只需在下载文件时流式传输文件,我的想法是 Python 中的 MP3 播放器,它可以打开 Soundcloud 歌曲.

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

推荐答案

您可以将 GStreamerpython 绑定(需要 PyGTK).

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

GStreamerplaybin 文档

更新

控制播放器:

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天全站免登陆