[Python] Pygame can open .ogg file in one scope but not in another

Discussão em 'Python' iniciado por Stack, Maio 17, 2025.

  1. Stack

    Stack Membro Participativo

    In a python script . . .

    def control_audio():
    global audio_flag
    print("audio toggled")
    audio_flag = not audio_flag
    if (audio_flag == True):
    on_msg_button = "/home/mrmoto/Documents/now_speaks.ogg"
    pygame.mixer.music.load(on_msg_button)
    pygame.mixer.music.play()
    amp_mute.on()
    while mixer.music.get_busy():
    pass
    amp_mute.off()
    client.publish("audio_state", "on")

    if (audio_flag == False):
    off_msg_button = "/home/mrmoto/Documents/moto_silent_2.ogg"
    pygame.mixer.music.load(off_msg_button)
    pygame.mixer.music.play()
    amp_mute.on()
    while mixer.music.get_busy():
    pass
    amp_mute.off()
    client.publish("audio_state", "off")


    moto_silent_2 can be opened and played as it should. However, in the same script

    def on_message(client, userdata, message):

    if (message.topic == "mrmoto_alerts"):
    global lasttimeA
    global eventsA
    global lasttimeB
    global eventsB
    msg = message.payload.decode("utf-8")
    if (msg=="prione"):
    eventtime = time.time()
    if (eventtime - lasttimeA >= 10):
    eventsA = eventsA + 1
    e = datetime.datetime.now()
    eventStr = ("PIR1 EVENT {:d} {:%Y-%m-%d %H:%M:%S} \r\n".format(eventsA, e))
    lasttimeA = eventtime
    log_event(eventStr)
    a_msg = "/home/mrmoto/Documents/hallway.ogg"
    play_msg(a_msg)
    log_event(eventStr)


    if (message.topic == "mrmoto_alerts"):
    msg = message.payload.decode("utf-8")
    if (msg =="pritwo"):
    eventtime = time.time()
    if (eventtime - lasttimeB >= 10):
    eventsB = eventsB + 1
    e = datetime.datetime.now()
    eventStr = ("PIR2 EVENT {:d} {:%Y-%m-%d %H:%M:%S} \r\n".format(eventsB, e ))
    lasttimeB = eventtime
    log_event(eventStr)
    b_msg = "/home/mrmoto/Documents/laundry.ogg"
    play_msg(b_msg)
    log_event(eventStr)

    if (message.topic == "audio_check"):
    global audio_flag
    if (audio_flag == True):
    client.publish("audio_state", "on")
    if (audio_flag == False):
    client.publish("audio_state", "off")

    if (message.topic == "command"):

    msg = message.payload.decode("utf-8")
    if (msg == "audio_off"):
    log_event("audio off by command\r\n")
    audio_flag = False
    off_msg = "/home/mrmoto/Documents/moto_slient_2.ogg"
    pygame.mixer.music.load(off_msg)
    pygame.mixer.music.play()
    amp_mute.on()
    while mixer.music.get_busy():
    pass
    amp_mute.off()
    client.publish("audio_state", "off")

    if (msg == "audio_on"):
    log_event("audio on by command\r\n")
    audio_flag = True
    on_msg = "/home/mrmoto/Documents/now_speaks.ogg"
    pygame.mixer.music.load(on_msg)
    pygame.mixer.music.play()
    amp_mute.on()
    while mixer.music.get_busy():
    pass
    amp_mute.off()
    client.publish("audio_state", "on")

    if (msg == "is_moto_running"):
    client.publsh("moto_state", "running")


    Pygame throws the error

    pygame.error: Couldn't open '/home/mrmoto/Documents/moto_slient_2.ogg'


    I have no idea what is or might be going on. Can someone see an error? I know the code is not pretty . . . it was supposed to be a quick experiment.

    control_audio() is responding to the push of a button on a device. on_message() is responding to MQTT messages sent from an iOS app

    Code is running on a Raspberry Pi Zero W

    Continue reading...

Compartilhe esta Página