分页: 1 / 1

使用 PICO 播放 MP3 音乐

发表于 : 2021年 4月 27日 21:52
shaoziyang
在 PICO 的 CircuitPython 6.2.0 中,内置了一个音频MP3库,因此可以播放音乐。

图片

参考程序

代码: 全选

import time
import board
import digitalio
 
from audiomp3 import MP3Decoder
 
try:
 from audioio import AudioOut
except ImportError:
 try:
 from audiopwmio import PWMAudioOut as AudioOut
 except ImportError:
 pass # not always supported by every board!
 
button1 = digitalio.DigitalInOut(board.GP20)
button1.switch_to_input(pull=digitalio.Pull.UP)
 
# Replace with your mp3files name
mp3files = ["Selamat.mp3"]
 
# You have to specify some mp3 file when creating the decoder
mp3 = open(mp3files[0], "rb")
decoder = MP3Decoder(mp3)
audio = AudioOut(board.GP18)
 
while True:
 if not button1.value:
 decoder.file = open(mp3files[0], "rb")
 audio.play(decoder)
 print("playing", mp3files[0])
 
 t = time.monotonic()
 while time.monotonic() – t < 3:
 pass

说明
隐藏内容
你必须登入/注册才可观看隐藏内容