如何在不使用pyglet将文件保存到文件中的情况下播放音频流?

时间:2017-08-23 14:21:16

标签: python-3.x audio-streaming pyglet

现在我有这些库:请求 pyglet pyaudio

如何播放音频流,例如,从this网站播放音频流而不将其保存到文件中(使用缓冲)?

关于StreamingSource类

,此库的文档中存在令人困惑的信息

当我在StreamingSource对象(source.get_audio_data(DATA))中按字节推送信息时,我将其推入播放器(pyglet.media.Player())后会抛出一个异常,表示StreamingSource没有属性持续时间

代码:

import pyglet, requests

req = requests.get('http://ic7.101.ru:8000/c15_3', stream=True)

player = pyglet.media.Player()

source = pyglet.media.StreamingSource()

CHUNK = 1024

for num, chunk in enumerate(req.iter_content(CHUNK)):
    if num == 1000:
        break
    source.get_audio_data(chunk)
    if num == 100:
        player.queue(source)
        player.play()
        pyglet.app.run()
pyglet.clock.schedule_once(lambda dt: pyglet.app.exit(), source.duration)

回溯:

Traceback (most recent call last):
  File "/home/user/.PyCharmCE2017.1/config/scratches/scratch.py", line 16, in <module>
    player.queue(source)
  File "/usr/local/lib/python3.5/dist-packages/pyglet/media/__init__.py", line 978, in queue
    group.queue(source)
  File "/usr/local/lib/python3.5/dist-packages/pyglet/media/__init__.py", line 698, in queue
    self.duration += source.duration
TypeError: unsupported operand type(s) for +=: 'float' and 'NoneType'

1 个答案:

答案 0 :(得分:1)

如果您只想从网址播放文件(音频/视频)而不保存,可以使用vlc,如下所示。
vlc的详细信息为here

您可以安装vlc(在Windows上)

pip install python-vlc

源代码

import vlc

url = 'http://ic7.101.ru:8000/c15_3'
#define VLC instance
instance = vlc.Instance('--input-repeat=-1', '--fullscreen')

#Define VLC player
player=instance.media_player_new()

#Define VLC media
media=instance.media_new(url)

#Set player media
player.set_media(media)

#Play the media
player.play()