Skip to content

Commit c70fe23

Browse files
committed
Add stop_tone()/with syntax to play_file
1 parent b7d7d7e commit c70fe23

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

adafruit_circuitplayground/express.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -672,22 +672,19 @@ def play_file(self, file_name):
672672
cpx.play_file("rimshot.wav")
673673
"""
674674
# Play a specified file.
675+
self.stop_tone()
675676
self._speaker_enable.value = True
676-
try:
677-
if sys.implementation.version[0] >= 3:
678-
audio = audioio.AudioOut(board.SPEAKER)
679-
file = audioio.WaveFile(open(file_name, "rb"))
680-
audio.play(file)
677+
if sys.implementation.version[0] >= 3:
678+
with audioio.AudioOut(board.SPEAKER) as audio:
679+
wavefile = audioio.WaveFile(open(file_name, "rb"))
680+
audio.play(wavefile)
681681
while audio.playing:
682682
pass
683-
audio.deinit()
684-
else:
685-
audio = audioio.AudioOut(board.SPEAKER, open(file_name, "rb"))
683+
else:
684+
with audioio.AudioOut(board.SPEAKER, open(file_name, "rb")) as audio:
686685
audio.play()
687686
while audio.playing:
688687
pass
689-
except RuntimeError:
690-
pass
691688
self._speaker_enable.value = False
692689

693690

0 commit comments

Comments
 (0)