Fix possible crashes and glitches when playing song previews (#898)

* Add AutoplayPending

* Add locks

* Add Base Update

* Add Base Update

* Add locks
This commit is contained in:
McSchneidar authored and GitHub committed 2026-10-06 05:20:14 +02:00
1 parent 52bdf3950e
commit e7fc959def
4 files changed
+122 -40

No files matched your search

@@ -177,6 +177,7 @@ namespace VocaluxeLib.Menu.SongMenu
protected int _AutoplayDelayinMs = CBase.Config.GetAutoplayPreviewDelay(); protected int _AutoplayDelayinMs = CBase.Config.GetAutoplayPreviewDelay();
protected System.Timers.Timer _AutoplayTimer = new System.Timers.Timer(); protected System.Timers.Timer _AutoplayTimer = new System.Timers.Timer();
protected volatile bool _AutoplayPending = false;
private SColorF _ColorInternal; private SColorF _ColorInternal;
protected SColorF _Color protected SColorF _Color
@@ -228,12 +229,22 @@ namespace VocaluxeLib.Menu.SongMenu
_Initialized = true; _Initialized = true;
} }
public abstract void Update(SScreenSongOptions songOptions); public virtual void Update(SScreenSongOptions songOptions)
{
if (_AutoplayPending)
{
_AutoplayPending = false;
_PreviewSelectedSong();
}
}
public abstract void OnShow(); public abstract void OnShow();
public virtual void OnHide() public virtual void OnHide()
{ {
_AutoplayTimer.Stop();
_AutoplayPending = false;
var check = CBase.Graphics.GetNextScreenType(); var check = CBase.Graphics.GetNextScreenType();
if (CBase.Graphics.GetNextScreenType() == EScreen.Sing) if (CBase.Graphics.GetNextScreenType() == EScreen.Sing)
{ {
@@ -384,6 +395,9 @@ namespace VocaluxeLib.Menu.SongMenu
protected virtual void _EnterCategory(int categoryNr) protected virtual void _EnterCategory(int categoryNr)
{ {
_AutoplayTimer.Stop();
_AutoplayPending = false;
if (!_Initialized) if (!_Initialized)
{ {
return; return;
@@ -400,6 +414,9 @@ namespace VocaluxeLib.Menu.SongMenu
protected virtual void _LeaveCategory() protected virtual void _LeaveCategory()
{ {
_AutoplayTimer.Stop();
_AutoplayPending = false;
if (!_Initialized) if (!_Initialized)
{ {
return; return;
@@ -429,6 +446,9 @@ namespace VocaluxeLib.Menu.SongMenu
protected void _ResetPreview(bool playBGagain = true) protected void _ResetPreview(bool playBGagain = true)
{ {
_AutoplayTimer.Stop();
_AutoplayPending = false;
if (_PreviewNrInternal == -1) if (_PreviewNrInternal == -1)
{ {
return; return;
@@ -454,9 +474,17 @@ namespace VocaluxeLib.Menu.SongMenu
protected void _InitializeAutoplayTimer() protected void _InitializeAutoplayTimer()
{ {
_AutoplayTimer.Stop();
_AutoplayPending = false;
_AutoplayTimer.Interval = _AutoplayDelayinMs; _AutoplayTimer.Interval = _AutoplayDelayinMs;
_AutoplayTimer.AutoReset = false; _AutoplayTimer.AutoReset = false;
_AutoplayTimer.Elapsed += (object sender, System.Timers.ElapsedEventArgs e) => { _PreviewSelectedSong(); }; _AutoplayTimer.Elapsed -= _OnAutoplayTimerElapsed;
_AutoplayTimer.Elapsed += _OnAutoplayTimerElapsed;
}
private void _OnAutoplayTimerElapsed(object sender, System.Timers.ElapsedEventArgs e)
{
_AutoplayPending = true;
} }
protected void _AutoplayPreviewIfEnabled() protected void _AutoplayPreviewIfEnabled()
@@ -469,16 +497,10 @@ namespace VocaluxeLib.Menu.SongMenu
protected void _PlayPreviewAfterDelay() protected void _PlayPreviewAfterDelay()
{ {
if (!_AutoplayTimer.Enabled) _AutoplayPending = false;
{
_AutoplayTimer.Start();
}
else
{
_AutoplayTimer.Stop(); _AutoplayTimer.Stop();
_AutoplayTimer.Start(); _AutoplayTimer.Start();
} }
}
#region ThemeEdit #region ThemeEdit
public void MoveElement(int stepX, int stepY) public void MoveElement(int stepX, int stepY)
@@ -246,6 +246,8 @@ namespace VocaluxeLib.Menu.SongMenu
public override void Update(SScreenSongOptions songOptions) public override void Update(SScreenSongOptions songOptions)
{ {
base.Update(songOptions);
if (songOptions.Selection.RandomOnly) if (songOptions.Selection.RandomOnly)
{ {
_PreviewSelectedSong(); _PreviewSelectedSong();
@@ -220,6 +220,8 @@ namespace VocaluxeLib.Menu.SongMenu
public override void Update(SScreenSongOptions songOptions) public override void Update(SScreenSongOptions songOptions)
{ {
base.Update(songOptions);
if (songOptions.Selection.RandomOnly) if (songOptions.Selection.RandomOnly)
{ {
_PreviewSelectedSong(); _PreviewSelectedSong();
+87 -31
View File
@@ -1,4 +1,4 @@
#region license #region license
// This file is part of Vocaluxe. // This file is part of Vocaluxe.
// //
// Vocaluxe is free software: you can redistribute it and/or modify // Vocaluxe is free software: you can redistribute it and/or modify
@@ -24,6 +24,7 @@ namespace VocaluxeLib.Utils.Player
{ {
public class CSongPlayer : CSoundPlayer public class CSongPlayer : CSoundPlayer
{ {
private readonly object _lock = new object();
private CSong _Song; private CSong _Song;
private bool _VideoEnabled; private bool _VideoEnabled;
private CVideoStream _Video; private CVideoStream _Video;
@@ -31,8 +32,16 @@ namespace VocaluxeLib.Utils.Player
public bool VideoEnabled public bool VideoEnabled
{ {
get { return _VideoEnabled; } get
{
lock (_lock)
{
return _VideoEnabled;
}
}
set set
{
lock (_lock)
{ {
if (_VideoEnabled == value) if (_VideoEnabled == value)
{ {
@@ -50,18 +59,24 @@ namespace VocaluxeLib.Utils.Player
} }
} }
} }
}
public int SongId public int SongId
{ {
get { return _Song == null ? -1 : _Song.Id; } get
{
var song = _Song;
return song == null ? -1 : song.Id;
}
} }
public bool SongHasVideo public bool SongHasVideo
{ {
get get
{ {
return _Song != null && !string.IsNullOrEmpty(_Song.Folder) && !string.IsNullOrEmpty(_Song.Video) && var song = _Song;
File.Exists(Path.Combine(_Song.Folder, _Song.Video)); return song != null && !string.IsNullOrEmpty(song.Folder) && !string.IsNullOrEmpty(song.Video) &&
File.Exists(Path.Combine(song.Folder, song.Video));
} }
} }
@@ -69,9 +84,10 @@ namespace VocaluxeLib.Utils.Player
{ {
get get
{ {
if (_Song != null && !string.IsNullOrEmpty(_Song.Artist) && !string.IsNullOrEmpty(_Song.Title)) var song = _Song;
if (song != null && !string.IsNullOrEmpty(song.Artist) && !string.IsNullOrEmpty(song.Title))
{ {
return _Song.Artist + " - " + _Song.Title; return song.Artist + " - " + song.Title;
} }
return base.ArtistAndTitle; return base.ArtistAndTitle;
@@ -80,35 +96,43 @@ namespace VocaluxeLib.Utils.Player
public CTextureRef Cover public CTextureRef Cover
{ {
get { return _Song == null ? CBase.Cover.GetNoCover() : _Song.CoverTextureBig; } get
{
var song = _Song;
return song == null ? CBase.Cover.GetNoCover() : song.CoverTextureBig;
}
} }
public CSongPlayer(bool loop = false) : base(loop) { } public CSongPlayer(bool loop = false) : base(loop) { }
public CTextureRef GetVideoTexture() public CTextureRef GetVideoTexture()
{ {
if (_Video == null || _Song == null) lock (_lock)
{ {
var video = _Video;
var song = _Song;
if (video == null || video.IsClosed() || song == null || CBase.Video == null || CBase.Sound == null)
return null; return null;
}
if (CBase.Video.GetFrame(_Video, CBase.Sound.GetPosition(_StreamId))) if (CBase.Video.GetFrame(video, CBase.Sound.GetPosition(_StreamId)))
{
var texture = video.Texture;
if (texture != null)
{ {
if (_VideoFading != null) if (_VideoFading != null)
{ {
bool finished; bool finished;
_Video.Texture.Color.A = _VideoFading.GetValue(out finished); texture.Color.A = _VideoFading.GetValue(out finished);
if (finished) if (finished)
{
_VideoFading = null; _VideoFading = null;
} }
return texture;
} }
return _Video.Texture;
} }
return null; return null;
} }
}
public void Load(CSong song, float position = 0f, bool autoplay = false) public void Load(CSong song, float position = 0f, bool autoplay = false)
{ {
@@ -117,14 +141,20 @@ namespace VocaluxeLib.Utils.Player
throw new ArgumentNullException("song"); throw new ArgumentNullException("song");
} }
lock (_lock)
{
Load(song.GetMP3(), position, autoplay); Load(song.GetMP3(), position, autoplay);
_Song = song; _Song = song;
_LoadVideo(); _LoadVideo();
} }
}
private void _LoadVideo() private void _LoadVideo()
{ {
if (_Song == null) lock (_lock)
{
var song = _Song;
if (song == null)
{ {
return; return;
} }
@@ -134,82 +164,107 @@ namespace VocaluxeLib.Utils.Player
return; return;
} }
var videoFilePath = Path.Combine(_Song.Folder, _Song.Video); var videoFilePath = Path.Combine(song.Folder, song.Video);
_Video = CBase.Video.Load(videoFilePath); var video = CBase.Video?.Load(videoFilePath);
if (_Video == null) if (video == null)
{ {
return; return;
} }
_Video = video;
_VideoFading = new CFading(0f, 1f, 3f); _VideoFading = new CFading(0f, 1f, 3f);
if (IsPlaying) if (IsPlaying)
{ {
CBase.Video.Skip(_Video, Position, _Song.VideoGap); CBase.Video?.Skip(video, Position, song.VideoGap);
CBase.Video.Resume(_Video); CBase.Video?.Resume(video);
} }
else else
{ {
CBase.Video.Skip(_Video, 0f, _Song.VideoGap); CBase.Video?.Skip(video, 0f, song.VideoGap);
}
} }
} }
public override bool Play() public override bool Play()
{
lock (_lock)
{ {
if (!base.Play()) if (!base.Play())
{ {
return false; return false;
} }
if (_Video != null) var video = _Video;
var song = _Song;
if (video != null && !video.IsClosed() && song != null)
{ {
CBase.Video.Skip(_Video, Position, _Song.VideoGap); CBase.Video?.Skip(video, Position, song.VideoGap);
CBase.Video.Resume(_Video); CBase.Video?.Resume(video);
} }
return true; return true;
} }
}
public override bool Pause() public override bool Pause()
{
lock (_lock)
{ {
if (!base.Pause()) if (!base.Pause())
{ {
return false; return false;
} }
if (_Video != null) var video = _Video;
if (video != null && !video.IsClosed())
{ {
CBase.Video.Pause(_Video); CBase.Video?.Pause(video);
} }
return true; return true;
} }
}
public override bool Stop() public override bool Stop()
{
lock (_lock)
{ {
if (!base.Stop()) if (!base.Stop())
{ {
return false; return false;
} }
if (_Video != null) var video = _Video;
var song = _Song;
if (video != null && !video.IsClosed())
{ {
CBase.Video.Pause(_Video); CBase.Video?.Pause(video);
CBase.Video.Skip(_Video, 0f, _Song.VideoGap); if (song != null)
{
CBase.Video?.Skip(video, 0f, song.VideoGap);
}
} }
return true; return true;
} }
}
private void _CloseVideo() private void _CloseVideo()
{
lock (_lock)
{ {
if (_Video != null) if (_Video != null)
{ {
CBase.Video.Close(ref _Video); CBase.Video?.Close(ref _Video);
_Video = null;
}
} }
} }
public override void Close() public override void Close()
{
lock (_lock)
{ {
base.Close(); base.Close();
@@ -218,3 +273,4 @@ namespace VocaluxeLib.Utils.Player
} }
} }
} }
}