Skip to content

Commit d0aa6dc

Browse files
authored
Merge pull request #36 from kattni/play-file-stop-tone-fix
Resolves issues using play_file and start/stop_tone
2 parents 862a379 + c70fe23 commit d0aa6dc

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

adafruit_circuitplayground/express.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,8 @@ def stop_tone(self):
649649
# Stop playing any tones.
650650
if self._sample is not None and self._sample.playing:
651651
self._sample.stop()
652+
self._sample.deinit()
653+
self._sample = None
652654
self._speaker_enable.value = False
653655

654656
def play_file(self, file_name):
@@ -670,18 +672,19 @@ def play_file(self, file_name):
670672
cpx.play_file("rimshot.wav")
671673
"""
672674
# Play a specified file.
675+
self.stop_tone()
673676
self._speaker_enable.value = True
674677
if sys.implementation.version[0] >= 3:
675-
audio = audioio.AudioOut(board.SPEAKER)
676-
file = audioio.WaveFile(open(file_name, "rb"))
677-
audio.play(file)
678-
while audio.playing:
679-
pass
678+
with audioio.AudioOut(board.SPEAKER) as audio:
679+
wavefile = audioio.WaveFile(open(file_name, "rb"))
680+
audio.play(wavefile)
681+
while audio.playing:
682+
pass
680683
else:
681-
audio = audioio.AudioOut(board.SPEAKER, open(file_name, "rb"))
682-
audio.play()
683-
while audio.playing:
684-
pass
684+
with audioio.AudioOut(board.SPEAKER, open(file_name, "rb")) as audio:
685+
audio.play()
686+
while audio.playing:
687+
pass
685688
self._speaker_enable.value = False
686689

687690

0 commit comments

Comments
 (0)