標籤:
在WPF 中可以使用MediaElement 為應用程式添加媒體播放控制項,以完成播放音頻、視頻功能。由於MediaElement 屬於UIElement,所以它同時也支援滑鼠及鍵盤的操作。本篇將使用MediaElement 類和Windows API Code Pack 建立一個簡單的視頻播放器實現一些準系統。
一、後台邏輯代碼說明
1.選擇視頻檔案
在視頻檔案瀏覽部分引入,並添加WMV、AVI 等檔案過濾器。
點擊詳細參考
//選擇視頻檔案private void openBtn_Click(object sender, RoutedEventArgs e){ CommonOpenFileDialog open = new CommonOpenFileDialog(); open.EnsureReadOnly = true; open.Filters.Add(new CommonFileDialogFilter("Mp4檔案", "*.mp4")); open.Filters.Add(new CommonFileDialogFilter("Wmv檔案", "*.wmv")); open.Filters.Add(new CommonFileDialogFilter("Avi檔案", "*.avi")); open.Filters.Add(new CommonFileDialogFilter("Mp3檔案", "*.mp3")); if (open.ShowDialog() == CommonFileDialogResult.Ok) { //指定媒體檔案地址 mediaElement.Source = new Uri(open.FileName, UriKind.Relative); playBtn.IsEnabled = true; }}
2.可以使用play()/Pause()方法,控制視頻播放或暫停
//開始播放private void playBtn_Click(object sender, RoutedEventArgs e){ mediaElement.Play(); mediaElement.ToolTip = "開始播放";}//停止播放private void stopBtn_Click(object sender, RoutedEventArgs e){ mediaElement.Pause(); mediaElement.ToolTip = "停止播放";}
3.通過修改MediaElement 的Position 值實現視頻進度調整操作。時間間隔通過TimeSpan 進行設定(下面代碼以10秒為間隔)。
//後退private void backBtn_Click(object sender, RoutedEventArgs e){ mediaElement.Position = mediaElement.Position - TimeSpan.FromSeconds(10);}//前進private void forwardBtn_Click(object sender, RoutedEventArgs e){ mediaElement.Position = mediaElement.Position +TimeSpan.FromSeconds(10);}
4. 音量大小部分只需將Slider Value 變化值與MediaElement Volume 值做一個簡單Binding 即可。
<MediaElement Height="300" Width="450" Name="mediaElement" Volume="{Binding ElementName=volumeSlider,Path=Value}" LoadedBehavior="Manual" />
二、前台Xaml代碼定義
<StackPanel HorizontalAlignment="Center" Margin="20"> <Border BorderThickness="3" Background="Black"> <Border.BorderBrush> <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1"> <GradientStop Offset="0" Color="White"/> <GradientStop Offset="0.5" Color="Gold"/> </LinearGradientBrush> </Border.BorderBrush><MediaElement Height="300" Width="450" Name="mediaElement" Volume="{Binding ElementName=volumeSlider,Path=Value}" LoadedBehavior="Manual" /> </Border> <StackPanel Orientation="Horizontal" Height="40" HorizontalAlignment="Center"> <Button x:Name="openBtn" Content="Open File" Style="{StaticResource btnStyle}" Click="openBtn_Click" Margin="0" Width="64"/> <Button x:Name="playBtn" Content="Play" Style="{StaticResource btnStyle}" Click="playBtn_Click"/> <Button x:Name="stopBtn" Content="Stop" Style="{StaticResource btnStyle}" Click="stopBtn_Click"/> <Button x:Name="backBtn" Content="Back" Style="{StaticResource btnStyle}" Click="backBtn_Click"/> <Button x:Name="forwardBtn" Content="Forward" Style="{StaticResource btnStyle}" Click="forwardBtn_Click" Height="30" Margin="5,5,5,0" VerticalAlignment="Top"/> </StackPanel> <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="5"> <TextBlock Text="Volume" Foreground="Gold"/> <Slider x:Name="volumeSlider" Minimum="0" Maximum="1" Value="0.5" Width="200"/> </StackPanel></StackPanel>
樣式資源定義
<Window.Resources> <Style x:Key="btnStyle" TargetType="Button"> <Setter Property="Background"> <Setter.Value> <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1"> <GradientStop Offset="0" Color="White"/> <GradientStop Offset="0.5" Color="#FF554D4A"/> </LinearGradientBrush> </Setter.Value> </Setter> <Setter Property="FontStyle" Value="Italic"/> <Setter Property="Margin" Value="5"/> <Setter Property="Width" Value="60"/> <Setter Property="Foreground" Value="Gold"/> <Style.Triggers> <Trigger Property="Button.IsMouseOver" Value="True"> <Setter Property="Foreground" Value="Black"/> </Trigger> </Style.Triggers> </Style></Window.Resources>
運行結果:
參考文章來自:http://www.cnblogs.com/gnielee/archive/2010/05/06/wpf4-media-player-mediaelement.html
WPF 媒體播放器(MediaElement)使用執行個體(轉)