次の方法で共有


方法 : ユーザー イベントによってメディアの再生をトリガーする

この例では、メディアの再生をイベントと同期させる方法を示します。

使用例

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>

参照

参照

MediaElement

MediaTimeline

RoutedEvent

Storyboard

概念

グラフィックスとマルチメディア

その他の技術情報

オーディオとビデオに関する「方法」トピック