-5

When I use the Play method with the SoundPlayer, my program exits and there is no sound playing. Please help me.

Code:

SoundPlayer player;
player = new SoundPlayer();

private void wii_WiimoteChanged(object s, EventArgs e)
{
  if (wii.WiimoteState.GuitarState.FretButtonState.Green == true)
  {
     Play(1);
  }
}

private void Play(int soundID)
{
  player.SoundLocation = "sounds/" + soundID + ".wav";
  player.Load();
}

private void player_LoadCompleted(object sender, AsyncCompletedEventArgs e)
{
   player.Play();
}

2 Answers2

0

http://msdn.microsoft.com/en-us/library/system.media.soundplayer.aspx

Init player

player = new SoundPlayer();
player.LoadCompleted += new AsyncCompletedEventHandler(player_LoadCompleted);

Load a file

player.SoundLocation = "sounds/" + soundID + ".wav";
player.Load();

On load finished play

private void player_LoadCompleted(object sender, 
AsyncCompletedEventArgs e)
{   
        player.Play();
}
Royi Mindel
  • 1,188
  • 11
  • 34
0

try with full path

player.SoundLocation = Path.GetFullPath("sounds/" + soundID + ".wav");

sample code:

using (SoundPlayer player = new SoundPlayer(Path.GetFullPath("sounds/" + soundID + ".wav")))
{
    player.PlaySync();
}
Damith
  • 59,353
  • 12
  • 95
  • 149