ESPHome 2026.1.0-dev
Loading...
Searching...
No Matches
speaker_media_player.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_ESP32
4
5#include "audio_pipeline.h"
6
10
15
16#ifdef USE_OTA_STATE_LISTENER
18#endif
19
20#include <deque>
21#include <freertos/FreeRTOS.h>
22#include <freertos/queue.h>
23
24namespace esphome {
25namespace speaker {
26
35
40
42 float volume;
44};
45
48#ifdef USE_OTA_STATE_LISTENER
49 ,
51#endif
52{
53 public:
54 float get_setup_priority() const override { return esphome::setup_priority::PROCESSOR; }
55 void setup() override;
56 void loop() override;
57
58#ifdef USE_OTA_STATE_LISTENER
59 void on_ota_global_state(ota::OTAState state, float progress, uint8_t error, ota::OTAComponent *comp) override;
60#endif
61
62 // MediaPlayer implementations
64 bool is_muted() const override { return this->is_muted_; }
65
66 void set_buffer_size(size_t buffer_size) { this->buffer_size_ = buffer_size; }
67 void set_task_stack_in_psram(bool task_stack_in_psram) { this->task_stack_in_psram_ = task_stack_in_psram; }
68
69 // Percentage to increase or decrease the volume for volume up or volume down commands
70 void set_volume_increment(float volume_increment) { this->volume_increment_ = volume_increment; }
71
72 // Volume used initially on first boot when no volume had been previously saved
73 void set_volume_initial(float volume_initial) { this->volume_initial_ = volume_initial; }
74
75 void set_volume_max(float volume_max) { this->volume_max_ = volume_max; }
76 void set_volume_min(float volume_min) { this->volume_min_ = volume_min; }
77
78 void set_announcement_speaker(Speaker *announcement_speaker) { this->announcement_speaker_ = announcement_speaker; }
80 this->announcement_format_ = announcement_format;
81 }
82 void set_media_speaker(Speaker *media_speaker) { this->media_speaker_ = media_speaker; }
84 this->media_format_ = media_format;
85 }
86
87 Trigger<> *get_mute_trigger() const { return this->mute_trigger_; }
88 Trigger<> *get_unmute_trigger() const { return this->unmute_trigger_; }
90
91 void play_file(audio::AudioFile *media_file, bool announcement, bool enqueue);
92
93 void set_playlist_delay_ms(AudioPipelineType pipeline_type, uint32_t delay_ms);
94
95 protected:
96 // Receives commands from HA or from the voice assistant component
97 // Sends commands to the media_control_commanda_queue_
98 void control(const media_player::MediaPlayerCall &call) override;
99
101 void set_volume_(float volume, bool publish = true);
102
106 void set_mute_state_(bool mute_state);
107
110
113 inline bool single_pipeline_() { return (this->media_speaker_ == nullptr); }
114
115 // Processes commands from media_control_command_queue_.
117
118 std::unique_ptr<AudioPipeline> announcement_pipeline_;
119 std::unique_ptr<AudioPipeline> media_pipeline_;
122
125 bool media_repeat_one_{false};
127
132
134
135 std::deque<PlaylistItem> announcement_playlist_;
136 std::deque<PlaylistItem> media_playlist_;
137
139
141
142 bool is_paused_{false};
143 bool is_muted_{false};
144
145 // The amount to change the volume on volume up/down commands
147
148 // The initial volume used by Setup when no previous volume was saved
150
153
154 // Used to save volume/mute state for restoration on reboot
156
160};
161
162} // namespace speaker
163} // namespace esphome
164
165#endif
Listener interface for global OTA state changes (includes OTA component pointer).
optional< media_player::MediaPlayerSupportedFormat > announcement_format_
void set_announcement_format(const media_player::MediaPlayerSupportedFormat &announcement_format)
void save_volume_restore_state_()
Saves the current volume and mute state to the flash for restoration.
std::deque< PlaylistItem > announcement_playlist_
void set_volume_increment(float volume_increment)
void set_volume_(float volume, bool publish=true)
Updates this->volume and saves volume/mute state to flash for restortation if publish is true.
void set_task_stack_in_psram(bool task_stack_in_psram)
void play_file(audio::AudioFile *media_file, bool announcement, bool enqueue)
void set_mute_state_(bool mute_state)
Sets the mute state.
Trigger< float > * get_volume_trigger() const
void set_media_speaker(Speaker *media_speaker)
std::deque< PlaylistItem > media_playlist_
std::unique_ptr< AudioPipeline > media_pipeline_
void set_volume_initial(float volume_initial)
optional< media_player::MediaPlayerSupportedFormat > media_format_
void control(const media_player::MediaPlayerCall &call) override
void set_media_format(const media_player::MediaPlayerSupportedFormat &media_format)
void set_playlist_delay_ms(AudioPipelineType pipeline_type, uint32_t delay_ms)
void on_ota_global_state(ota::OTAState state, float progress, uint8_t error, ota::OTAComponent *comp) override
void set_announcement_speaker(Speaker *announcement_speaker)
std::unique_ptr< AudioPipeline > announcement_pipeline_
media_player::MediaPlayerTraits get_traits() override
bool single_pipeline_()
Returns true if the media player has only the announcement pipeline defined, false if both the announ...
const float PROCESSOR
For components that use data from sensors like displays.
Definition component.cpp:82
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
optional< media_player::MediaPlayerCommand > command
optional< audio::AudioFile * > file
optional< audio::AudioFile * > file