133 lines
3.5 KiB
C#
133 lines
3.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 List<MediaPlayer> activePlayers = new();
|
|
|
|
public Audioplayer() { shortplayer.MediaEnded += Player_MediaEnded; }
|
|
|
|
public void PlaySound(byte[] audioBlob, int soundId)
|
|
{
|
|
if (audioBlob == null || audioBlob.Length == 0)
|
|
return;
|
|
|
|
string tempFile = System.IO.Path.Combine(
|
|
System.IO.Path.GetTempPath(),
|
|
"casino_" + Guid.NewGuid().ToString("N") + ".mp3");
|
|
|
|
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)
|
|
{
|
|
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();
|
|
}
|
|
}
|
|
}
|