-
Notifications
You must be signed in to change notification settings - Fork 0
/
MP3Player.cs
57 lines (52 loc) · 1.69 KB
/
MP3Player.cs
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
using NAudio.Wave.SampleProviders;
using NAudio.Wave;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace pakar_alert_overlay
{
public class MP3Player
{
public static async Task PlayMP3FromResourceAsync(byte[] mp3Data)
{
if (mp3Data != null && mp3Data.Length > 0)
{
using (var memoryStream = new MemoryStream(mp3Data))
using (var reader = new Mp3FileReader(memoryStream))
using (var waveOut = new WaveOutEvent())
{
// Set the playback volume (optional)
waveOut.Volume = 1.0f; // You can adjust the volume as needed (0.0 to 1.0).
// Set the output device and start asynchronous playback
waveOut.Init(reader);
waveOut.Play();
// Wait for playback to complete asynchronously
while (waveOut.PlaybackState == PlaybackState.Playing)
{
await Task.Delay(100);
}
}
}
else
{
Console.WriteLine("Resource data is empty.");
}
}
public static async Task SoundAlarm()
{
byte[] mp3Data = Properties.Resources.alarm;
if (mp3Data != null && mp3Data.Length > 0)
{
await PlayMP3FromResourceAsync(mp3Data);
}
else
{
Console.WriteLine("MP3 resource not found or is empty.");
}
}
}
}