Skip to content

play_file() with wait_to_finish=False does not play #118

Closed
@FoamyGuy

Description

@FoamyGuy

In the currently released version of this library calling pyportal.peripherals.play_file("somefile.wav", wait_to_finish=False) results in no audio playing.

Full reproducer script:

import board
from adafruit_pyportal import PyPortal

# Create the PyPortal object
pyportal = PyPortal()

# Set display to show REPL
board.DISPLAY.show(None)

pyportal.peripherals.play_file("piano2.wav", wait_to_finish=False)

while True:
    pass

Running the same code on version 6.0.0 of this library does play the wav file.

I think the change to use with context processor is what is causing it not to play the file when wait_to_finish is False

In this section of code it returns in the case of False for wait_to_finish the return causes it to close the with context which closes the file and causes it not to be able to be played.

with open(file_name, "rb") as wavfile:
wavedata = audiocore.WaveFile(wavfile)
self._speaker_enable.value = True
self.audio.play(wavedata)
if not wait_to_finish:
return
while self.audio.playing:
pass
self._speaker_enable.value = False

Unless there is some way to make the lifetime of the with block continue to remain open after the play_file() function returns I think we would need to remove the usage of the with. We could store the reference to the open file on self and close it on subsequent calls before opening a new one.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions