你的位置:首页 > ASP.net教程

[ASP.net教程]Wfp MediaElement 和Slider音频播放器


 1 <Style x:Key="btnFallback" TargetType="{x:Type Button}"> 2       <Setter Property="Template"> 3         <Setter.Value> 4           <ControlTemplate TargetType="{x:Type Button}"> 5             <Border x:Name="bd" Height="50" Width="50" Background="Transparent" CornerRadius="36" BorderBrush="Gray" BorderThickness="2"> 6               <Path x:Name="p1" Data="M576 160v320l320-320v704l-320-320v320l-352-352z" HorizontalAlignment="Center" VerticalAlignment="Center" Canvas.Top="10" Fill="Gray" Height="30" Stretch="Fill" Width="30" Canvas.Left="0" Margin="0 0 5 0"/> 7             </Border> 8             <ControlTemplate.Triggers> 9               <Trigger Property="IsMouseOver" Value="True"> 10                 <Setter Property="BorderBrush" TargetName="bd" Value="white"/> 11                 <Setter Property="Fill" TargetName="p1" Value="white"/> 12               </Trigger> 13             </ControlTemplate.Triggers> 14           </ControlTemplate> 15         </Setter.Value> 16       </Setter> 17     </Style> 18     <Style x:Key="btnPlay" TargetType="{x:Type Button}"> 19       <Setter Property="Template"> 20         <Setter.Value> 21           <ControlTemplate TargetType="{x:Type Button}"> 22             <Border x:Name="bd" Height="60" Width="60" Background="Transparent" CornerRadius="36" BorderBrush="Gray" BorderThickness="2"> 23               <Path x:Name="p1" Data="M790.857 529.714l-758.857 421.714q-13.143 7.429-22.571 1.714t-9.429-20.571v-841.143q0-14.857 9.429-20.571t22.571 1.714l758.857 421.714q13.143 7.429 13.143 17.714t-13.143 17.714z"  24                  HorizontalAlignment="Center" VerticalAlignment="Center" Canvas.Top="10" Fill="Gray" Height="30" Stretch="Fill" Width="30" Canvas.Left="0" Margin="10 0 5 0"/> 25             </Border> 26             <ControlTemplate.Triggers> 27               <Trigger Property="IsMouseOver" Value="True"> 28                 <Setter Property="BorderBrush" TargetName="bd" Value="white"/> 29                 <Setter Property="Fill" TargetName="p1" Value="white"/> 30               </Trigger> 31             </ControlTemplate.Triggers> 32           </ControlTemplate> 33         </Setter.Value> 34       </Setter> 35     </Style> 36     <Style x:Key="btnStop" TargetType="{x:Type Button}"> 37       <Setter Property="Template"> 38         <Setter.Value> 39           <ControlTemplate TargetType="{x:Type Button}"> 40               <Border x:Name="bd" Height="60" Width="60" Background="Transparent" CornerRadius="36" BorderBrush="Gray" BorderThickness="2"> 41                 <Path x:Name="p1" Data="M877.714 109.714v804.571q0 14.857-10.857 25.714t-25.714 10.857h-292.571q-14.857 0-25.714-10.857t-10.857-25.714v-804.571q0-14.857 10.857-25.714t25.714-10.857h292.571q14.857 0 25.714 10.857t10.857 25.714zM365.714 109.714v804.571q0 14.857-10.857 25.714t-25.714 10.857h-292.571q-14.857 0-25.714-10.857t-10.857-25.714v-804.571q0-14.857 10.857-25.714t25.714-10.857h292.571q14.857 0 25.714 10.857t10.857 25.714z"  42                  HorizontalAlignment="Center" VerticalAlignment="Center" Canvas.Top="10" Fill="Gray" Height="30" Stretch="Fill" Width="30" Canvas.Left="0" Margin="5 0 5 0"/> 43               </Border> 44             <ControlTemplate.Triggers> 45               <Trigger Property="IsMouseOver" Value="True"> 46                 <Setter Property="BorderBrush" TargetName="bd" Value="white"/> 47                 <Setter Property="Fill" TargetName="p1" Value="white"/> 48               </Trigger> 49             </ControlTemplate.Triggers> 50           </ControlTemplate> 51         </Setter.Value> 52       </Setter> 53     </Style> 54     <Style x:Key="btnFast" TargetType="{x:Type Button}"> 55       <Setter Property="Template"> 56         <Setter.Value> 57           <ControlTemplate TargetType="{x:Type Button}"> 58             <Border x:Name="bd" Height="50" Width="50" Background="Transparent" CornerRadius="36" BorderBrush="Gray" BorderThickness="2"> 59               <Path x:Name="p1" Data="M25.714 943.429q-10.857 10.857-18.286 7.429t-7.429-18.286v-841.143q0-14.857 7.429-18.286t18.286 7.429l405.714 405.714q4.571 4.571 7.429 10.857v-405.714q0-14.857 7.429-18.286t18.286 7.429l405.714 405.714q10.857 10.857 10.857 25.714t-10.857 25.714l-405.714 405.714q-10.857 10.857-18.286 7.429t-7.429-18.286v-405.714q-2.857 5.714-7.429 10.857z"  60                  HorizontalAlignment="Center" VerticalAlignment="Center" Canvas.Top="10" Fill="Gray" Height="30" Stretch="Fill" Width="30" Canvas.Left="0" Margin="6 0 0 0"/> 61             </Border> 62             <ControlTemplate.Triggers> 63               <Trigger Property="IsMouseOver" Value="True"> 64                 <Setter Property="BorderBrush" TargetName="bd" Value="white"/> 65                 <Setter Property="Fill" TargetName="p1" Value="white"/> 66               </Trigger> 67             </ControlTemplate.Triggers> 68           </ControlTemplate> 69         </Setter.Value> 70       </Setter> 71     </Style> 72     <Style x:Key="btnOpen" TargetType="{x:Type Button}"> 73       <Setter Property="Template"> 74         <Setter.Value> 75           <ControlTemplate TargetType="{x:Type Button}"> 76             <Border x:Name="bd" Height="60" Width="160" Background="Transparent" CornerRadius="3" BorderBrush="Gray" BorderThickness="2"> 77               <StackPanel Orientation="Horizontal" Margin="30 0 0 0"> 78               <Path x:Name="p1" Data="M1073.714 544q0 17.714-17.714 37.714l-192 226.286q-24.571 29.143-68.857 49.429t-82 20.286h-621.714q-19.429 0-34.571-7.429t-15.143-24.571q0-17.714 17.714-37.714l192-226.286q24.571-29.143 68.857-49.429t82-20.286h621.714q19.429 0 34.571 7.429t15.143 24.571zM877.714 347.429v91.429h-475.429q-53.714 0-112.571 27.143t-93.714 68.286l-195.429 229.714q0-2.286-0.286-7.143t-0.286-7.143v-548.571q0-52.571 37.714-90.286t90.286-37.714h182.857q52.571 0 90.286 37.714t37.714 90.286v18.286h310.857q52.571 0 90.286 37.714t37.714 90.286z"  79                  HorizontalAlignment="Center" VerticalAlignment="Center" Canvas.Top="10" Fill="Gray" Height="30" Stretch="Fill" Width="30" Canvas.Left="0" Margin="0 0 5 0"/> 80                 <TextBlock Text="打开文件..." Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center"/> 81               </StackPanel> 82               </Border> 83             <ControlTemplate.Triggers> 84               <Trigger Property="IsMouseOver" Value="True"> 85                 <Setter Property="BorderBrush" TargetName="bd" Value="white"/> 86                 <Setter Property="Fill" TargetName="p1" Value="white"/> 87               </Trigger> 88             </ControlTemplate.Triggers> 89           </ControlTemplate> 90         </Setter.Value> 91       </Setter> 92     </Style> 93  94  95 <Grid> 96     <Grid Margin="2"> 97       <Grid.RowDefinitions> 98         <RowDefinition Height="*"/> 99         <RowDefinition Height="90"/>100       </Grid.RowDefinitions>101       <MediaElement x:Name="MePack" Grid.RowSpan="2" MediaOpened="MePack_MediaOpened" LoadedBehavior="Manual"/>102       <Border x:Name="bdMain" Grid.RowSpan="2" Background="#161D27">103         <Button Style="{StaticResource btnOpen}" Click="openBtn_Click"/>104       </Border>105       <Grid Grid.Row="2" Height="80" Background="Black" Opacity=".8">106         <Grid.ColumnDefinitions>107           <ColumnDefinition Width="*"/>108           <ColumnDefinition Width="80"/>109           <ColumnDefinition Width="80"/>110           <ColumnDefinition Width="80"/>111           <ColumnDefinition Width="*"/>112         </Grid.ColumnDefinitions>113         <Grid.RowDefinitions>114           <RowDefinition Height="20"/>115           <RowDefinition Height="*"/>116         </Grid.RowDefinitions>117         <Slider x:Name="CvSlider" Grid.ColumnSpan="5" Style="{StaticResource SliderEnter}" 118             PreviewMouseLeftButtonDown="CvSlider_PreviewMouseLeftButtonDown"119             PreviewMouseLeftButtonUp="CvSlider_PreviewMouseLeftButtonUp"120             ValueChanged="CvSlider_ValueChanged"121             Maximum="0" 122             IsMoveToPointEnabled="True"/>123         <Button x:Name="btnBack" Grid.Row="1" Grid.Column="1" Style="{StaticResource btnFallback}" Margin="10 5" HorizontalAlignment="Center" VerticalAlignment="Center"/>124         <Button x:Name="btnPlay" Grid.Row="1" Grid.Column="2" Style="{StaticResource btnPlay}" Margin="10 0" HorizontalAlignment="Center" VerticalAlignment="Center" Click="btnSwitch" Tag="0" ToolTip="播放"/>125         <Button x:Name="btnStop" Grid.Row="1" Grid.Column="2" Visibility="Hidden" Style="{StaticResource btnStop}" Margin="10 0" HorizontalAlignment="Center" VerticalAlignment="Center" Click="btnSwitch" Tag="1" ToolTip="暂停"/>126         <Button x:Name="btnFast" Grid.Row="1" Grid.Column="3" Style="{StaticResource btnFast}" Margin="10 5" HorizontalAlignment="Center" VerticalAlignment="Center"/>127         <TextBlock x:Name="TimeLengh" Text="00:00:00" Margin="10" Grid.Column="4" Grid.Row="1" HorizontalAlignment="Right"/>128       </Grid>129     </Grid>130   </Grid>

