为WPF Surface应用程序添加声音效果

时间:2015-08-03 06:24:45

标签: c# wpf audio pixelsense

我试图在我的WPF Surface应用程序中触摸/移动对象时播放声音,以描述它已被选中或其移动。这是我尝试过但它似乎不起作用。

SoundPlayerAction soundPlayerAction = new SoundPlayerAction();
soundPlayerAction.Source = new Uri(@"C:\Resources\Selection.wav", UriKind.RelativeOrAbsolute);
EventTrigger eventTrigger = new EventTrigger(TouchEnterEvent); // this is the event you want to trigger the sound effect.
eventTrigger.Actions.Add(soundPlayerAction);

非常感谢任何反馈或想法。

3 个答案:

答案 0 :(得分:0)

在项目中创建一个新文件夹,并将其重命名为SOUNDS,然后将声音文件插入其中并尝试:

  SoundPlayerAction soundPlayerAction = new SoundPlayerAction();
    soundPlayerAction.Source = new Uri(@"SOUNDS\Selection.wav", UriKind.RelativeOrAbsolute);


    EventTrigger eventTrigger = new EventTrigger(TouchEnterEvent); // this is the event you want to trigger the sound effect.

    eventTrigger.Actions.Add(soundPlayerAction);
     Triggers.Add(eventTrigger); // Add this event trigger to Window.Triggers collection.

答案 1 :(得分:0)

C:\Resources\Selection.wav显然不是声音文件的正确路径。

你应该创建一个名为" Resources"的文件夹。在Visual Studio项目中,并将声音文件添加到该文件夹​​。然后转到声音文件的属性并将其Build Action设置为Resource,并将Copy to Output Directory设置为Copy awaysCopy if newer

现在您可以通过相对Uri访问该文件,如下所示:

soundPlayerAction.Source = new Uri("Resources/Selection.wav", UriKind.Relative);

答案 2 :(得分:0)

我遇到了这个链接https://www.youtube.com/watch?v=nF-HYoTurc8并且稍微改变了我的代码并且声音有效:D

在我的PreviewTouchDown方法中,我添加了:

SoundPlayer soundPlayerAction = new SoundPlayer(TangramsTool.Properties.Resources.Selection);
soundPlayerAction.Play();