Files
WIR-MACHEN-ALLE-ARM/Casino/Audioplayer.cs
T
2026-09-19 10:26:56 +02:00

91 lines
2.5 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;
namespace Casino
{
class Audioplayer //ki
{
private readonly MediaPlayer player = new MediaPlayer();
private readonly MediaPlayer shortplayer = new MediaPlayer();
private readonly Dictionary<int, string> soundCache = new Dictionary<int, string>();
public Audioplayer() { shortplayer.MediaEnded += Player_MediaEnded; }
public void PlaySound(byte[] audioBlob, int soundId)
{
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;
}
shortplayer.Stop();
shortplayer.Close(); // gibt die alte Datei frei
shortplayer.Open(new Uri(tempFile, UriKind.Absolute));
shortplayer.Play();
}
private void Player_MediaEnded(object? sender, EventArgs e)
{
player.Position = TimeSpan.Zero;
player.Play();
}
public void PlayLoopingSound(byte[] audioBlob)
{
string tempFile = System.IO.Path.Combine(
System.IO.Path.GetTempPath(),
"Hintergrundmusik.mp3");
player.Volume = 0.1;
File.WriteAllBytes(tempFile, audioBlob);
player.Open(new Uri(tempFile));
player.MediaEnded -= Player_MediaEnded;
player.MediaEnded += Player_MediaEnded;
player.Play();
}
public void PlayLoopingSound_2(byte[] audioBlob)
{
string tempFile = System.IO.Path.Combine(
System.IO.Path.GetTempPath(),
"Hintergrundmusik_Bus.mp3");
player.Volume = 0.1;
File.WriteAllBytes(tempFile, audioBlob);
player.Open(new Uri(tempFile));
player.MediaEnded -= Player_MediaEnded;
player.MediaEnded += Player_MediaEnded;
player.Play();
}
public void Stop()
{
player.Stop();
}
}
}