Playing .mp3 in Python from an URL

Viewed 130

I am trying to play audio in my python code where I am taking a response from a free dictionary API and trying to play the pronunciation of the word. I am doing a GET request from https://api.dictionaryapi.dev/api/v2/entries/en/hello and from the response I am extracting the audio URL from obj[0]['phonetics'][0]['audio'].

I am playing it using vlc

import vlc

url = obj[0]['phonetics'][0]['audio']
p = vlc.MediaPlayer("https:{}".format(url))
p.play()

It does play correctly, but after I quit my app it displays an error: [0000021593639c90] prefetch stream error: unimplemented query (264) in control

I was not able to find a way to remove this error. How do I stop it from printing this error message? Are there other alternatives to play the .mp3 from the URL? Any help is appreciated. Thanks.

1 Answers

You could capture the output and redirect it somewhere else.

Python's contextlib has a couple of context managers for redirecting stdout and stderr. I don't know which you need but the code would be something like:

from contextlib import redirect_stdout
from io import StringIO

with redirect_stdout(StringIO()):
    p.play()

This is using redirect_stdout.

edit: This approach doesn't work as the stdout is coming at the C library level. See this article, and this third party library which can help.

Related