本文介绍了如何在Python 3.5.3上播放来自互联网广播的流音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在Windows 8.1 x64上使用Python 3.5.3,我需要从此处播放音频a>

I am using Python 3.5.3 on Windows 8.1 x64 and i need play audio from here

我已经尝试过pyaudio,但是它只给我带来了白噪声,并且在运行了pyaudio几次后才出现错误(pyaudio模块'pyaudio'没有属性'PyAudio').

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

请告诉我,如何更好地使用Python播放来自url的流式音频...

Please, advise me how better play the streaming audio from url, using Python...

P.S.我已经获得了带有以下代码的歌曲名称和歌手姓名:

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'\0')

        stream_title = re.search(br"StreamTitle='([^']*)';", 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)

推荐答案

如果您开放使用外部库,则可以使用pip install python-vlc

If you are open for external libraries, you can install vlc binding for python using pip install python-vlc

并使用player方法直接从URL播放音频文件,如下所示.

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

vlc播放器的优点在于,您可以直接从URL播放大多数媒体类型(不仅是mp3),还可以执行类似

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

>>> play.pause()  #pause play back
>>> player.play() #resume play back
>>> player.stop() #stop play back

这篇关于如何在Python 3.5.3上播放来自互联网广播的流音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 21:00