This commit is contained in:
2026-09-19 10:29:15 +02:00
2 changed files with 131 additions and 87 deletions
+54 -12
View File
@@ -14,7 +14,7 @@ namespace Casino
{
private readonly MediaPlayer player = new MediaPlayer();
private readonly MediaPlayer shortplayer = new MediaPlayer();
private readonly Dictionary<int, string> soundCache = new Dictionary<int, string>();
private readonly List<MediaPlayer> activePlayers = new();
public Audioplayer() { shortplayer.MediaEnded += Player_MediaEnded; }
@@ -23,18 +23,60 @@ namespace Casino
if (audioBlob == null || audioBlob.Length == 0)
return;
if (!soundCache.TryGetValue(soundId, out string tempFile))
{
tempFile = System.IO.Path.Combine(System.IO.Path.GetTempPath(),
$"sound_{Guid.NewGuid():N}.mp3");
File.WriteAllBytes(tempFile, audioBlob);
soundCache[soundId] = tempFile;
}
string tempFile = System.IO.Path.Combine(
System.IO.Path.GetTempPath(),
"casino_" + Guid.NewGuid().ToString("N") + ".mp3");
shortplayer.Stop();
shortplayer.Close(); // gibt die alte Datei frei
shortplayer.Open(new Uri(tempFile, UriKind.Absolute));
shortplayer.Play();
try
{
File.WriteAllBytes(tempFile, audioBlob);
MediaPlayer player = new MediaPlayer();
activePlayers.Add(player);
player.MediaEnded += (sender, e) =>
{
player.Close();
activePlayers.Remove(player);
try
{
if (File.Exists(tempFile))
File.Delete(tempFile);
}
catch
{
// Datei wird eventuell noch kurz von Windows verwendet.
}
};
player.MediaFailed += (sender, e) =>
{
player.Close();
activePlayers.Remove(player);
try
{
if (File.Exists(tempFile))
File.Delete(tempFile);
}
catch
{
}
};
player.Open(new Uri(tempFile));
player.Play();
}
catch (Exception ex)
{
MessageBox.Show(
"Fehler beim Abspielen des Sounds:\n" + ex.Message,
"Soundfehler",
MessageBoxButton.OK,
MessageBoxImage.Error);
}
}
private void Player_MediaEnded(object? sender, EventArgs e)