如何在不使用 pyglet 将其保存到文件中的情况下播放音频流? [英] How can I play audio stream without saving it into the file with pyglet?

查看:25
本文介绍了如何在不使用 pyglet 将其保存到文件中的情况下播放音频流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我有这些库:requestspygletpyaudio

如何使用来自这个站点的音频流播放音频流,而没有将其保存到文件中(使用缓冲)?

How can I play an audio stream using ones, for example, from this site without saving it into the file(using buffering)?

这个库的文档中有关于 StreamingSource 类的令人困惑的信息

There is a confusing information in documentation of this library about a StreamingSource class

当我在 StreamingSource 对象 (source.get_audio_data(DATA)) 中以字节为单位推送信息,然后我将这个信息推送到 Player(pyglet.media.Player()) 时,它抛出一个异常,表示 StreamingSource没有属性持续时间

When I push the information in bytes in StreamingSource object(source.get_audio_data(DATA)) and after that I push this one into a Player(pyglet.media.Player()) it throws an exception, that says that the StreamingSource hasn't attribute duration

代码:

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'

推荐答案

如果您只想从 url 播放文件(音频/视频)而不保存,您可以使用 vlc 如下.
vlc 的详细信息在这里

If you just want to play a file (audio/video) from a url without saving, you can use vlc as below.
Details on vlc are here

您可以将 vlc(在 Windows 上)安装为

You can install vlc (on windows) as

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

这篇关于如何在不使用 pyglet 将其保存到文件中的情况下播放音频流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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