1 <UserControl x:Class="SL1.SLMedia" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 mc:Ignorable="d" 7 d:DesignHeight="300" d:DesignWidth="400"> 8 9 <Grid x:Name="LayoutRoot" Background="White"> 10 <MediaElement x:Name="mePalyer" MinWidth="1" Source="vedio/1.mp4" Margin="0,0,0,50" MediaOpened="mePalyer_MediaOpened"></MediaElement> 11 <Button x:Name="btnPlay" Content="播放" VerticalAlignment="Bottom" HorizontalAlignment="Left" Margin="20,20" FontFamily="Arial Black" FontSize="25" Click="btnPlay_Click"></Button> 12 <Button x:Name="btnStop" Content="暂停" VerticalAlignment="Bottom" HorizontalAlignment="Left" Margin="100,20" FontFamily="Arial Black" FontSize="25" Click="btnStop_Click"> 13 </Button> 14 <ProgressBar x:Name="pbTimer" HorizontalAlignment="Left" Height="35" Margin="180,245,0,20" VerticalAlignment="Bottom" Width="210" Minimum="1" Loaded="ProgressBar_Loaded_1" /> 15 16 </Grid> 17 </UserControl>
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Net; 5 using System.Windows; 6 using System.Windows.Controls; 7 using System.Windows.Documents; 8 using System.Windows.Input; 9 using System.Windows.Media; 10 using System.Windows.Media.Animation; 11 using System.Windows.Shapes; 12 using System.Windows.Threading; 13 14 namespace SL1 15 { 16 public partial class SLMedia : UserControl 17 { 18 public SLMedia() 19 { 20 InitializeComponent(); 21 } 22 23 private void btnPlay_Click(object sender, RoutedEventArgs e) 24 { 25 mePalyer.Play(); 26 } 27 28 private void btnStop_Click(object sender, RoutedEventArgs e) 29 { 30 // mePalyer.Stop(); 31 mePalyer.Pause(); 32 } 33 34 private void ProgressBar_Loaded_1(object sender, RoutedEventArgs e) 35 { 36 37 } 38 39 void tick_Tick(object sender, EventArgs e) 40 { 41 // pbTimer.Value += 3; 42 pbTimer.Value= mePalyer.Position.TotalMilliseconds; 43 } 44 45 private void mePalyer_MediaOpened(object sender, RoutedEventArgs e) 46 { 47 pbTimer.Maximum = mePalyer.NaturalDuration.TimeSpan.TotalMilliseconds; 48 DispatcherTimer tick = new DispatcherTimer(); 49 tick.Tick += tick_Tick; 50 tick.Interval = TimeSpan.FromMilliseconds(500); 51 tick.Start(); 52 } 53 54 55 } 56 }