1

我相信我想要做的是异步播放音频。我正在为 Windows 8 编写一个应用程序,它要求每次按下按钮时都播放音频文件。我遇到的问题是,文件会播放,但如果它还没有完成播放并且你按下按钮,它就不会再发声了。

XAML
<MediaElement x:Name="KickBasicAudio"
              Source="Assets/Drums/Dance/KickBasic.wav"
              AutoPlay="False" />
<Button Content="KICK" ClickMode="Press" Click="KickBasicPlay"/>

C#
private void KickBasicPlay(object sender, RoutedEventArgs e)
{
    KickBasicAudio.Play();
}
4

2 回答 2

1

在这种情况下可能SoundPlayer是更好的选择,因为这允许 Asyc 播放

using System.Media; //The reference needed.

private SoundPlayer _player = new SoundPlayer("file");

private void KickBasicPlay(object sender, RoutedEventArgs e)
{
    _player.Play();
}
于 2013-08-16T03:05:26.030 回答
0

我想你想要一个后台工作线程来做这件事。尝试这个。

using System.ComponentModel; //This is the reference needed.

private void KickBasicPlay(object sender, RoutedEventArgs e)
{
    BackgroundWorker thread = new BackgroundWorker();
    thread.DoWork += (sender, e) => { KickBasicAudio.Play() };
}
于 2013-08-16T03:06:40.387 回答