System.Windows.Controls 名前空 ...


.NET Framework クラス ライブラリ
MediaElement クラス

更新 : 2007 年 11 月

オーディオとビデオまたはそのいずれかを含むコントロールを表します。

名前空間 :  System.Windows.Controls
アセンブリ :  PresentationFramework (PresentationFramework.dll 内)
XAML の XMLNS : http://schemas.microsoft.com/winfx/xaml/presentation

構文

Visual Basic (宣言)
<LocalizabilityAttribute(LocalizationCategory.NeverLocalize)> _
Public Class MediaElement _
    Inherits FrameworkElement _
    Implements IUriContext
Visual Basic (使用法)
Dim instance As MediaElement
C#
[LocalizabilityAttribute(LocalizationCategory.NeverLocalize)]
public class MediaElement : FrameworkElement, 
    IUriContext
Visual C++
[LocalizabilityAttribute(LocalizationCategory::NeverLocalize)]
public ref class MediaElement : public FrameworkElement, 
    IUriContext
J#
/** @attribute LocalizabilityAttribute(LocalizationCategory.NeverLocalize) */
public class MediaElement extends FrameworkElement implements IUriContext
JScript
public class MediaElement extends FrameworkElement implements IUriContext
XAML オブジェクト要素の使用
<MediaElement .../>
解説

アプリケーションでメディアを配布する場合、メディア ファイルをプロジェクト リソースとして使用することはできません。代わりにプロジェクト ファイルでメディアの種類を Content に設定し、CopyToOutputDirectoryPreserveNewest または Always に設定する必要があります。

MediaElement は、コントロールの制御方法に応じて、独立モードまたはクロック モードの 2 つのモードで使用できます。独立モードで使用した場合の MediaElement はイメージに似ており、直接 Source URI を指定できます。クロック モードの MediaElement は、アニメーションのターゲットと考えることができます。したがって、対応する Timeline および Clock のエントリがタイミング ツリーにあります。メディア モードの詳細については、「マルチメディアの概要」を参照してください。

独立モードで MediaElement を制御する例については、「方法 : MediaElement (再生、一時停止、停止、ボリューム、および速度) を制御する」を参照してください。

コントロールの最終的なサイズと位置はメディア コンテンツを使用して決定されるため、MediaOpened イベントが発生するまでは、コントロールの ActualWidthActualHeight は 0 と報告されます。オーディオのみのコンテンツの場合は、これらのプロパティが常に 0 になります。

固定サイズのコントロールの場合は、WidthHeight、またはその両方のプロパティを設定できます。ただし、メディアの縦横比を維持するには、Width または Height のいずれか一方のプロパティのみを設定します。


MediaElement を使用して、メディアの再生を制御する方法を次の例に示します。ここでは、再生、一時停止、停止、メディア内での前後へのスキップ、ボリューム調整、速度比率調整が可能な単純なメディア プレーヤーを作成します。

次のコードでは、UI を作成します。

メモ :

メディアを対話的に停止、一時停止、および再生できるようにするには、MediaElementLoadedBehavior プロパティを Manual に設定する必要があります。

