N
N
Nikita Yaremenko2021-04-19 14:17:07
Python
Nikita Yaremenko, 2021-04-19 14:17:07

How to play audio from URL (python)?

import pyaudio
from urllib.request import urlopen

pyaud = pyaudio.PyAudio()

srate=44100

stream = pyaud.open(format = pyaud.get_format_from_width(1),
                channels = 1,
                rate = srate,
                output = True)


url = "www.anyurl.com"
u = urlopen(url)

data = u.read(8192)

while data:

    stream.write(data)
    data = u.read(8192)

there is the following code, but for some reason it only works with .wav files, how can I convert it to work with .mp3?
the essence of the script is that it runs audio from some site on the computer

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Karbivnichy, 2021-04-19
@nyar_roller

Either convert to wav or use another library:

import pyglet

music = pyglet.resource.media('music.mp3')
music.play()

pyglet.app.run()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question