SoundPlayer.PlaySync 方法
定义
播放 .wav 文件,如果尚未加载 .wav 文件,则先加载该文件。Plays the .wav file and loads the .wav file first if it has not been loaded.
public:
void PlaySync();
public void PlaySync ();
member this.PlaySync : unit -> unit
Public Sub PlaySync ()
例外
加载所用的时间超出了 LoadTimeout 指定的时间(以毫秒为单位)。The elapsed time during loading exceeds the time, in milliseconds, specified by LoadTimeout.
找不到 SoundLocation 指定的文件。The file specified by SoundLocation cannot be found.
.wav 标头已损坏;由 SoundLocation 指定的文件不是 PCM .wav 文件。The .wav header is corrupted; the file specified by SoundLocation is not a PCM .wav file.
示例
下面的代码示例演示 PlaySync 如何使用方法同步播放 .wav 文件。The following code example demonstrates the use of the PlaySync method to synchronously play a .wav file.
private:
SoundPlayer^ Player;
void loadSoundAsync()
{
// Note: You may need to change the location specified based on
// the location of the sound to be played.
this->Player->SoundLocation = "http://www.tailspintoys.com/sounds/stop.wav";
this->Player->LoadAsync();
}
void Player_LoadCompleted( Object^ /*sender*/, System::ComponentModel::AsyncCompletedEventArgs^ /*e*/ )
{
if (this->Player->IsLoadCompleted == true)
{
this->Player->PlaySync();
}
}
private SoundPlayer Player = new SoundPlayer();
private void loadSoundAsync()
{
// Note: You may need to change the location specified based on
// the location of the sound to be played.
this.Player.SoundLocation = "http://www.tailspintoys.com/sounds/stop.wav";
this.Player.LoadAsync();
}
private void Player_LoadCompleted (
object sender,
System.ComponentModel.AsyncCompletedEventArgs e)
{
if (this.Player.IsLoadCompleted)
{
this.Player.PlaySync();
}
}
Private WithEvents Player As New SoundPlayer
Sub LoadSoundAsync()
' Note: You may need to change the location specified based on
' the location of the sound to be played.
Me.Player.SoundLocation = "http://www.tailspintoys.com/sounds/stop.wav"
Me.Player.LoadAsync ()
End Sub
Private Sub PlayWhenLoaded(ByVal sender As Object, ByVal e As _
System.ComponentModel.AsyncCompletedEventArgs) Handles _
Player.LoadCompleted
If Me.Player.IsLoadCompleted = True Then
Me.Player.PlaySync()
End If
End Sub
注解
PlaySync方法使用当前线程来播放 .wav 文件,使线程无法处理其他消息,直到加载完成为止。The PlaySync method uses the current thread to play a .wav file, preventing the thread from handling other messages until the load is complete. 您可以使用 LoadAsync 或 Load 方法来提前将 .wav 文件加载到内存中。You can use the LoadAsync or Load method to load the .wav file to memory in advance. 从或 URL 成功加载 .wav 文件后,对 Stream 的播放方法的后续调用 SoundPlayer 将无需重新加载 .wav 文件,直至声音的路径更改。After a .wav file is successfully loaded from a Stream or URL, future calls to playback methods for the SoundPlayer will not need to reload the .wav file until the path for the sound changes.
如果未指定 .wav 文件或未能加载 .wav 文件,则该 PlaySync 方法将播放默认的嘟嘟声。If the .wav file has not been specified or it fails to load, the PlaySync method will play the default beep sound.