@@ -44,7 +44,8 @@ static const char *mp3_file_ptr[][2] = {
4444#endif
4545};
4646
47- static uint8_t mp3_file_index = 0 ;
47+ static uint8_t aplay_mode = 1 ;
48+ static uint8_t mp3_file_index = 0 ;
4849static uint8_t playback_pending = 0 ;
4950
5051static void audio_player_task (void * pvParameters )
@@ -63,12 +64,26 @@ static void audio_player_task(void *pvParameters)
6364 while (1 ) {
6465 xEventGroupWaitBits (
6566 user_event_group ,
66- AUDIO_PLAYER_RUN_BIT ,
67+ AUDIO_PLAYER_RUN_BIT | AUDIO_PLAYER_EXIT_BIT ,
6768 pdFALSE ,
6869 pdFALSE ,
6970 portMAX_DELAY
7071 );
7172
73+ EventBits_t uxBits = xEventGroupGetBits (user_event_group );
74+ if (uxBits & AUDIO_PLAYER_EXIT_BIT ) {
75+ xEventGroupSetBits (user_event_group , AUDIO_PLAYER_IDLE_BIT );
76+ xEventGroupClearBits (user_event_group , AUDIO_PLAYER_RUN_BIT );
77+
78+ free (synth );
79+ free (frame );
80+ free (stream );
81+
82+ ESP_LOGI (TAG , "exited." );
83+
84+ vTaskDelete (NULL );
85+ }
86+
7287 // Initialize mp3 parts
7388 mad_stream_init (stream );
7489 mad_frame_init (frame );
@@ -108,7 +123,8 @@ static void audio_player_task(void *pvParameters)
108123 free (stream );
109124
110125 ESP_LOGE (TAG , "unrecoverable error" );
111- esp_restart ();
126+
127+ vTaskDelete (NULL );
112128}
113129
114130void audio_player_play_file (uint8_t idx )
@@ -123,6 +139,9 @@ void audio_player_play_file(uint8_t idx)
123139 }
124140 mp3_file_index = idx ;
125141 EventBits_t uxBits = xEventGroupGetBits (user_event_group );
142+ if (uxBits & AUDIO_PLAYER_EXIT_BIT ) {
143+ return ;
144+ }
126145 if (uxBits & AUDIO_PLAYER_RUN_BIT ) {
127146 // Previous playback is still not complete
128147 playback_pending = 1 ;
@@ -133,6 +152,21 @@ void audio_player_play_file(uint8_t idx)
133152#endif
134153}
135154
155+ void audio_player_set_mode (uint8_t idx )
156+ {
157+ #ifdef CONFIG_ENABLE_AUDIO_PROMPT
158+ aplay_mode = idx ;
159+ ESP_LOGI (TAG , "mode: %u" , aplay_mode );
160+
161+ if (aplay_mode ) {
162+ xEventGroupClearBits (user_event_group , AUDIO_PLAYER_EXIT_BIT );
163+ audio_player_init ();
164+ } else {
165+ xEventGroupSetBits (user_event_group , AUDIO_PLAYER_EXIT_BIT );
166+ }
167+ #endif
168+ }
169+
136170void audio_player_init (void )
137171{
138172 EventBits_t uxBits = xEventGroupGetBits (user_event_group );
0 commit comments