update ffmpeg_speaker

This commit is contained in:
linbo 2025-10-10 10:27:45 +08:00
parent 11f0097521
commit 4c852f5260

View File

@ -78,16 +78,17 @@ void ffmpegSpeaker::start() {
} }
void ffmpegSpeaker::resetPlayState() void ffmpegSpeaker::resetPlayState()
{ {
state_.is_decoding = false;
state_.is_running = false;
state_.is_paused = false;
audio_path_.clear();
if (pcm_handle_) { if (pcm_handle_) {
snd_pcm_drain(pcm_handle_); snd_pcm_drain(pcm_handle_);
snd_pcm_close(pcm_handle_); snd_pcm_close(pcm_handle_);
pcm_handle_ = nullptr; pcm_handle_ = nullptr;
} }
state_.is_decoding = false;
state_.is_running = false;
state_.is_paused = false;
audio_path_.clear();
LOG(INFO) << "[ffmpegSpeaker] (resetPlayState): Success, id=" << id_; LOG(INFO) << "[ffmpegSpeaker] (resetPlayState): Success, id=" << id_;
} }
@ -346,7 +347,12 @@ void ffmpegSpeaker::play_audio_() {
else { else {
//音频数据队列为空且解码已经结束,退出 //音频数据队列为空且解码已经结束,退出
if (!state_.is_decoding) { if (!state_.is_decoding) {
break; if (pcm_handle_) {
// 停止播放,清空缓存,否则结束会有杂音
snd_pcm_drop(pcm_handle_);
LOG(INFO) << "[ffmpegSpeaker] ALSA buffer cleared";
break;
}
} }
this_thread::sleep_for(chrono::seconds(1)); // 空队列等待 this_thread::sleep_for(chrono::seconds(1)); // 空队列等待
} }