merge31
This commit is contained in:
+15
-6
@@ -4,6 +4,7 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
@@ -13,18 +14,26 @@ namespace Casino
|
||||
{
|
||||
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)
|
||||
public void PlaySound(byte[] audioBlob, int soundId)
|
||||
{
|
||||
string tempFile = System.IO.Path.Combine(
|
||||
System.IO.Path.GetTempPath(),
|
||||
".mp3");
|
||||
if (audioBlob == null || audioBlob.Length == 0)
|
||||
return;
|
||||
|
||||
File.WriteAllBytes(tempFile, audioBlob);
|
||||
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.Open(new Uri(tempFile));
|
||||
shortplayer.Stop();
|
||||
shortplayer.Close(); // gibt die alte Datei frei
|
||||
shortplayer.Open(new Uri(tempFile, UriKind.Absolute));
|
||||
shortplayer.Play();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user