View Code
 1 public DispatcherTimer tm = new DispatcherTimer(); 2     public TimeSpan ts = new TimeSpan(); 3     public int begin = 0; 4     public MediaElementMain() 5     { 6       InitializeComponent(); 7       tm.Tick += tm_Tick; 8       tm.Interval = TimeSpan.FromSeconds(0.05); 9       tm.Start(); 10     } 11  12     void tm_Tick(object sender, EventArgs e) 13     { 14       if(begin==0) 15       { 16         this.CvSlider.Value = MePack.Position.TotalSeconds; 17         int d = (int)CvSlider.Value / 3600;//时 18         int g = (int)CvSlider.Value - d * 3600; 19         int f = g / 60;//分 20         int w = g - f * 60;//秒 21  22         int o = (int)CvSlider.Maximum / 3600;//时 23         int p = (int)CvSlider.Maximum - o * 3600; 24         int k = p / 60;//分 25         int l = p - k * 60;//秒 26         TimeLengh.Text = d.ToString() + ":" + f.ToString() + ":" + w.ToString() + "/" + o.ToString() + ":" + k.ToString() + ":" + l.ToString(); 27       } 28     } 29  30     private void openBtn_Click(object sender, RoutedEventArgs e) 31     { 32       OpenFileDialog openFile = new OpenFileDialog(); 33       openFile.Filter = "ir files (*.wmv)||*.avi||*.mp3||*.mp4||All files (*.*)|*.*"; //过滤文件类型 34       if (openFile.ShowDialog() == true) 35       { 36         MePack.Source = new Uri(openFile.FileName,UriKind.Relative); 37         MePack.Play(); 38         bdMain.Visibility = Visibility.Hidden; 39         btnStop.Visibility = Visibility.Visible; 40         btnPlay.Visibility = Visibility.Hidden; 41         //getTime(openFile.FileName); 42       } 43     } 44     //public static string getTime(string path) 45     //{ 46     //  try 47     //  { 48     //    Shell32.Shell shell = new Shell32.ShellClass(); 49     //    //文件路径 50     //    Shell32.Folder folder = shell.NameSpace(path.Substring(0, path.LastIndexOf("\\"))); 51     //    //文件名称 52     //    Shell32.FolderItem folderitem = folder.ParseName(path.Substring(path.LastIndexOf("\\") + 2)); 53     //    return folder.GetDetailsOf(folderitem, 21); 54     //  } 55     //  catch (Exception ex) 56     //  { 57     //    return null; 58     //  } 59     //} 60     private void MePack_MediaOpened(object sender, RoutedEventArgs e) 61     { 62       btnSwitch(btnPlay, null); 63       CvSlider.Minimum = 0; 64       CvSlider.Maximum = MePack.NaturalDuration.TimeSpan.TotalSeconds; 65       TimeSpan totaotp = MePack.NaturalDuration.TimeSpan; 66     } 67     private void btnSwitch(object sender, RoutedEventArgs e) 68     { 69       if (MePack.Source != null) 70       { 71         Button btn = sender as Button; 72         int num = Convert.ToInt32(btn.Tag); 73         if (num == 1) 74         { 75           MePack.Pause(); 76           btnStop.Visibility = Visibility.Hidden; 77           btnPlay.Visibility = Visibility.Visible; 78         } 79         else 80         { 81           MePack.Play(); 82           btnStop.Visibility = Visibility.Visible; 83           btnPlay.Visibility = Visibility.Hidden; 84         } 85       } 86     } 87  88     private void CvSlider_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 89     { 90       begin = 1; 91     } 92  93     private void CvSlider_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) 94     { 95       MePack.Position = ts; 96       int d = (int)CvSlider.Value / 3600;//时 97       int g = (int)CvSlider.Value - d * 3600; 98       int f = g / 60;//分 99       int w = g - f * 60;//秒100 101       int o = (int)CvSlider.Maximum / 3600;//时102       int p = (int)CvSlider.Maximum - o * 3600;103       int k = p / 60;//分104       int l = p - k * 60;//秒105       TimeLengh.Text = d.ToString() + ":" + f.ToString() + ":" + w.ToString() + "/" + o.ToString() + ":" + k.ToString() + ":" + l.ToString();106       begin = (int)ts.TotalSeconds;107     }108 109     private void CvSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)110     {111       ts = TimeSpan.FromSeconds(e.NewValue);112       int d = (int)CvSlider.Value / 3600;//时113       int g = (int)CvSlider.Value - d * 3600;114       int f = g / 60;//分115       int w = g - f * 60;//秒116 117       int o = (int)CvSlider.Maximum / 3600;//时118       int p = (int)CvSlider.Maximum - o * 3600;119       int k = p / 60;//分120       int l = p - k * 60;//秒121       TimeLengh.Text = d.ToString() + ":" + f.ToString() + ":" + w.ToString() + "/" + o.ToString() + ":" + k.ToString() + ":" + l.ToString();122     }

View Code

效果图:

http://www.cnblogs.com/yanjinhua/p/5643459.html