File tree 1 file changed +26
-1
lines changed
adafruit_circuitplayground
1 file changed +26
-1
lines changed Original file line number Diff line number Diff line change @@ -49,7 +49,6 @@ def __init__(self):
49
49
# Define audio:
50
50
self ._speaker_enable = digitalio .DigitalInOut (board .SPEAKER_ENABLE )
51
51
self ._speaker_enable .switch_to_output (value = False )
52
-
53
52
self .sample = None
54
53
self .sine_wave = None
55
54
@@ -445,6 +444,32 @@ def stop_tone(self):
445
444
self .sample .stop ()
446
445
self ._speaker_enable .value = False
447
446
447
+ def play_file (self , file_name ):
448
+ """ Play a .wav file using the onboard speaker.
449
+
450
+ :param file_name: The name of your .wav file in quotation marks including .wav
451
+
452
+ .. image :: /_static/speaker.jpg
453
+
454
+ .. code-block:: python
455
+
456
+ from adafruit_circuitplayground.express import cpx
457
+
458
+ while True:
459
+ if cpx.button_a:
460
+ cpx.play_file("laugh.wav")
461
+ elif cpx.button_b:
462
+ cpx.play_file("rimshot.wav")
463
+ """
464
+ # Play a specified file.
465
+ self ._speaker_enable .value = True
466
+ self .a = audioio .AudioOut (board .SPEAKER , open (file_name , "rb" ))
467
+
468
+ self .a .play ()
469
+ while self .a .playing :
470
+ pass
471
+ self ._speaker_enable .value = False
472
+
448
473
449
474
cpx = Express ()
450
475
"""Object that is automatically created on import.
You can’t perform that action at this time.
0 commit comments