Skip to content

SD card randomly freezes with repeated mp3 playback #8017

Open
@gallaugher

Description

@gallaugher

CircuitPython version

8.0.5

Code/REPL

# https://github.com/gallaugher/disco-button/blob/main/demo_of_sd_mp3_play_break_with_sd.py
# note repo above also has mount_sd.py, but it's the standard one that's from Adafruit's learn guide
# Just a test of playing MP3 files when pushing a button

import board, time, microcontroller, mount_sd, digitalio, random

from audiopwmio import PWMAudioOut as AudioOut
from audiomp3 import MP3Decoder

from adafruit_debouncer import Button

# create a debounced Button
button_input = digitalio.DigitalInOut(board.GP21)
button_input.switch_to_input(pull=digitalio.Pull.UP) # False when pressed, True when not pressed
button = Button(button_input)

# setup the speaker
audio = AudioOut(board.GP15) # assuming speaker plug tip to GP16

# set path where audio files can be found on device
path = "/sd/disco_songs/"

# set up the mp3 decoder
filename = "Boogie Wonderland.mp3"
mp3_file = open(path + filename, "rb")
decoder = MP3Decoder(mp3_file)

# song is normally sent over MQTT
songs = ["Disco Inferno",
        "Boogie Wonderland",
        "Play That Funky Music",
        "Stayin' Alive"]

# play an mp3 file - pass in string that includes filename extension
def play_mp3(filename):
#     if audio.playing:
#         audio.stop()
    print(f"About to play {path + filename}")
    try:
        decoder.file = open(path + filename, "rb")
        audio.play(decoder)
    except OSError as e:
        print(f"No such file/directory: {path + filename}\nERROR: {e}\nRESTARTING")
        time.sleep(5.0)
        audio.stop()
    except:
        print("UNKNOWN ERROR - RESTARTING - error while playing sound")
        time.sleep(5.0)
        microcontroller.reset()

while True:
    button.update()
    if button.pressed:
        print("BUTTON PRESSED")
        song = random.choice(songs)+".mp3"
        play_mp3(song)
    if button.released:
        print("button released")

Behavior

Big squelchy crash that freezes the board. No exception thrown to the serial console.

Description

DanH suggested I post this issue after a conversation on Discord. I noticed when using a Raspberry Pi Pico W & Adafruit SD/SDIO card breakout that randomly (but often) mp3 playback will crash & freeze completely without throwing an error if an mp3 file is played over an existing mp3 file.
Here's a URL showing code that you can use to replicate the crash. Pressing buttons will randomly play an mp3 file (these are four full-length popular disco songs - can't share here due to copyright, but you should be able to get these, or DM me for private send for educational purposes). If you press the button every few seconds you'll eventually crash, often with a squelching loud sound, but no exception thrown.
I was able to work around this by using the commented out code that checks to see if am mp3 file is playing & if so, issues a .stop()
As always - thanks for your work, team. You are all truly wonderful humans!
https://github.com/gallaugher/disco-button/blob/main/demo_of_sd_mp3_play_break_with_sd.py

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions