-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoundPlayer.cs
More file actions
70 lines (59 loc) · 2.07 KB
/
Copy pathSoundPlayer.cs
File metadata and controls
70 lines (59 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System;
using System.IO;
using NAudio.Wave;
namespace MatheMann;
// Plays sounds/open.mp3 (shipped next to the DLL). Fire-and-forget; each call gets
// its own reader/output, disposed when playback ends.
public static class SoundPlayer
{
private static string? mp3Path;
private static bool pathResolved;
public static void Play(float volume = 1.0f)
{
try
{
var path = GetMp3Path();
Plugin.Log.Debug($"[MatheMann] Attempting to play sound: {path}");
if (path is null || !File.Exists(path))
{
Plugin.Log.Warning($"[MatheMann] Sound file NOT FOUND at: {path}");
return;
}
var reader = new AudioFileReader(path) { Volume = Math.Clamp(volume, 0f, 1f) };
var output = new WaveOutEvent();
output.PlaybackStopped += (_, args) =>
{
if (args.Exception is not null)
Plugin.Log.Warning($"[MatheMann] Playback error: {args.Exception.Message}");
output.Dispose();
reader.Dispose();
};
output.Init(reader);
output.Play();
Plugin.Log.Debug("[MatheMann] Playback started.");
}
catch (Exception ex)
{
Plugin.Log.Error(ex, "[MatheMann] Could not play sound (exception).");
}
}
// Use AssemblyLocation, not Assembly.Location (can be empty when loaded from bytes).
private static string? GetMp3Path()
{
if (pathResolved) return mp3Path;
pathResolved = true;
try
{
var asmPath = Plugin.PluginInterface.AssemblyLocation.FullName;
var dir = Path.GetDirectoryName(asmPath);
if (dir is not null)
mp3Path = Path.Combine(dir, "sounds", "open.mp3");
Plugin.Log.Debug($"[MatheMann] Resolved sound path: {mp3Path}");
}
catch (Exception ex)
{
Plugin.Log.Error(ex, "[MatheMann] Failed to resolve mp3 path.");
}
return mp3Path;
}
}