如何从 URL 播放 mp3 [英] How to play mp3 from URL

查看:47
本文介绍了如何从 URL 播放 mp3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个可以从 Soundcloud URL 播放 mp3 的 python 脚本

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.

我尝试了 pygame 通过从 mp3 传递字节然后仅通过执行播放函数来创建 Sound 对象.我没有收到任何错误:脚本运行但没有播放.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