2013-07-19 52 views
0

我正在製作一個使用windows phone sdk 7.1的簡單應用程序,每當用戶點擊畫布時,我都需要播放短暫的聲音效果。我如何在畫布或任何其他控件的Tap事件中添加這個未來?通過點擊畫布在windows phone 7.1中播放音效

我所知道的是Uri到項目文件夾中的文件:

"/TestApp;component/Resources/Untitled.wma" 

回答

-1

那麼我想出了正確的方法:

private void PlayDuuuu() 
{ 
    StreamResourceInfo stream = Application.GetResourceStream(new Uri("/AppName;component/Untitled.wav", UriKind.Relative)); 
    SoundEffect soundeffect = SoundEffect.FromStream(stream.Stream); 
    SoundEffectInstance soundInstance = soundeffect.CreateInstance(); 
    FrameworkDispatcher.Update(); 
    soundInstance.Play(); 
} 

而且我發現聲音文件無法在WMA格式...

2

最簡單的方法是從PhoneyTools使用SoundEffectPlayer。聲明像這樣在你的類

SoundEffectPlayer _player = null; 

var resource = Application.GetResourceStream(new Uri("/TestApp;component/Resources/Untitled.wma", UriKind.Relative)); 
var effect = SoundEffect.FromStream(resource.Stream); 
_player = new SoundEffectPlayer(effect); 

然後初始化它只需撥打

_player.Play(); 
+0

你能告訴我什麼使用聲明我應該聲明'SoundEffect'? –

+0

@ Saeid87我更新了我的答案 –

0

你也可以添加一個媒體元素的控制,設置它的源屬性爲您文件,只需在tap事件處理程序中調用player.Play()

1

我發現下面的解決方案是最好的,以播放聲音通知。它也播放.WMA文件。

public static void PlayMessageFailedSound() 
    { 
     var s = Song.FromUri("MessageFailed", new Uri(@"Resources/Alert_nudge.wma", UriKind.Relative)); 
     FrameworkDispatcher.Update(); 
     MediaPlayer.Play(s); 
    }