How to play streaming audio from internet radio on Python 3.5.3(如何在 Python 3.5.3 上播放来自网络电台的流音频)
问题描述
我在 Windows 8.1 x64 上使用 Python 3.5.3,我需要播放来自这里的音频一个>
我尝试过 pyaudio,但它只给我白噪声,并且在运行几次 pyaudio 后出现错误(pyaudio 模块pyaudio"没有属性PyAudio").
请告诉我如何更好地使用 Python 从 url 播放流音频...
附言我已经使用此代码获得了歌曲名称和艺术家姓名:
导入请求导入时间导入日期时间打印(日期时间.日期时间.现在())进口重新url = 'http://prem1.rockradio.com:80/bluesrock?9555ae7caa92404c73cade1d'编码 = 'latin1'信息 = ''radio_session = requests.Session()而真:radio = radio_session.get(url, headers={'Icy-MetaData': '1'}, stream=True)metaint = int(radio.headers['icy-metaint'])流 = radio.rawaudio_data = stream.read(metaint)meta_byte = stream.read(1)如果(元字节):meta_length = ord(meta_byte) * 16meta_data = stream.read(meta_length).rstrip(b' ')stream_title = re.search(br"StreamTitle="KFte"]*)';", meta_data)如果流标题:stream_title = stream_title.group(1).decode(encoding, errors='replace')如果信息 != stream_title:打印('正在播放:',stream_title)信息 = 流标题别的:经过别的:打印('没有流标题!')时间.sleep(1) 解决方案 如果你对外部库开放,你可以使用 pip install python-vlc
为 python 安装 vlc 绑定并使用 player
方法直接从 URL 播放音频文件,如下所示.
导入vlc导入时间url = 'http://prem1.rockradio.com:80/bluesrock?9555ae7caa92404c73cade1d'#define VLC 实例instance = vlc.Instance('--input-repeat=-1', '--fullscreen')#定义VLC播放器播放器=instance.media_player_new()#定义VLC媒体媒体=instance.media_new(网址)#设置播放器媒体player.set_media(媒体)#播放媒体播放器播放()
vlc
播放器的优点是您可以直接从 URL 播放大多数媒体类型(不仅仅是 mp3),还可以执行类似播放器的选项,例如
<预><代码>>>>player.pause() #暂停播放>>>player.play() #继续播放>>>player.stop() #停止播放
I am using Python 3.5.3 on Windows 8.1 x64 and i need play audio from here
I have tried pyaudio, but it gives me only white noise and error occurred after a few runs of pyaudio (pyaudio module 'pyaudio' has no attribute 'PyAudio').
Please, advise me how better play the streaming audio from url, using Python...
P.S. I already got the song title and artist name with this code:
import requests
import time
import datetime
print(datetime.datetime.now())
import re
url = 'http://prem1.rockradio.com:80/bluesrock?9555ae7caa92404c73cade1d'
encoding = 'latin1'
info = ''
radio_session = requests.Session()
while True:
radio = radio_session.get(url, headers={'Icy-MetaData': '1'}, stream=True)
metaint = int(radio.headers['icy-metaint'])
stream = radio.raw
audio_data = stream.read(metaint)
meta_byte = stream.read(1)
if (meta_byte):
meta_length = ord(meta_byte) * 16
meta_data = stream.read(meta_length).rstrip(b' ')
stream_title = re.search(br"StreamTitle="KFte"]*)';", meta_data)
if stream_title:
stream_title = stream_title.group(1).decode(encoding, errors='replace')
if info != stream_title:
print('Now playing: ', stream_title)
info = stream_title
else:
pass
else:
print('No StreamTitle!')
time.sleep(1)
If you are open for external libraries, you can install vlc binding for python using pip install python-vlc
And use player
method to play audio file directly from URL as below.
import vlc
import time
url = 'http://prem1.rockradio.com:80/bluesrock?9555ae7caa92404c73cade1d'
#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()
Advantage of vlc
player is that you can play most media types directly from URL (not just mp3) and also perform player like options such as
>>> player.pause() #pause play back
>>> player.play() #resume play back
>>> player.stop() #stop play back
这篇关于如何在 Python 3.5.3 上播放来自网络电台的流音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Python 3.5.3 上播放来自网络电台的流音频


- YouTube API v3 返回截断的观看记录 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01