在SoundPlayer中使用Play方法时存在程序

时间:2013-08-25 07:55:22

标签: c# soundplayer

当我使用SoundPlayer的Play方法时,我的程序退出并且没有声音播放。请帮帮我。

代码:

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 个答案:

答案 0 :(得分:0)

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

初始玩家

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

加载文件

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

加载完成播放

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

答案 1 :(得分:0)

尝试完整路径

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

示例代码:

using (SoundPlayer player = new SoundPlayer(Path.GetFullPath("sounds/" + soundID + ".wav")))
{
    player.PlaySync();
}