Please tell me and, if possible, provide a working code with comments that could play audio from the buffer?
The question is that I found a working code that writes an audio stream to an mp3 file:
import requests
stream_url = 'https://ep256.hostingradio.ru:8052/europaplus256.mp3'
r = requests.get(stream_url, stream=True)
with open('stream.mp3', 'wb') as f:
try:
for block in r.iter_content(1024):
f.write(block)
except KeyboardInterrupt:
pass
I thought that since it is being written to a file, then it can be played from some kind of buffer, am I right in thinking? The following ridiculous attempts were made (obviously not working):
import requests
import simpleaudio as sa
stream_url = 'https://ep256.hostingradio.ru:8052/europaplus256.mp3'
r = requests.get(stream_url, stream=True)
with r:
try:
for block in r.iter_content(1024):
play_obj = sa.play_buffer(block, 1, 2, 44100)
except KeyboardInterrupt:
pass
Please tell me how to write not to a file with that first code, but to redirect to a simpleaudio stream … I understand that I am doing something wrong with the buffer, maybe some transformations need to be done or I’m not letting it in a loop. Please do not scold too much …