Visual Basic
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="SDKSample.MediaElementExample" >

  <StackPanel Background="Black">

    <!-- To interactively stop, pause, and play the media, the LoadedBehavior 
           property of the MediaElement must be set to "Manual". -->
    <MediaElement Source="media\numbers.wmv" Name="myMediaElement" 
     Width="450" Height="250" LoadedBehavior="Manual" UnloadedBehavior="Stop" Stretch="Fill" 
     MediaOpened="Element_MediaOpened" MediaEnded="Element_MediaEnded"/>

    <StackPanel HorizontalAlignment="Center" Width="450" Orientation="Horizontal">

      <!-- Play button. -->
      <Image Source="images\UI_play.gif" MouseDown="OnMouseDownPlayMedia" Margin="5" />

      <!-- Pause button. -->
      <Image Source="images\UI_pause.gif" MouseDown="OnMouseDownPauseMedia" Margin="5" />

      <!-- Stop button. -->
      <Image Source="images\UI_stop.gif" MouseDown="OnMouseDownStopMedia" Margin="5" />

      <!-- Volume slider. This slider allows a Volume range between 0 and 1. -->
      <TextBlock Foreground="White" VerticalAlignment="Center" Margin="5"  >Volume</TextBlock>
      <Slider Name="volumeSlider" VerticalAlignment="Center" ValueChanged="ChangeMediaVolume" 
       Minimum="0" Maximum="1" Value="0.5" Width="70"/>

      <!-- Volume slider. This slider allows a Volume range between 0 and 10. -->
      <TextBlock Foreground="White" Margin="5"  VerticalAlignment="Center">Speed</TextBlock>
      <Slider Name="speedRatioSlider" VerticalAlignment="Center" ValueChanged="ChangeMediaSpeedRatio" 
       Value="1" Width="70" />

      <!-- Seek to slider. Ths slider allows you to jump to different parts of the media playback. -->
      <TextBlock Foreground="White" Margin="5"  VerticalAlignment="Center">Seek To</TextBlock>
      <Slider Name="timelineSlider" Margin="5" ValueChanged="SeekToMediaPosition" Width="70"/>

    </StackPanel>
  </StackPanel>
</Page>
C#
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="SDKSample.MediaElementExample" >

  <StackPanel Background="Black">

    <!-- To interactively stop, pause, and play the media, the LoadedBehavior 
           property of the MediaElement must be set to "Manual". -->
    <MediaElement Source="media\numbers.wmv" Name="myMediaElement" 
     Width="450" Height="250" LoadedBehavior="Manual" UnloadedBehavior="Stop" Stretch="Fill" 
     MediaOpened="Element_MediaOpened" MediaEnded="Element_MediaEnded"/>

    <StackPanel HorizontalAlignment="Center" Width="450" Orientation="Horizontal">

      <!-- Play button. -->
      <Image Source="images\UI_play.gif" MouseDown="OnMouseDownPlayMedia" Margin="5" />

      <!-- Pause button. -->
      <Image Source="images\UI_pause.gif" MouseDown="OnMouseDownPauseMedia" Margin="5" />

      <!-- Stop button. -->
      <Image Source="images\UI_stop.gif" MouseDown="OnMouseDownStopMedia" Margin="5" />

      <!-- Volume slider. This slider allows a Volume range between 0 and 1. -->
      <TextBlock Foreground="White" VerticalAlignment="Center" Margin="5"  >Volume</TextBlock>
      <Slider Name="volumeSlider" VerticalAlignment="Center" ValueChanged="ChangeMediaVolume" 
       Minimum="0" Maximum="1" Value="0.5" Width="70"/>

      <!-- Volume slider. This slider allows a Volume range between 0 and 10. -->
      <TextBlock Foreground="White" Margin="5"  VerticalAlignment="Center">Speed</TextBlock>
      <Slider Name="speedRatioSlider" VerticalAlignment="Center" ValueChanged="ChangeMediaSpeedRatio" 
       Value="1" Width="70" />

      <!-- Seek to slider. Ths slider allows you to jump to different parts of the media playback. -->
      <TextBlock Foreground="White" Margin="5"  VerticalAlignment="Center">Seek To</TextBlock>
      <Slider Name="timelineSlider" Margin="5" ValueChanged="SeekToMediaPosition" Width="70"/>

    </StackPanel>
  </StackPanel>
</Page>

次のコードでは、サンプル UI コントロールの機能を実装します。PlayPause、および Stop メソッドは、それぞれメディアの再生、一時停止、停止に使用します。MediaElementPosition プロパティを変更すると、メディア内で前後にスキップできます。最後に、Volume プロパティと SpeedRatio プロパティを使用して、ボリュームとメディアの再生速度を調整します。

Visual Basic
Imports System
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Documents
Imports System.Windows.Navigation
Imports System.Windows.Shapes
Imports System.Windows.Data
Imports System.Windows.Media
Imports System.Windows.Input

