Skip to content

Commit

Permalink
fix zhongyang219#583 定时器先取得锁再更改播放状态,避免之前未返回就重入
Browse files Browse the repository at this point in the history
  • Loading branch information
lrisora committed Aug 24, 2023
1 parent 3f01865 commit d224eac
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 35 deletions.
75 changes: 40 additions & 35 deletions MusicPlayer2/MusicPlayerDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
6 changes: 6 additions & 0 deletions MusicPlayer2/Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "BassCore.h"
#include "SpectralDataHelper.h"
#include "MediaTransControls.h"
#include <mutex>

#define WM_PLAYLIST_INI_START (WM_USER+104) // 播放列表开始加载时的消息
#define WM_PLAYLIST_INI_COMPLATE (WM_USER+105) // 播放列表加载完成消息
Expand Down Expand Up @@ -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类的唯一的对象
Expand Down

0 comments on commit d224eac

Please sign in to comment.