Skip to content

Add call to stop audio playback #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions adafruit_pyportal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ def __init__(
self.set_backlight = self.peripherals.set_backlight
self.sd_check = self.peripherals.sd_check
self.play_file = self.peripherals.play_file
self.stop_play = self.peripherals.stop_play

self.image_converter_url = self.network.image_converter_url
self.wget = self.network.wget
Expand Down
29 changes: 21 additions & 8 deletions adafruit_pyportal/peripherals.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def __init__(self, spi, display, splash_group, debug=False):
self.audio = audioio.AudioOut(board.SPEAKER)
else:
raise AttributeError("Board does not have a builtin speaker!")
self.wavfile = None

if debug:
print("Init SD Card")
Expand Down Expand Up @@ -144,16 +145,28 @@ def play_file(self, file_name, wait_to_finish=True):
"""Play a wav file.

:param str file_name: The name of the wav file to play on the speaker.
:param bool wait_to_finish: flag to determine if this is a blocking call

"""
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

# pylint: disable=consider-using-with
# can't use `with` because we need wavefile to remain open after return
self.wavfile = open(file_name, "rb")
wavedata = audiocore.WaveFile(self.wavfile)
self._speaker_enable.value = True
self.audio.play(wavedata)
if not wait_to_finish:
return
while self.audio.playing:
pass
self.wavfile.close()
self._speaker_enable.value = False

def stop_play(self):
"""Stops playing a wav file."""
self.audio.stop()
if self.wavfile is not None:
self.wavfile.close()
self._speaker_enable.value = False

def sd_check(self):
Expand Down