Namespace SDKSample

    Partial Class MediaElementExample
        Inherits Page

        ' Play the media.
        Sub OnMouseDownPlayMedia(ByVal sender As Object, ByVal args As MouseButtonEventArgs)

            ' The Play method will begin the media if it is not currently active or 
            ' resume media if it is paused. This has no effect if the media is
            ' already running.
            myMediaElement.Play()

            ' Initialize the MediaElement property values.
            InitializePropertyValues()

        End Sub 'OnMouseDownPlayMedia


        ' Pause the media.
        Sub OnMouseDownPauseMedia(ByVal sender As Object, ByVal args As MouseButtonEventArgs)

            ' The Pause method pauses the media if it is currently running.
            ' The Play method can be used to resume.
            myMediaElement.Pause()

        End Sub 'OnMouseDownPauseMedia


        ' Stop the media.
        Sub OnMouseDownStopMedia(ByVal sender As Object, ByVal args As MouseButtonEventArgs)

            ' The Stop method stops and resets the media to be played from
            ' the beginning.
            myMediaElement.Stop()

        End Sub 'OnMouseDownStopMedia


        ' Change the volume of the media.
        Private Sub ChangeMediaVolume(ByVal sender As Object, ByVal args As RoutedPropertyChangedEventArgs(Of Double))
            myMediaElement.Volume = System.Convert.ToDouble(volumeSlider.Value)

        End Sub 'ChangeMediaVolume

        ' Change the speed of the media.
        Private Sub ChangeMediaSpeedRatio(ByVal sender As Object, ByVal args As RoutedPropertyChangedEventArgs(Of Double))
            myMediaElement.SpeedRatio = System.Convert.ToDouble(speedRatioSlider.Value)

        End Sub 'ChangeMediaSpeedRatio

        ' When the media opens, initialize the "Seek To" slider maximum value
        ' to the total number of miliseconds in the length of the media clip.
        Private Sub Element_MediaOpened(ByVal sender As Object, ByVal args As RoutedEventArgs)
            timelineSlider.Maximum = myMediaElement.NaturalDuration.TimeSpan.TotalMilliseconds
        End Sub

        ' When the media playback is finished. Stop() the media to seek to media start.
        Private Sub Element_MediaEnded(ByVal sender As Object, ByVal args As RoutedEventArgs)
            myMediaElement.Stop()
        End Sub

        ' Jump to different parts of the media (seek to). 
        Private Sub SeekToMediaPosition(ByVal sender As Object, ByVal args As RoutedPropertyChangedEventArgs(Of Double))
            Dim SliderValue As Integer = CType(timelineSlider.Value, Integer)

            ' Overloaded constructor takes the arguments days, hours, minutes, seconds, miniseconds.
            ' Create a TimeSpan with miliseconds equal to the slider value.
            Dim ts As New TimeSpan(0, 0, 0, 0, SliderValue)
            myMediaElement.Position = ts
        End Sub

        ' Set the media's starting Volume and SpeedRatio to the current value of the
        ' their respective slider controls.
        Private Sub InitializePropertyValues()
            myMediaElement.Volume = System.Convert.ToDouble(volumeSlider.Value)
            myMediaElement.SpeedRatio = System.Convert.ToDouble(speedRatioSlider.Value)
        End Sub
    End Class 'MediaElementExample
End Namespace 'SDKSample
C#
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Input;

namespace SDKSample
{

   public partial class MediaElementExample : Page
   {

      // Play the media.
      void OnMouseDownPlayMedia(object sender, MouseButtonEventArgs args)
      {

         // The Play method will begin the media if it is not currently active or 
         // resume media if it is paused. This has no effect if the media is
         // already running.
         myMediaElement.Play();

         // Initialize the MediaElement property values.
         InitializePropertyValues();

      }

      // Pause the media.
      void OnMouseDownPauseMedia(object sender, MouseButtonEventArgs args)
      {

         // The Pause method pauses the media if it is currently running.
         // The Play method can be used to resume.
         myMediaElement.Pause();

      }

