Cześć. Napisałem sobie metodę, która z pomocą SoundPlayer odtwarza mi dźwięki:
public bool[] error_do_not_play_sound_anymore = new bool[10];
// [...]
public void PlayRequestedSoundEffect(string SelectedSoundEffect)
{
// wykorzystanie: PlayRequestedSoundEffect("TorpedoExplosion");
// Domyślnie error_do_not_play_sound_anymore[x] = false, jeśli raz nie uda się odtworzyć danego dźwięku,
// to zmienia się na true i więcej tego już nie próbuje
if (error_do_not_play_sound_anymore[0] == false && SelectedSoundEffect == "MessageLog")
{
try
{
SoundPlayer simpleSound0 = new SoundPlayer(@"sounds\messagelogsound.wav");
simpleSound0.Play();
}
catch
{
MessageBox.Show("Error:\n\nfile not found: sounds\\messagelogsounds.wav", "No file found", MessageBoxButtons.OK, MessageBoxIcon.Warning);
error_do_not_play_sound_anymore[0] = true;
}
}
// TorpedoExplosion - play a sound when torpedo hits a target
if (error_do_not_play_sound_anymore[1] == false && SelectedSoundEffect == "TorpedoExplosion")
{
try
{
SoundPlayer simpleSound1 = new SoundPlayer(@"sounds\torpedo_explosion.wav");
simpleSound1.Play();
}
catch
{
MessageBox.Show("Error:\n\nfile not found: sounds\\torpedo_explosion.wav", "No file found", MessageBoxButtons.OK, MessageBoxIcon.Warning);
error_do_not_play_sound_anymore[1] = true;
}
}
// TorpedoFire - play a sound when torpedo is launched from the submarine
if (error_do_not_play_sound_anymore[2] == false && SelectedSoundEffect == "TorpedoFire")
{
try
{
SoundPlayer simpleSound2 = new SoundPlayer(@"sounds\torpedo_fire.wav");
simpleSound2.Play();
}
catch
{
MessageBox.Show("Error:\n\nfile not found: sounds\\torpedo_fire.wav", "No file found", MessageBoxButtons.OK, MessageBoxIcon.Warning);
error_do_not_play_sound_anymore[2] = true;
}
}
// RadarPing - play a sound when enemy contact is detected
if (error_do_not_play_sound_anymore[3] == false && SelectedSoundEffect == "RadarPing")
{
try
{
SoundPlayer simpleSound3 = new SoundPlayer(@"sounds\radar_ping.wav");
simpleSound3.Play();
}
catch
{
MessageBox.Show("Error:\n\nfile not found: sounds\\radar_ping.wav", "No file found", MessageBoxButtons.OK, MessageBoxIcon.Warning);
error_do_not_play_sound_anymore[3] = true;
}
}
// SonarSweepQuiet
if (error_do_not_play_sound_anymore[4] == false && SelectedSoundEffect == "SonarSweepQuiet")
{
try
{
SoundPlayer simpleSound4 = new SoundPlayer(@"sounds\sonar_sweep_quiet.wav");
simpleSound4.Play();
}
catch
{
MessageBox.Show("Error:\n\nfile not found: sounds\\sonar_sweep_quiet.wav", "No file found", MessageBoxButtons.OK, MessageBoxIcon.Warning);
error_do_not_play_sound_anymore[4] = true;
}
}
// SonarSweepQuiet
if (error_do_not_play_sound_anymore[5] == false && SelectedSoundEffect == "SonarSweepNormal")
{
try
{
SoundPlayer simpleSound5 = new SoundPlayer(@"sounds\sonar_sweep.wav");
simpleSound5.Play();
}
catch
{
MessageBox.Show("Error:\n\nfile not found: sounds\\sonar_sweep.wav", "No file found", MessageBoxButtons.OK, MessageBoxIcon.Warning);
error_do_not_play_sound_anymore[5] = true;
}
}
}
Problem pojawia się, gdy zorientowałem się przeglądając stackoverflow, że SoundPlayer może odtwarzać tylko jeden plik jednocześnie. Niestety, nie znalazłem kodu, który umiałbym wykorzystać, aby można było odtworzyć dwa różne dźwięki na raz (lub ten sam dźwięk więcej niż jeden raz), np poprzez szybkie wciśnięcie jednego buttona (odtwarzającego jeden dźwięk) i szybko po nim wciskając inny (odtwarzając inny dźwięk).
Czy ktoś by mógł KLAROWNIE wytłumaczyć mi, jak mogę to zrobić (lub wklepać kod)?
Próbowałem wykorzystać WindowsMediaPlayer, ale ma tyle funkcji, że i tak umiem odtworzyć dźwięk tylko raz.
Szukałem na google i na yt, ale chyba za mało, bo odpowiedzi jeszcze nie znalazłem.
Z góry dziękuję