.net SoundPlayer事件从未触发

本文关键字:SoundPlayer 事件 net | 更新日期: 2023-09-27 18:01:33

所以我试图使用soundplayer来加载wav文件,然后在加载完成后触发一个事件。原因是我不想让我的程序继续运行,直到WAV完全加载并准备好(如果有其他方法可以做到这一点,请告诉我)。

但不幸的是LoadCompleted事件从未触发!

这是我最后一次尝试。我做错了什么?

this.audio = new SoundPlayer(); 
audio.SoundLocation = "songs''" + songname + ".wav"; 
audio.LoadAsync();
属于songdata对象的

。然后是main类main方法

this.songdata.audio.LoadCompleted += new AsyncCompletedEventHandler(audio_LoadCompleted);
this.songdata.audio.Play();

最后在主类中:

void audio_LoadCompleted(object sender, AsyncCompletedEventArgs e)
{
    MessageBox.Show("aaa");
}

文件开始播放,但没有出现消息框。顺便说一句,我使用的是Visual Studio 2010和。net 4.0。

.net SoundPlayer事件从未触发

我得到了以下代码发送给我一个LoadCompleted事件,然而,我在SoundPlayer对象的同一个类中有"事件回调函数"。

class MediaPlayer{
System.Media.SoundPlayer soundPlayer;
public MediaPlayer(MemoryStream stream){
   soundPlayer = new System.Media.SoundPlayer(stream);
   soundPlayer.LoadCompleted += new AsyncCompletedEventHandler(player_LoadCompleted);
   soundPlayer.Load();
}
public void Play(){
   soundPlayer.Play();
}
// Handler for the LoadCompleted event.
private void player_LoadCompleted(object sender, AsyncCompletedEventArgs e){   
   Console.WriteLine("LoadCompleted"); 
} 
}