      // Stop the media.
      void OnMouseDownStopMedia(object sender, MouseButtonEventArgs args)
      {

         // The Stop method stops and resets the media to be played from
         // the beginning.
         myMediaElement.Stop();

      }

      // Change the volume of the media.
      private void ChangeMediaVolume(object sender, RoutedPropertyChangedEventArgs<double> args)
      {
         myMediaElement.Volume = (double)volumeSlider.Value;
      }

      // Change the speed of the media.
      private void ChangeMediaSpeedRatio(object sender, RoutedPropertyChangedEventArgs<double> args)
      {
         myMediaElement.SpeedRatio = (double)speedRatioSlider.Value;
      }

      // When the media opens, initialize the "Seek To" slider maximum value
      // to the total number of miliseconds in the length of the media clip.
      private void Element_MediaOpened(object sender, EventArgs e)
      {
         timelineSlider.Maximum = myMediaElement.NaturalDuration.TimeSpan.TotalMilliseconds;
      }

      // When the media playback is finished. Stop() the media to seek to media start.
      private void Element_MediaEnded(object sender, EventArgs e)
      {
         myMediaElement.Stop();
      }

      // Jump to different parts of the media (seek to). 
      private void SeekToMediaPosition(object sender, RoutedPropertyChangedEventArgs<double> args)
      {
         int SliderValue = (int)timelineSlider.Value;

         // Overloaded constructor takes the arguments days, hours, minutes, seconds, miniseconds.
         // Create a TimeSpan with miliseconds equal to the slider value.
         TimeSpan ts = new TimeSpan(0, 0, 0, 0, SliderValue);
         myMediaElement.Position = ts;
      }

      void InitializePropertyValues()
      {
         // Set the media's starting Volume and SpeedRatio to the current value of the
         // their respective slider controls.
         myMediaElement.Volume = (double)volumeSlider.Value;
         myMediaElement.SpeedRatio = (double)speedRatioSlider.Value;
      }

   }
}

その他のコード

方法 : ユーザー イベントによってメディアの再生をトリガするこの例では、メディアの再生をイベントと同期させる方法を示します。
方法 : メディアの再生を反復するこの例では、メディアを無制限に再生する (無限ループで再生されるようにメディアを設定する) 方法について説明します。
方法 : アニメーションを使用してメディアを再生する次の例では、同じ Storyboard 内の MediaTimeline クラスと DoubleAnimationUsingKeyFrames クラスを使用してメディアとアニメーションを同時に再生する方法を示します。
方法 : ストーリーボードを使用して MediaElement を制御するこの例では、Storyboard 内の MediaTimeline を使用して、MediaElement を制御する方法を示します。
方法 : MediaElement で変換を使用するこの例では、MediaElementRotateTransform を使用する方法を示します。
方法 : ビデオを使用して領域を塗りつぶすこの例では、メディアを使用して領域を塗りつぶす方法を示します。メディアを使用して領域を塗りつぶす 1 つの方法は、MediaElement と共に VisualBrush を使用することです。MediaElement を使用してメディアを読み込んで再生した後、そのメディアを使用して VisualBrushVisual プロパティを設定します。その後は、VisualBrush を使用して、読み込んだメディアを使用して領域を塗りつぶすことができます。
継承階層

System..::.Object
  System.Windows.Threading..::.DispatcherObject
    System.Windows..::.DependencyObject
      System.Windows.Media..::.Visual
        System.Windows..::.UIElement
          System.Windows..::.FrameworkElement
            System.Windows.Controls..::.MediaElement
スレッド セーフ

この型のすべてのパブリック static (Visual Basic では Shared) メンバは、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
プラットフォーム

Windows Vista

.NET Framework および .NET Compact Framework では、各プラットフォームのすべてのバージョンはサポートしていません。サポートされているバージョンについては、「.NET Framework システム要件」を参照してください。

バージョン情報

.NET Framework

サポート対象 : 3.5、3.0
参照

参照

その他の技術情報

タグ :


Page view tracker