如何从URL播放mp3 [英] How to play mp3 from URL
问题描述
我正在尝试编写一个Python脚本,该脚本将从Soundcloud URL中播放mp3
I'm trying to write a python script that will play mp3 from Soundcloud URL
这是我已经做的:
from urllib.request import urlopen
url = "soundcloud.com/artist/song.mp3"
u = urlopen(url)
data = u.read(1024)
while data:
player.play(data)
data = u.read(1024)
我尝试了pyaudio
,并提供了许多选项,例如更改格式,频道,费率.
我只是从扬声器中听到奇怪的声音,我在Google上搜索pyaudio
播放mp3,但没有找到任何信息.
I tried pyaudio
with many options like changing formats, channels, rate.
and I just get strange sound from the speakers, I searched Google for pyaudio
playing mp3 and didn't found any information.
我尝试通过通过从mp3传递字节来创建Sound
对象,然后仅通过执行播放功能来尝试pygame
.我没有收到任何错误:脚本正在运行,但没有任何播放.
I tried pygame
by creating Sound
object by passing the bytes from the mp3 and then just by executing the play function. I am not getting any errors: the script runs but nothing is playing.
我正在使用Python 3和Ubuntu.
I'm working with Python 3 and Ubuntu.
推荐答案
如果您恰好安装了VLC(或愿意安装它),那么这应该可以工作:
If you happen to have VLC installed (or are willing to install it), then this should work:
import vlc
p = vlc.MediaPlayer("http://your_mp3_url")
p.play()
其优点是,它可以与VLC一起使用的所有功能,而不仅限于MP3.如果需要,也可以将其暂停.
This has the advantage that it works with everything VLC works with, not just MP3. It can also be paused if you want to.
您可以使用
pip install python-vlc
这篇关于如何从URL播放mp3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!