快速切换音频管道的会出现管道没有关闭。或者。管道没有启动。
但是切换完成后在SD卡音频播放过程是我就没有干涉音频的播放了。出现问题是在SD卡播放过程偶尔出现出现复位。
控制音频管道的播放。
Code: Select all
static void link_pipeline(int src) //连接新的音源通道
{
if (media_src != src)
{
if (media_src) //原来有在其他音频管道,进行unlink
{
spots_marker(); //记录SD卡插播放状态
ESP_ERROR_CHECK(audio_pipeline_terminate(pipeline));
ESP_ERROR_CHECK(audio_pipeline_unlink(pipeline));
switch (media_src)
{
case MEDIA_SRC_SD:
ESP_ERROR_CHECK(audio_pipeline_unregister(pipeline, fatfs_stream_reader));
ESP_ERROR_CHECK(audio_pipeline_unregister(pipeline, mp3_decoder));
break;
case MEDIA_SRC_FLASH:
audio_hal_set_volume(board_handle->audio_hal, nvs_data->music_vol);
audio_hal_set_volume(board_handle->woofer_hal, nvs_data->music_vol);
ESP_ERROR_CHECK(audio_pipeline_unregister(pipeline, mp3_decoder));
break;
case MEDIA_SRC_A2DP:
ESP_ERROR_CHECK(audio_pipeline_unregister(pipeline, bt_stream_reader));
break;
default:
break;
}
ESP_ERROR_CHECK(audio_pipeline_unregister(pipeline, rsp_handle));
ESP_ERROR_CHECK(audio_pipeline_unregister(pipeline, i2s_writer));
}
media_src = src; //
if (media_src) //有切换到其他音频通道
{
audio_pipeline_register(pipeline, rsp_handle, "filter");
audio_pipeline_register(pipeline, i2s_writer, "i2s");
switch (media_src)
{
case MEDIA_SRC_SD:
ESP_LOGI(TAG, "[1.0] sd Register all elements to audio pipeline");
ESP_ERROR_CHECK(audio_pipeline_register(pipeline, fatfs_stream_reader, "file"));
ESP_ERROR_CHECK(audio_pipeline_register(pipeline, mp3_decoder, "sd mp3"));
ESP_LOGI(TAG, "[2.0] Link it together [sdcard]-->fatfs_stream-->mp3_decoder-->raw");
ESP_ERROR_CHECK(audio_pipeline_link(pipeline, (const char *[]){"file", "sd mp3", "filter", "i2s"}, 4));
break;
case MEDIA_SRC_FLASH:
audio_hal_set_volume(board_handle->audio_hal, nvs_data->tones_vol);
audio_hal_set_volume(board_handle->woofer_hal, nvs_data->tones_vol);
ESP_ERROR_CHECK(audio_element_set_read_cb(mp3_decoder, mp3_music_read_cb, NULL));
ESP_LOGI(TAG, "[1.0] flash Register all elements to audio pipeline");
ESP_ERROR_CHECK(audio_pipeline_register(pipeline, mp3_decoder, "flash mp3"));
ESP_LOGI(TAG, "[2.0] Link it together [sdcard]-->fatfs_stream-->mp3_decoder-->raw");
ESP_ERROR_CHECK(audio_pipeline_link(pipeline, (const char *[]){"flash mp3", "filter", "i2s"}, 3));
break;
case MEDIA_SRC_A2DP:
ESP_LOGI(TAG, "[1.0] bt Register all elements to audio pipeline");
ESP_ERROR_CHECK(audio_pipeline_register(pipeline, bt_stream_reader, "bt"));
ESP_LOGI(TAG, "[2.0] Link it together bt_stream_reader-->mp3_decoder-->raw");
ESP_ERROR_CHECK(audio_pipeline_link(pipeline, (const char *[]){"bt", "filter", "i2s"}, 3));
ESP_ERROR_CHECK(rsp_filter_set_src_info(rsp_handle, 44100, 2));
break;
default:
ESP_ERROR_CHECK(ESP_FAIL);
break;
}
}
}
else
{
if (src)
stop_pipeline();
}
vTaskDelay(100 / portTICK_RATE_MS);
}
Code: Select all
void music_ctrl_task(void) //控制音频任务
{
music_ctrl_cmd_t evt;
while (1)
{
xQueueReceive(music_ctrl_evt_handle, &evt, portMAX_DELAY);
switch (evt)
{
case AEL_USER_CMD_INSET_PLAY:
spots_marker(); //记录SD卡插播放状态
link_pipeline(MEDIA_SRC_SD); //
audio_element_set_uri(fatfs_stream_reader, mp3_file_url);
ESP_ERROR_CHECK(audio_pipeline_run(pipeline));
ESP_ERROR_CHECK(audio_pipeline_wait_for_run(pipeline));
inset_flg = 1;
break;
case AEL_USER_CMD_SD_PLAY: //播放SD卡音频
if(inset_flg == 0)//没有插播中
{
link_pipeline(MEDIA_SRC_SD); //
audio_element_set_uri(fatfs_stream_reader, mp3_file_url);
ESP_ERROR_CHECK(audio_pipeline_run(pipeline));
ESP_ERROR_CHECK(audio_pipeline_wait_for_run(pipeline));
}
else //插播中
{
if (Spots_url)
free(Spots_url);
Spots_url = strdup(mp3_file_url); //记录插播要恢复的音乐
spots_pos = 0;
}
break;
case AEL_USER_SPOTS_RESUME: //插播恢复
if (audio_element_get_state(i2s_writer) == AEL_STATE_FINISHED)
{
link_pipeline(MEDIA_SRC_SD); //
if (Spots_url) //插播有恢复的点
{
audio_element_info_t info; //插播记录插播放命令
audio_element_set_uri(fatfs_stream_reader, Spots_url);
if(spots_pos)
{
audio_element_getinfo(fatfs_stream_reader, &info);
info.byte_pos = spots_pos;
audio_element_setinfo(fatfs_stream_reader, &info); //
}
}
else
{
audio_element_set_uri(fatfs_stream_reader, mp3_file_url);
}
ESP_ERROR_CHECK(audio_pipeline_run(pipeline));
ESP_ERROR_CHECK(audio_pipeline_wait_for_run(pipeline));
clean_spots_marker();
}
break;
case AEL_USER_CMD_SD_STOP: //停止SD卡播放
clean_spots_marker();
if (media_src == MEDIA_SRC_SD)
{
stop_pipeline();
}
if (s_connection_state == ESP_A2D_CONNECTION_STATE_CONNECTED)
{
play_bt_sink_pepiline();
}
break;
case AEL_USER_CMD_FLASH_PLAY: //播放flash里面的音源
link_pipeline(MEDIA_SRC_FLASH); //
ESP_ERROR_CHECK(audio_pipeline_run(pipeline));
ESP_ERROR_CHECK(audio_pipeline_wait_for_run(pipeline));
break;
case AEL_USER_CMD_BT_SW: //切换到蓝牙音源
if (bt_stream_reader && (audio_element_get_state(i2s_writer) == AEL_STATE_FINISHED ||
audio_element_get_state(i2s_writer) == AEL_STATE_INIT))
{
link_pipeline(MEDIA_SRC_A2DP); //
ESP_ERROR_CHECK(audio_pipeline_run(pipeline));
ESP_ERROR_CHECK(audio_pipeline_wait_for_run(pipeline));
ESP_ERROR_CHECK(esp_a2d_media_ctrl(ESP_A2D_MEDIA_CTRL_START));
}
break;
case AEL_USER_BT_DISABLE: //蓝牙失能
if (media_src == MEDIA_SRC_A2DP) //原来在蓝牙通道退出蓝牙通道
{
link_pipeline(MEDIA_SRC_NULL); //退出蓝牙通道
}
esp_bt_disable();
break;
case AEL_USER_BT_ENABLE: //蓝牙使能
esp_bt_enable();
break;
case AEL_USER_CMD_VOLUP: //音量加
nvs_data->music_vol += 10;
if (nvs_data->music_vol > 100)
{
nvs_data->music_vol = 100;
}
if(media_src != MEDIA_SRC_FLASH)//不改变提示的音量
{
audio_hal_set_volume(board_handle->audio_hal, nvs_data->music_vol);
audio_hal_set_volume(board_handle->woofer_hal, nvs_data->music_vol);
}
save_nvs_data();
break;
case AEL_USER_CMD_VOLDOWN: //音量减
nvs_data->music_vol -= 10;
if (nvs_data->music_vol < 0)
{
nvs_data->music_vol = 0;
}
if (media_src != MEDIA_SRC_FLASH)//不改变提示的音量
{
audio_hal_set_volume(board_handle->audio_hal, nvs_data->music_vol);
audio_hal_set_volume(board_handle->woofer_hal, nvs_data->music_vol);
}
save_nvs_data();
break;
default:
break;
}
}
}