-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSoundMananger.cs
91 lines (78 loc) · 2.39 KB
/
SoundMananger.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Media;
using System.Diagnostics;
using System.Numerics;
using NAudio.Wave;
using System.Reflection;
using System.IO;
using Capital_and_Cargo.Properties;
namespace Capital_and_Cargo
{
internal class SoundMananger
{
//private static string audioFilePath = "\\Capital-and-Cargo\\sound\\music\\gameMusic.wav";
// Create a new SoundPlayer instance with the path to the .wav audio file
private SoundPlayer player;
public bool playsound = true;
public bool playmusic = true;
public SoundMananger()
{
this.player = new SoundPlayer(Properties.Resources.gameMusic);
}
public void updateMusic()
{
if (playmusic)
{ playMusic(); }
else
{ stopMusic(); }
}
public void playMusic()
{
using (SoundPlayer SoundPlayer = new SoundPlayer(Properties.Resources.gameMusic))
{
SoundPlayer.PlayLooping();
}
}
public void stopMusic()
{
player.Stop();
}
public void playSound(System.IO.UnmanagedMemoryStream soundData)
{
if (playsound)
{
Task.Run(() => soundThread(soundData));
}
}
private void soundThread(System.IO.UnmanagedMemoryStream soundData)
{
WaveOutEvent buttonSound = new WaveOutEvent();
WaveFileReader audioReader;
using (Stream stream = soundData)
{
if (stream != null)
{
using(audioReader = new WaveFileReader(stream))
{
try
{
buttonSound.Init(audioReader);
buttonSound.Play();
while (buttonSound.PlaybackState == PlaybackState.Playing)
{
Thread.Sleep(50);
}
}
catch (Exception ex ){
Debug.Write(ex);
}
}
}
}
}
}
}