zoukankan      html  css  js  c++  java
  • WPF 播放器

      1 <Window
      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     x:Class="MusicPlayer.Window1"
      7     xmlns:Converters ="clr-namespace:MusicPlayer"
      8  WindowStyle="None" ResizeMode="NoResize"
      9     Title="Window1" Height="240" Width="402" mc:Ignorable="d" 
     10 
     11 Background="#FF554D4A">
     12     <Window.Resources>
     13         <Style TargetType="Button">
     14             <Setter Property="Background">
     15                 <Setter.Value>
     16                     <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
     17                         <GradientStop Offset="0" Color="White"/>
     18                         <GradientStop Offset="0.5" Color="#FF554D4A"/>
     19                     </LinearGradientBrush>
     20                 </Setter.Value>
     21             </Setter>
     22             <Setter Property="FontStyle" Value="Italic"/>
     23             <Setter Property="Margin" Value="5"/>
     24             <Setter Property="Width" Value="60"/>
     25             <Setter Property="Foreground" Value="Gold"/>
     26             <Style.Triggers>
     27                 <Trigger Property="Button.IsMouseOver" Value="True">
     28                     <Setter Property="Foreground" Value="Black"/>
     29                 </Trigger>
     30             </Style.Triggers>
     31         </Style>
     32     </Window.Resources>
     33     <Grid Height="240" Background="#FF554D4A" Margin="0,5,0,-5" 
     34 
     35 d:LayoutOverrides="HorizontalMargin">
     36         <Grid.RowDefinitions>
     37    <RowDefinition/>
     38             <RowDefinition Height="30"/>
     39             <RowDefinition Height="45"/>
     40         </Grid.RowDefinitions>
     41         <MediaElement x:Name="mdeMusic"
     42                       IsMuted="False" Stretch="Fill" Volume="0.5"
     43                       MediaEnded="mdeMusic_MediaEnded"
     44                       MediaOpened="mdeMusic_MediaOpened"
     45                       LoadedBehavior="Manual"
     46        Grid.Row="0" 
     47                       UnloadedBehavior="Stop"/>
     48      <Slider  Minimum="0"  ValueChanged="sldProgress_ValueChanged" 
     49    x:Name="sldProgress" Grid.Row="1" VerticalAlignment="Bottom"/>
     50   <Grid Grid.Row="2">
     51   <Grid.ColumnDefinitions>
     52    <ColumnDefinition Width="75"/>
     53    <ColumnDefinition Width="75"/>
     54    <ColumnDefinition Width="75"/>
     55    <ColumnDefinition />
     56    <ColumnDefinition Width="150"/>
     57   </Grid.ColumnDefinitions>
     58   <Button Grid.Column="0" x:Name="btnPlay" Content="Play" 
     59 
     60 Click="btnPlay_Click"></Button>
     61   <Button Grid.Column="1" x:Name="btnStop" Content="Stop" Click="btnStop_Click" 
     62 
     63 ></Button>
     64   <Button Grid.Column="2" x:Name="btnOpen" Content="Open" Click="btnOpen_Click" 
     65 
     66  HorizontalAlignment="Right" d:LayoutOverrides="Width"></Button>
     67   <Slider Value="{Binding Volume, ElementName=mdeMusic, Mode=TwoWay, 
     68 
     69 UpdateSourceTrigger=Default}"
     70   VerticalAlignment="Center" 
     71    Maximum="1" LargeChange="0.1" SmallChange="0.01" x:Name="sldVolumn" 
     72 
     73 Grid.Column="4" />  
     74   </Grid>
     75   <TextBlock x:Name="txtMusicName" TextWrapping="Wrap" Margin="0,-3,0,0" 
     76 
     77 VerticalAlignment="Top" Height="25" />
     78     </Grid>
     79 </Window>
     80 
     81  
     82 
     83 后台代码:
     84 
     85 using System;
     86 using System.Collections.Generic;
     87 using System.Linq;
     88 using System.Text;
     89 using System.Windows;
     90 using System.Windows.Controls;
     91 using System.Windows.Data;
     92 using System.Windows.Documents;
     93 using System.Windows.Input;
     94 using System.Windows.Media;
     95 using System.Windows.Media.Imaging;
     96 using System.Windows.Navigation;
     97 using System.Windows.Shapes;
     98 using Microsoft.Win32;
     99 using System.Windows.Media.Animation;
    100 using System.Windows.Threading;
    101 
    102 namespace MusicPlayer
    103 {
    104     /// <summary>
    105     /// Window1.xaml 的交互逻辑
    106     /// </summary>
    107     public partial class Window1 : Window
    108     {
    109         OpenFileDialog openFile = new OpenFileDialog();
    110         private string videoName = "";
    111         private DispatcherTimer timer;
    112         public Window1()
    113         {
    114             InitializeComponent();
    115             this.timer = new DispatcherTimer();
    116         }
    117 
    118 
    119         /// <summary>
    120         /// 打开文件时 设置进度条最大值
    121         /// </summary>
    122         /// <param name="sender"></param>
    123         /// <param name="e"></param>
    124         private void mdeMusic_MediaOpened(object sender, 
    125 
    126 System.Windows.RoutedEventArgs e)
    127         {
    128             double seconds = mdeMusic.NaturalDuration.TimeSpan.TotalSeconds;
    129             sldProgress.Maximum = seconds / 10;
    130             sldProgress.Value = 0;
    131 
    132             //一个所代表的秒数
    133             double baseSecond = seconds / sldProgress.Maximum;
    134             this.timer.Interval = new TimeSpan(001);
    135             this.timer.Tick += new EventHandler(timer_Tick);
    136             this.timer.Start();
    137         }
    138 
    139         /// <summary>
    140         /// 播放结束时
    141         /// </summary>
    142         /// <param name="sender"></param>
    143         /// <param name="e"></param>
    144         private void mdeMusic_MediaEnded(object sender, RoutedEventArgs e)
    145         {
    146             btnPlay.Content = "Play";
    147             mdeMusic.Position = TimeSpan.Zero;
    148             mdeMusic.Pause();
    149         }
    150 
    151         private void btnPlay_Click(object sender, 
    152 
    153 System.Windows.RoutedEventArgs e)
    154         {
    155             if (this.btnPlay.Content.ToString() == "Play")
    156             {
    157                 this.btnPlay.Content = "Pause";
    158                 mdeMusic.Play();
    159                 txtMusicName.Text = "正在播放:" + openFile.SafeFileName;
    160                 InitializePropertyValues();
    161             }
    162             else
    163             {
    164                 mdeMusic.Pause();
    165                 txtMusicName.Text = "已暂停:" + openFile.SafeFileName;
    166                 this.btnPlay.Content = "Play";
    167             }
    168         }
    169 
    170         /// <summary>
    171         /// 
    172         /// </summary>
    173         public void UserControl1()
    174         {
    175             InitializeComponent();
    176             Timeline.DesiredFrameRateProperty.OverrideMetadata(
    177             typeof(Timeline),
    178             new FrameworkPropertyMetadata
    179             {
    180                 DefaultValue = 20
    181             });
    182         }
    183 
    184         private void btnStop_Click(object sender, 
    185 
    186 System.Windows.RoutedEventArgs e)
    187         {
    188             mdeMusic.Position = TimeSpan.Zero;
    189             this.txtMusicName.Text = "";
    190             btnPlay.Content = "Play";
    191             mdeMusic.Stop();
    192         }
    193         /// <summary>
    194         /// 打开音乐
    195         /// </summary>
    196         /// <param name="sender"></param>
    197         /// <param name="e"></param>
    198         private void btnOpen_Click(object sender, 
    199 
    200 System.Windows.RoutedEventArgs e)
    201         {
    202             openFile.Filter = "All Files(*.*)|*.*";
    203             openFile.InitialDirectory = "F:\films";
    204             openFile.ShowDialog(this);
    205             videoName = openFile.FileName;
    206             if ((videoName != null) && (videoName != ""))
    207             {
    208                 Uri file = new Uri(videoName);
    209                 mdeMusic.Source = file;
    210                 mdeMusic.LoadedBehavior = MediaState.Manual;
    211                 mdeMusic.Play();
    212             }
    213         }
    214 
    215         void InitializePropertyValues()
    216         {
    217             mdeMusic.Volume = (double)sldVolumn.Value;
    218         }
    219 
    220         private void sldProgress_ValueChanged(object sender, 
    221 
    222 RoutedPropertyChangedEventArgs<double> e)
    223         {
    224             TimeSpan span = new TimeSpan(00, (int)(sldProgress.Value) * 10);
    225             mdeMusic.Position = span;
    226         }
    227 
    228         void timer_Tick(object sender, EventArgs e)
    229         {
    230             this.sldProgress.ValueChanged -= 
    231                 new RoutedPropertyChangedEventHandler<double>
    232 
    233 (sldProgress_ValueChanged);
    234 
    235             this.sldProgress.Value = this.mdeMusic.Position.TotalSeconds / 
    236 
    237 10.0;
    238 
    239             this.sldProgress.ValueChanged += 
    240                 new RoutedPropertyChangedEventHandler<double>
    241 
    242 (sldProgress_ValueChanged);
    243             try
    244             {
    245                 this.txtMusicName.Text = "正在播放:" + openFile.SafeFileName;
    246                 //显示播放时间/总时间
    247                 //this.txtMessage.Text = this.mdeMusic.Position.Hours.ToString
    248 
    249 () + ":" + this.mdeMusic.Position.Minutes.ToString() + ":" + 
    250 
    251 this.mdeMusic.Position.Seconds.ToString() + "/" + 
    252 
    253 this.mdeMusic.NaturalDuration.TimeSpan.Hours.ToString() + ":" + 
    254 
    255 this.mdeMusic.NaturalDuration.TimeSpan.Minutes.ToString() + ":" + 
    256 
    257 this.mdeMusic.NaturalDuration.TimeSpan.Seconds.ToString();
    258             }
    259             catch
    260             {
    261                 MessageBox.Show("出错了:" + e);
    262             }
    263         }
    264     }
    265 }
  • 相关阅读:
    笔记
    【转】并行类加载——让tomcat玩转双十一 @双十一实战
    Twitter Snowflake 生成唯一id java版本
    nginx添加 nginx_heath模块
    谁在关心toString的性能?
    HashMap的工作原理
    修改nginx版本名称伪装任意web server
    nginx concat模块配置 页面返回400 bad request
    linux 安装rz sz命令
    nginx 添加nginx-http-concat模块
  • 原文地址:https://www.cnblogs.com/zecVip/p/4429180.html
Copyright © 2011-2022 走看看