Skip to content

Commit

Permalink
Merge pull request #116 from joebaird/main
Browse files Browse the repository at this point in the history
Add call to stop audio playback
  • Loading branch information
FoamyGuy authored Dec 13, 2021
2 parents a160fd1 + 02a7b86 commit 8031c95
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
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

0 comments on commit 8031c95

Please sign in to comment.