1

我是 WPF 的完整初学者,并且有一个使用 StoryBoard 播放声音的应用程序。

public void PlaySound()
{
 MediaElement m = (MediaElement)audio.FindName("MySound.wma");
 m.IsMuted = false;
 FrameworkElement audioKey = (FrameworkElement)keys.FindName("MySound");
 Storyboard s = (Storyboard)audioKey.FindResource("MySound.wma");
 s.Begin(audioKey);
}

<Storyboard x:Key="MySound.wma">
 <MediaTimeline d:DesignTimeNaturalDuration="1.615" BeginTime="00:00:00" Storyboard.TargetName="MySound.wma" Source="Audio\MySound.wma"/>
</Storyboard>

我有一个可怕的延迟,有时需要 10 秒才能播放声音。我怀疑这与以下事实有关:无论我等待多长时间 - 直到我离开该功能后才会播放声音。我不明白。我打电话给 Begin,但没有任何反应。有没有办法用可以立即播放且没有延迟的东西替换此方法或 StoryBoard 对象?

4

1 回答 1

1

我将代码更改为-

<Button Cursor="Hand" HorizontalAlignment="Left" Margin="70,0,0,0" x:Name="MyButton"  Width="286" Content="Hi!" Focusable="False" IsTabStop="False">
        <Button.Triggers>
            <EventTrigger RoutedEvent="Button.PreviewMouseLeftButtonDown">
                <EventTrigger.Actions>
                    <BeginStoryboard>
                        <Storyboard SlipBehavior="Slip" BeginTime="00:00:00">
                            <MediaTimeline Storyboard.TargetName="MySound_wma" Source="MySound.wma"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>
    </Button.Triggers>
</Button>

现在看来工作正常,谢谢xD

于 2010-06-18T22:17:44.147 回答