From d224eac9937bb4639c721a3ceb4c343a8cc9babc Mon Sep 17 00:00:00 2001 From: lrisora <43085437+lrisora@users.noreply.github.com> Date: Fri, 25 Aug 2023 07:55:56 +0800 Subject: [PATCH] =?UTF-8?q?fix=20#583=20=E5=AE=9A=E6=97=B6=E5=99=A8?= =?UTF-8?q?=E5=85=88=E5=8F=96=E5=BE=97=E9=94=81=E5=86=8D=E6=9B=B4=E6=94=B9?= =?UTF-8?q?=E6=92=AD=E6=94=BE=E7=8A=B6=E6=80=81=EF=BC=8C=E9=81=BF=E5=85=8D?= =?UTF-8?q?=E4=B9=8B=E5=89=8D=E6=9C=AA=E8=BF=94=E5=9B=9E=E5=B0=B1=E9=87=8D?= =?UTF-8?q?=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MusicPlayer2/MusicPlayerDlg.cpp | 75 ++++++++++++++++++--------------- MusicPlayer2/Player.h | 6 +++ 2 files changed, 46 insertions(+), 35 deletions(-) diff --git a/MusicPlayer2/MusicPlayerDlg.cpp b/MusicPlayer2/MusicPlayerDlg.cpp index 60aaa93d4..409bf761b 100644 --- a/MusicPlayer2/MusicPlayerDlg.cpp +++ b/MusicPlayer2/MusicPlayerDlg.cpp @@ -2543,50 +2543,55 @@ void CMusicPlayerDlg::OnTimer(UINT_PTR nIDEvent) // CPlayer::GetInstance().GetPlayerCoreCurrentPosition(); //} - //if (CPlayer::GetInstance().SongIsOver() && (!theApp.m_lyric_setting_data.stop_when_error || !CPlayer::GetInstance().IsError())) //当前曲目播放完毕且没有出现错误时才播放下一曲 - if ((CPlayer::GetInstance().SongIsOver() || (!theApp.m_play_setting_data.stop_when_error && (CPlayer::GetInstance().IsError() || CPlayer::GetInstance().GetSongLength() <= 0))) - && m_play_error_cnt <= CPlayer::GetInstance().GetSongNum() - && CPlayer::GetInstance().IsFileOpened()) //当前曲目播放完毕且没有出现错误时才播放下一曲 + // 这里在更改播放状态,需要先取得锁,没有成功取得锁的话下次再试 + if (CPlayer::GetInstance().GetPlayStatusMutex().try_lock()) { - if (CPlayer::GetInstance().IsError() || CPlayer::GetInstance().GetSongLength() <= 0) - m_play_error_cnt++; - else - m_play_error_cnt = 0; - //当前正在编辑歌词,或顺序播放模式下列表中的歌曲播放完毕时(PlayTrack函数会返回false),播放完当前歌曲就停止播放 - if ((m_pLyricEdit != nullptr && m_pLyricEdit->m_dlg_exist) || !CPlayer::GetInstance().PlayTrack(NEXT, true)) + //if (CPlayer::GetInstance().SongIsOver() && (!theApp.m_lyric_setting_data.stop_when_error || !CPlayer::GetInstance().IsError())) //当前曲目播放完毕且没有出现错误时才播放下一曲 + if ((CPlayer::GetInstance().SongIsOver() || (!theApp.m_play_setting_data.stop_when_error && (CPlayer::GetInstance().IsError() || CPlayer::GetInstance().GetSongLength() <= 0))) + && m_play_error_cnt <= CPlayer::GetInstance().GetSongNum() + && CPlayer::GetInstance().IsFileOpened()) //当前曲目播放完毕且没有出现错误时才播放下一曲 { - CPlayer::GetInstance().MusicControl(Command::STOP); //停止播放 - //ShowTime(); - if (theApp.m_lyric_setting_data.cortana_info_enable) - m_cortana_lyric.ResetCortanaText(); + if (CPlayer::GetInstance().IsError() || CPlayer::GetInstance().GetSongLength() <= 0) + m_play_error_cnt++; + else + m_play_error_cnt = 0; + //当前正在编辑歌词,或顺序播放模式下列表中的歌曲播放完毕时(PlayTrack函数会返回false),播放完当前歌曲就停止播放 + if ((m_pLyricEdit != nullptr && m_pLyricEdit->m_dlg_exist) || !CPlayer::GetInstance().PlayTrack(NEXT, true)) + { + CPlayer::GetInstance().MusicControl(Command::STOP); //停止播放 + //ShowTime(); + if (theApp.m_lyric_setting_data.cortana_info_enable) + m_cortana_lyric.ResetCortanaText(); + } + SwitchTrack(); + UpdatePlayPauseButton(); } - SwitchTrack(); - UpdatePlayPauseButton(); - } - if (CPlayer::GetInstance().IsPlaying() && (theApp.m_play_setting_data.stop_when_error && CPlayer::GetInstance().IsError())) - { - CPlayer::GetInstance().MusicControl(Command::PAUSE); - UpdatePlayPauseButton(); - } - - //处理AB重复 - if (CPlayer::GetInstance().GetABRepeatMode() == CPlayer::AM_AB_REPEAT) - { - Time a_position = CPlayer::GetInstance().GetARepeatPosition(); - Time b_position = CPlayer::GetInstance().GetBRepeatPosition(); - if (a_position > CPlayer::GetInstance().GetSongLength() || b_position > CPlayer::GetInstance().GetSongLength()) + if (CPlayer::GetInstance().IsPlaying() && (theApp.m_play_setting_data.stop_when_error && CPlayer::GetInstance().IsError())) { - CPlayer::GetInstance().ResetABRepeat(); - UpdateABRepeatToolTip(); + CPlayer::GetInstance().MusicControl(Command::PAUSE); + UpdatePlayPauseButton(); } - else + + //处理AB重复 + if (CPlayer::GetInstance().GetABRepeatMode() == CPlayer::AM_AB_REPEAT) { - Time current_play_time{ CPlayer::GetInstance().GetCurrentPosition() }; - if (current_play_time < CPlayer::GetInstance().GetARepeatPosition() || current_play_time > CPlayer::GetInstance().GetBRepeatPosition()) + Time a_position = CPlayer::GetInstance().GetARepeatPosition(); + Time b_position = CPlayer::GetInstance().GetBRepeatPosition(); + if (a_position > CPlayer::GetInstance().GetSongLength() || b_position > CPlayer::GetInstance().GetSongLength()) { - CPlayer::GetInstance().SeekTo(CPlayer::GetInstance().GetARepeatPosition().toInt()); + CPlayer::GetInstance().ResetABRepeat(); + UpdateABRepeatToolTip(); + } + else + { + Time current_play_time{ CPlayer::GetInstance().GetCurrentPosition() }; + if (current_play_time < CPlayer::GetInstance().GetARepeatPosition() || current_play_time > CPlayer::GetInstance().GetBRepeatPosition()) + { + CPlayer::GetInstance().SeekTo(CPlayer::GetInstance().GetARepeatPosition().toInt()); + } } } + CPlayer::GetInstance().GetPlayStatusMutex().unlock(); } if (CWinVersionHelper::IsWindowsVista()) diff --git a/MusicPlayer2/Player.h b/MusicPlayer2/Player.h index ad9ac457a..cfbbc1adf 100644 --- a/MusicPlayer2/Player.h +++ b/MusicPlayer2/Player.h @@ -12,6 +12,7 @@ #include "BassCore.h" #include "SpectralDataHelper.h" #include "MediaTransControls.h" +#include #define WM_PLAYLIST_INI_START (WM_USER+104) // 播放列表开始加载时的消息 #define WM_PLAYLIST_INI_COMPLATE (WM_USER+105) // 播放列表加载完成消息 @@ -237,6 +238,11 @@ class CPlayer static CPlayer m_instance; //CPlayer类唯一的对象 CCriticalSection m_critical; CCriticalSection m_album_cover_sync; //用于专辑封面的线程同步对象 + std::timed_mutex m_play_status_sync; // 更改播放状态时加锁,请使用GetPlayStatusMutex获取 +public: + // 在“稳态”(稳定的播放/暂停/停止)之间切换期间请先持有此锁 + // 避免其他线程中途介入以及当前操作发生重入 + std::timed_mutex& GetPlayStatusMutex() { return m_play_status_sync; } public: //获取CPlayer类的唯一的对象