SoundPlayer.PlaySync 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
播放 .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檔案,防止線程處理其他訊息,直到載入完成為止。 您可以使用 LoadAsync 或 Load 方法來事先將.wav檔案載入記憶體。 從 Stream 或 URL 成功載入.wav檔案之後,未來對 的播放方法 SoundPlayer 呼叫就不需要重載.wav檔案,直到音效的路徑變更為止。
如果尚未指定.wav檔案,或無法載入, PlaySync 方法將會播放預設的嗶聲音效。