看到园子里的大虾们写了播放器,这不,我也写了一个和大家在此分享一下.
分别是前台和后台;
备注:参考了一些园子里朋友的代码片段,该播放器是一个独立的 操作界面
需要传入 音频路径 .为了测试 您可以直接赋值给url
1 < Grid x:Name = " LayoutRoot " Background = " White " Height = " 237 " Width = " 499 " > 2 3 < MediaElement Height = " 0 " HorizontalAlignment = " Left " Margin = " 12,12,0,0 " Name = " myElement " VerticalAlignment = " Top " Width = " 0 " LoadedBehavior = " Manual " MediaEnded = " myElement_MediaEnded " /> 4 < Button Content = " 播放 " Height = " 23 " HorizontalAlignment = " Left " Margin = " 10,41,0,0 " Name = " button1 " VerticalAlignment = " Top " Width = " 75 " Click = " button1_Click " /> 5 < Button Content = " 暂停 " Height = " 23 " HorizontalAlignment = " Left " Margin = " 111,41,0,0 " Name = " button2 " VerticalAlignment = " Top " Width = " 75 " Click = " button2_Click " /> 6 < Button Content = " 停止 " Height = " 23 " HorizontalAlignment = " Left " Margin = " 211,41,0,0 " Name = " button3 " VerticalAlignment = " Top " Width = " 75 " Click = " button3_Click " /> 7 < Button Content = " 静音 " Height = " 23 " HorizontalAlignment = " Left " Margin = " 319,41,0,0 " Name = " button4 " VerticalAlignment = " Top " Width = " 75 " Click = " button4_Click " /> 8 < Label Height = " 23 " HorizontalAlignment = " Left " Margin = " 138,84,0,0 " Name = " label1 " VerticalAlignment = " Top " Width = " 120 " /> 9 < TextBlock Height = " 23 " HorizontalAlignment = " Left " Margin = " 10,113,0,0 " Name = " textBlock2 " Text = " 当前进度; " VerticalAlignment = " Top " /> 10 < Slider Height = " 23 " HorizontalAlignment = " Left " Margin = " 64,108,0,0 " Name = " slider1 " VerticalAlignment = " Top " Width = " 293 " Value = " {Binding ElementName=myElement,Path=Position} " ValueChanged = " slider1_ValueChanged " /> 11 < Label Height = " 23 " HorizontalAlignment = " Right " Margin = " 0,113,12,0 " Name = " label2 " VerticalAlignment = " Top " Width = " 132 " /> 12 < Label Height = " 23 " HorizontalAlignment = " Left " Margin = " 12,84,0,0 " Name = " label3 " VerticalAlignment = " Top " Width = " 120 " /> 13 < TextBlock Height = " 23 " HorizontalAlignment = " Left " Margin = " 259,147,0,0 " Name = " textBlock5 " Text = " 音量; " VerticalAlignment = " Top " /> 14 < Slider Height = " 23 " HorizontalAlignment = " Left " Margin = " 295,141,0,0 " Name = " slider2 " VerticalAlignment = " Top " Width = " 100 " ValueChanged = " slider2_ValueChanged " /> 15 < Label Height = " 23 " HorizontalAlignment = " Left " Margin = " 389,136,0,0 " Name = " label4 " VerticalAlignment = " Top " Width = " 59 " /> 16 < Button Content = " 下载 " Height = " 23 " HorizontalAlignment = " Left " Margin = " 412,41,0,0 " VerticalAlignment = " Top " Name = " btnDown " Width = " 75 " Click = " btnDown_Click_1 " /> 17 < Button Content = " 关闭 " Height = " 23 " HorizontalAlignment = " Left " Margin = " 424,214,0,0 " Name = " button5 " VerticalAlignment = " Top " Width = " 75 " Click = " button5_Click " /> 18 </ Grid > 19
=后台:
View Code
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Windows; 6 using System.Windows.Controls; 7 using System.Windows.Data; 8 using System.Windows.Documents; 9 using System.Windows.Input; 10 using System.Windows.Media; 11 using System.Windows.Media.Imaging; 12 using System.Windows.Navigation; 13 using System.Windows.Shapes; 14 using System.IO; 15 using System.Diagnostics; 16 17 namespace eOMS.CR 18 { 19 /// <summary> 20 /// PlayWindow.xaml 的交互逻辑 21 /// </summary> 22 23 public partial class PlayWindow : UserControl 24 { 25 string Url = string .Empty; // 音频文件路径 26 string sPath = string .Empty; // 保存文件路径 27 string isplay = " 0 " ; // 标记是否已经处于播放状态 28 System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer(); 29 30 public PlayWindow() 31 { 32 InitializeComponent(); 33 34 } 35 36 public PlayWindow( string SoundUrl) 37 { 38 39 try 40 { 41 42 InitializeComponent(); 43 Url = SoundUrl; 44 myElement.Source = new Uri(Url, UriKind.Absolute); 45 timer.Interval = new TimeSpan( 1000 ); 46 timer.Tick += new EventHandler(timer_Tick); 47 timer.Start(); 48 } 49 catch (Exception ex) 50 { 51 52 MessageBox.Show(ex.Message); 53 54 } 55 56 } 57 void timer_Tick( object sender, EventArgs e) 58 { 59 label2.Content = string .Format( 60 " {0}{1:00}:{2:00}:{3:00} " , 61 " 播放进度: " , 62 myElement.Position.Hours, 63 myElement.Position.Minutes, 64 myElement.Position.Seconds); 65 label3.Content = string .Format( " 下载进度:{0:##%} " , myElement.DownloadProgress); 66 67 } 68 69 70 private void InitializePropertyValues() 71 { 72 // myElement.Volume = (double)volumeSlider.Value; 73 // myElement.Source = new Uri(Url, UriKind.Absolute); 74 } 75 /// <summary> 76 /// 下载按钮引发的事件 77 /// </summary> 78 /// <param name="sender"></param> 79 /// <param name="e"></param> 80 // 播放进度 81 private void slider1_ValueChanged( object sender, RoutedPropertyChangedEventArgs < double > e) 82 { 83 84 try 85 { 86 87 if ( isplay == " 1 " ) 88 { 89 int value = ( int )slider1.Value; 90 int myvalue = ( int )myElement.NaturalDuration.TimeSpan.TotalSeconds; 91 TimeSpan ts = new TimeSpan( 0 , 0 , 0 , 0 , value); 92 // myElement.Position = TimeSpan.FromSeconds((myvalue* value) / 10); 93 myElement.Position = ts; 94 } 95 96 97 } 98 catch (Exception ex) 99 { 100 101 throw ex; 102 } 103 104 } 105 /// <summary> 106 /// 播放 107 /// </summary> 108 /// <param name="sender"></param> 109 /// <param name="e"></param> 110 private void button1_Click( object sender, RoutedEventArgs e) 111 { 112 myElement.Play(); 113 isplay = " 1 " ; 114 } 115 /// <summary> 116 /// 暂停 117 /// </summary> 118 /// <param name="sender"></param> 119 /// <param name="e"></param> 120 private void button2_Click( object sender, RoutedEventArgs e) 121 { 122 isplay = " 0 " ; 123 myElement.Pause(); 124 } 125 /// <summary> 126 /// 停止 127 /// </summary> 128 /// <param name="sender"></param> 129 /// <param name="e"></param> 130 private void button3_Click( object sender, RoutedEventArgs e) 131 { 132 isplay = " 0 " ; 133 myElement.Stop(); 134 } 135 /// <summary> 136 /// 静音 137 /// </summary> 138 /// <param name="sender"></param> 139 /// <param name="e"></param> 140 private void button4_Click( object sender, RoutedEventArgs e) 141 { 142 myElement.IsMuted = ! myElement.IsMuted; 143 } 144 /// <summary> 145 /// 音量控制 146 /// </summary> 147 /// <param name="sender"></param> 148 /// <param name="e"></param> 149 private void slider2_ValueChanged( object sender, RoutedPropertyChangedEventArgs < double > e) 150 { 151 // 音量控制 152 double d = ( double )((( double )slider2.Value) / 10 ); 153 myElement.Volume = d; 154 label4.Content = ( double .Parse(d.ToString( " F2 " )) * 100 ) + " % " ; 155 } 156 /// <summary> 157 /// 下载操作 158 /// </summary> 159 /// <param name="sender"></param> 160 /// <param name="e"></param> 161 private void btnDown_Click_1( object sender, RoutedEventArgs e) 162 { 163 164 System.Windows.Forms.SaveFileDialog sf = new System.Windows.Forms.SaveFileDialog(); 165 sf.Filter = " 音频文件|*.wav " ; 166 if ( sf.ShowDialog() == System.Windows.Forms.DialogResult.OK) 167 { 168 String targetPath = sf.FileName; 169 System.IO.File.Copy(Url, targetPath, true ); 170 } 171 172 173 } 174 /// <summary> 175 /// 隐藏当前窗口 176 /// </summary> 177 /// <param name="sender"></param> 178 /// <param name="e"></param> 179 private void button5_Click( object sender, RoutedEventArgs e) 180 { 181 myElement.Stop(); 182 myElement.Close(); 183 this .LayoutRoot.Visibility = Visibility.Hidden; 184 } 185 186 private void myElement_MediaEnded( object sender, RoutedEventArgs e) 187 { 188 isplay = " 0 " ; 189 } 190 191 192 } 193 }