WPF语音播放MediaPlayer

时间:2016-07-12 10:12:30   收藏:0   阅读:883

技术分享

 1         private MediaPlayer player;
 2 
 3         public MainWindow()
 4         {
 5             InitializeComponent();
 6             player = new MediaPlayer();
 7         }
 8 
 9         private void ButtonOfOpen_Click(object sender, RoutedEventArgs e)
10         {
11             player.Open(new Uri(@"C:\Users\Ares\Desktop\Sound\TfrAlarm.mp3", UriKind.RelativeOrAbsolute));
12             DispatcherTimer timer = new DispatcherTimer();
13             timer.Interval = TimeSpan.FromSeconds(1);
14             timer.Tick += timer_Tick;
15             timer.Start();
16         }
17 
18         void timer_Tick(object sender, EventArgs e)
19         {
20             if (player.Source == null) return;
21 
22             labelofTime.Content = String.Format("{0}/{1}", player.Position.ToString(@"mm\:ss"), player.NaturalDuration.TimeSpan.ToString(@"mm\:ss"));
23         }
24 
25         private void ButtonOfPlay_Click(object sender, RoutedEventArgs e)
26         {
27             player.Play();
28         }
29 
30         private void ButtonOfPause_Click(object sender, RoutedEventArgs e)
31         {
32             player.Pause();
33         }
34 
35         private void ButtonOfStop_Click(object sender, RoutedEventArgs e)
36         {
37             player.Stop();
38         }

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!