Multimedia in WPF
Multimedia:
Wpf supports the video and audio using mediaelement. This will enable the developer to integrate audio and video into the application.
WPF provides two classes to support audio and video content:
1. MediaElement - content control - XAML
2. MediaPlayer - Drawing object – only from code behind.
Namespace: System.Windows.Media
These classed are controlled from UI. These classes will internally use Windows Media Player 10 control for media playback.
MediaElement andMediaPlayer support two different modes:
1. Independent mode
2. Clock mode.
Independent Mode:
In this mode, the media play back is driven by the media content. This provides the option of media uri, playback options and position-speed ratio of media.
Loading of media is done through object's Source property or using Open method of MediaPlayer object's Open method.
Available media object controls: Play, Pause, Close, and Stop.
Clock Mode:
In this mode, media playback is derived from MediaTimeline. This provide the option of Media's Uri using MediaTimeline,playback using clock.
Loading of media is done through object's Source property of MediaTimeline. Creating the clock from the timeline, and attaching the clock to the media object when used in story board.
media playback clock is controlled by using ClockController control methods in clock mode.
MediaElement:
LoadedBehavior and UnloadedBehavior properties control the behavior of the MediaElement when IsLoaded is true or false.
<MediaElement Source="Media/win2wpf.wmv" StretchDirection="DownOnly"
LoadedBehavior="Manual" Name="mymedia" Visibility="Collapsed"/>
Sample code:
<StackPanel Margin="10" Orientation="Vertical">
<MediaElement Name="media1" Source= "Public\win2wpf.wmv"
LoadedBehavior="Manual" UnloadedBehavior="Stop" Stretch="Fill"
MediaOpened="mediaOpened" MediaEnded="mediaEnded" Width="250" Height="150" />
<StackPanel HorizontalAlignment="Center" Orientation="Horizontal">
<!-- Play button. -->
<Button Width="50" Height="20" Name="play" Content="Play"
Click="OnMouseClickPlayMedia" Margin="5" />
<!-- Pause button. -->
<Button Width="50" Height="20" Name="Pause" Content="Pause"
Click="OnMouseClickPauseMedia" Margin="5" />
<!-- Stop button. -->
<Button Width="50" Height="20" Name="Stop" Content="Stop"
Click="OnMouseClickStopMedia" Margin="5" />
</StackPanel>
</StackPanel>
From Code Behind:
// Play
void OnMouseClickPlayMedia(object sender, RoutedEventArgs args)
{
media1.Play();
}
// Pause
void OnMouseClickPauseMedia(object sender, RoutedEventArgs args)
{
media1.Pause();
}
// Stop
void OnMouseClickStopMedia(object sender, RoutedEventArgs args)
{
media1.Stop();
}
private void mediaOpened(object sender, EventArgs e)
{
// donothing
}
private void mediaEnded(object sender, EventArgs e)
{
media1.Stop();
}
MediaPlayer:
Two ways to control media playback:
1. With out setting clock – independent mode
2. With Clock - Clock Mode
MediaPlayer player1 = new MediaPlayer();
Player1.Open(new Uri(@"Media/win2wpf.wmv", UriKind.Relative));
VideoDrawing videoDrawing1 = new VideoDrawing();
videoDrawing1.Rect = new Rect(0, 0, 200, 200);
videoDrawing1.Player = player1;
player1.Play();