Fix i2s media player volume control (#4813)

This commit is contained in:
Jesse Hills 2023-05-13 17:19:06 +12:00
parent e0ee8ca17c
commit 625126df68
No known key found for this signature in database
GPG key ID: BEAAE804EFD8E83A

View file

@ -22,14 +22,14 @@ void I2SAudioMediaPlayer::control(const media_player::MediaPlayerCall &call) {
this->start(); this->start();
} }
} }
if (this->i2s_state_ != I2S_STATE_RUNNING) {
return;
}
if (call.get_volume().has_value()) { if (call.get_volume().has_value()) {
this->volume = call.get_volume().value(); this->volume = call.get_volume().value();
this->set_volume_(volume); this->set_volume_(volume);
this->unmute_(); this->unmute_();
} }
if (this->i2s_state_ != I2S_STATE_RUNNING) {
return;
}
if (call.get_command().has_value()) { if (call.get_command().has_value()) {
switch (call.get_command().value()) { switch (call.get_command().value()) {
case media_player::MEDIA_PLAYER_COMMAND_PLAY: case media_player::MEDIA_PLAYER_COMMAND_PLAY:
@ -97,7 +97,8 @@ void I2SAudioMediaPlayer::unmute_() {
this->muted_ = false; this->muted_ = false;
} }
void I2SAudioMediaPlayer::set_volume_(float volume, bool publish) { void I2SAudioMediaPlayer::set_volume_(float volume, bool publish) {
this->audio_->setVolume(remap<uint8_t, float>(volume, 0.0f, 1.0f, 0, 21)); if (this->audio_ != nullptr)
this->audio_->setVolume(remap<uint8_t, float>(volume, 0.0f, 1.0f, 0, 21));
if (publish) if (publish)
this->volume = volume; this->volume = volume;
} }
@ -157,13 +158,23 @@ void I2SAudioMediaPlayer::start_() {
#endif #endif
this->i2s_state_ = I2S_STATE_RUNNING; this->i2s_state_ = I2S_STATE_RUNNING;
this->high_freq_.start(); this->high_freq_.start();
this->audio_->setVolume(remap<uint8_t, float>(this->volume, 0.0f, 1.0f, 0, 21));
if (this->current_url_.has_value()) { if (this->current_url_.has_value()) {
this->audio_->connecttohost(this->current_url_.value().c_str()); this->audio_->connecttohost(this->current_url_.value().c_str());
this->state = media_player::MEDIA_PLAYER_STATE_PLAYING; this->state = media_player::MEDIA_PLAYER_STATE_PLAYING;
this->publish_state(); this->publish_state();
} }
} }
void I2SAudioMediaPlayer::stop() { this->i2s_state_ = I2S_STATE_STOPPING; } void I2SAudioMediaPlayer::stop() {
if (this->i2s_state_ == I2S_STATE_STOPPED) {
return;
}
if (this->i2s_state_ == I2S_STATE_STARTING) {
this->i2s_state_ = I2S_STATE_STOPPED;
return;
}
this->i2s_state_ = I2S_STATE_STOPPING;
}
void I2SAudioMediaPlayer::stop_() { void I2SAudioMediaPlayer::stop_() {
if (this->audio_->isRunning()) { if (this->audio_->isRunning()) {
this->audio_->stopSong(); this->audio_->stopSong();