SoundPlayer.PlaySync 方法

定义

播放 .wav 文件,如果尚未加载 .wav 文件,则先加载该文件。

public:
 void PlaySync();
public void PlaySync ();
member this.PlaySync : unit -> unit
Public Sub PlaySync ()

例外

加载所用的时间超出了 LoadTimeout 指定的时间(以毫秒为单位)。

找不到 SoundLocation 指定的文件。

.wav 标头已损坏;由 SoundLocation 指定的文件不是 PCM .wav 文件。

示例

下面的代码示例演示如何使用 PlaySync 方法同步播放.wav文件。

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文件,防止线程在加载完成之前处理其他消息。 可以使用 LoadAsyncLoad 方法提前将 .wav 文件加载到内存中。 从 Stream 或 URL 成功加载.wav文件后,对 的播放方法 SoundPlayer 的将来调用将不需要重新加载.wav文件,直到声音的路径更改。

如果.wav文件尚未指定或无法加载,该方法 PlaySync 将播放默认的蜂鸣声。

适用于

另请参阅