试图用sounddevice模块播放音频数据,但我被卡住了

2024-10-06 12:28:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用sounddevice模块播放音频流,我尝试了很多方法,但它们都没有多大意义,似乎对我来说根本不起作用,数据100%有效,如果保存到文件中,它会完美地播放,因此我知道数据不会以任何方式损坏

import requests
import numpy as np
import sounddevice as sd

url = 'https://audio10.broadcastify.com/dbhq0jtm96yrs78.mp3?nocache=7847116&xan=xtf9912b'
#creates a varable for the url

chunk_size = 256 *4
#chunk size

r = requests.get(url, stream = True)
#uses requests.get to get data from link

for chunk in r.iter_content(chunk_size=chunk_size):
#grabs the audio content of the web page with the r varable and putting it into the chunk varable as byte data
    chunk = np.frombuffer(chunk, dtype=np.int8)
    #turns the chunk byte data into an array
    print(chunk)
    #prints the chunk data to show the result

    #somehow use the soundevice module to play back the audio stream? but i dont know how?
    #the only clue that i have is that i cant use sd.play(chunk) because the play function isnt
    #able to play stream data like that

Tags: thetoimporturlplaydatastreamsize