48static const uint32_t MEDIA_CONTROLS_QUEUE_LENGTH = 20;
50static const UBaseType_t MEDIA_PIPELINE_TASK_PRIORITY = 1;
51static const UBaseType_t ANNOUNCEMENT_PIPELINE_TASK_PRIORITY = 1;
53static const char *
const TAG =
"speaker_media_player";
56#ifdef USE_SPEAKER_MEDIA_PLAYER_ON_OFF
66 VolumeRestoreState volume_restore_state;
67 if (this->
pref_.
load(&volume_restore_state)) {
75#ifdef USE_OTA_STATE_LISTENER
81 ANNOUNCEMENT_PIPELINE_TASK_PRIORITY);
84 ESP_LOGE(TAG,
"Failed to create announcement pipeline");
93 ESP_LOGE(TAG,
"Failed to create media pipeline");
98 ESP_LOGI(TAG,
"Set up speaker media player");
102 switch (pipeline_type) {
131 MediaCallCommand media_command;
134 bool enqueue = media_command.enqueue.has_value() && media_command.enqueue.value();
136 if (media_command.url.has_value() || media_command.file.has_value()) {
137#ifdef USE_SPEAKER_MEDIA_PLAYER_ON_OFF
143 PlaylistItem playlist_item;
144 if (media_command.url.has_value()) {
145 playlist_item.url = *media_command.url.value();
146 delete media_command.url.value();
148 if (media_command.file.has_value()) {
149 playlist_item.file = media_command.file;
152 if (this->
single_pipeline_() || (media_command.announce.has_value() && media_command.announce.value())) {
157 if (media_command.file.has_value()) {
159 }
else if (media_command.url.has_value()) {
176 if (media_command.file.has_value()) {
178 }
else if (media_command.url.has_value()) {
191 if (media_command.volume.has_value()) {
196 if (media_command.command.has_value()) {
197 switch (media_command.command.value()) {
199#ifdef USE_SPEAKER_MEDIA_PLAYER_ON_OFF
216#ifdef USE_SPEAKER_MEDIA_PLAYER_ON_OFF
230#ifdef USE_SPEAKER_MEDIA_PLAYER_ON_OFF
231 if (this->
single_pipeline_() || (media_command.announce.has_value() && media_command.announce.value()) ||
234 if (this->
single_pipeline_() || (media_command.announce.has_value() && media_command.announce.value())) {
289 if (this->
single_pipeline_() || (media_command.announce.has_value() && media_command.announce.value())) {
296 if (this->
single_pipeline_() || (media_command.announce.has_value() && media_command.announce.value())) {
303 if (this->
single_pipeline_() || (media_command.announce.has_value() && media_command.announce.value())) {
320#ifdef USE_OTA_STATE_LISTENER
322 ota::OTAComponent *comp) {
353 ESP_LOGE(TAG,
"The media pipeline's file reader encountered an error.");
355 ESP_LOGE(TAG,
"The media pipeline's audio decoder encountered an error.");
364 ESP_LOGE(TAG,
"The announcement pipeline's file reader encountered an error.");
366 ESP_LOGE(TAG,
"The announcement pipeline's audio decoder encountered an error.");
387 if (playlist_item.url.has_value()) {
389 }
else if (playlist_item.file.has_value()) {
393 if (timeout_ms > 0) {
403#ifdef USE_SPEAKER_MEDIA_PLAYER_ON_OFF
426 if (playlist_item.url.has_value()) {
428 }
else if (playlist_item.file.has_value()) {
432 if (timeout_ms > 0) {
442#ifdef USE_SPEAKER_MEDIA_PLAYER_ON_OFF
454 if (this->state != old_state) {
458#ifdef USE_SPEAKER_MEDIA_PLAYER_ON_OFF
480 MediaCallCommand media_command;
482 media_command.file = media_file;
484 media_command.announce =
true;
486 media_command.announce =
false;
488 media_command.enqueue = enqueue;
498 MediaCallCommand media_command;
500 auto ann =
call.get_announcement();
502 media_command.announce =
true;
504 media_command.announce =
false;
507 const auto &media_url =
call.get_media_url();
508 if (media_url.has_value()) {
510 new std::string(*media_url);
512 auto cmd =
call.get_command();
513 if (cmd.has_value()) {
515 media_command.enqueue =
true;
523 auto vol =
call.get_volume();
524 if (vol.has_value()) {
525 media_command.volume = vol;
531 auto cmd =
call.get_command();
532 if (cmd.has_value()) {
533 media_command.command = cmd;
534 TickType_t ticks_to_wait = portMAX_DELAY;
545 auto traits = media_player::MediaPlayerTraits();
547 traits.set_supports_pause(
true);
549#ifdef USE_SPEAKER_MEDIA_PLAYER_ON_OFF
550 traits.set_supports_turn_off_on(
true);
557 traits.get_supported_formats().push_back(this->
media_format_.value());
562 traits.get_supported_formats().push_back(media_format);
569 VolumeRestoreState volume_restore_state;
570 volume_restore_state.volume = this->
volume;
571 volume_restore_state.is_muted = this->
is_muted_;
588 if (old_mute_state != mute_state) {
615 if (volume < 0.001) {
void mark_failed()
Mark this component as failed.
ESPDEPRECATED("Use const char* overload instead. Removed in 2026.7.0", "2026.1.0") void defer(const std voi defer)(const char *name, std::function< void()> &&f)
Defer a callback to the next loop() call.
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_interval(const std voi set_interval)(const char *name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a unique name.
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") bool cancel_timeout(const std boo cancel_timeout)(const char *name)
Cancel a timeout function.
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") bool cancel_interval(const std boo cancel_interval)(const char *name)
Cancel an interval function.
ESPPreferenceObject make_entity_preference(uint32_t version=0)
Create a preference object for storing this entity's state/settings.
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Inform the parent automation that the event has triggered.
void add_global_state_listener(OTAGlobalStateListener *listener)
virtual void set_volume(float volume)
virtual void set_mute_state(bool mute_state)
OTAGlobalCallback * get_global_ota_callback()
T remap(U value, U min, U max, T min_out, T max_out)
Remap value from the range (min, max) to (min_out, max_out).