如何:使用用户事件来触发媒体播放功能
此示例演示如何使媒体播放与事件同步。
示例
下面的示例使用 MediaElement 控件和 MediaTimeline 类播放用户单击 Button 时发出的声音。
<Page xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" >
<StackPanel>
<!-- The MediaElement control plays the sound. -->
<MediaElement Name="myMediaElement" />
<Button>Click to Hear a Sound!
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<!-- Sound wave from this source is played when the button is clicked.-->
<MediaTimeline Source="C:\WINDOWS\Media\ringin.wav" Storyboard.TargetName="myMediaElement" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Button.Triggers>
</Button>
</StackPanel>
</Page>
<Page xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" >
<StackPanel>
<!-- The MediaElement control plays the sound. -->
<MediaElement Name="myMediaElement" />
<Button>Click to Hear a Sound!
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<!-- Sound wave from this source is played when the button is clicked.-->
<MediaTimeline Source="C:\WINDOWS\Media\ringin.wav" Storyboard.TargetName="myMediaElement" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Button.Triggers>
</Button>
</StackPanel>
</Page>