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