编程语言
首页 > 编程语言> > C# MediaPlayer

C# MediaPlayer

作者:互联网

  1 using System.Windows.Media;
  2 using Newtonsoft.Json;
  3 using System.ComponentModel;
  4 
  5 namespace ConsoleApp378
  6 {
  7     class Program:INotifyPropertyChanged
  8     {
  9         static void Main(string[] args)
 10         {
 11             Program obj = new Program();
 12             obj.MediaPlayerDemo();
 13             Console.ReadLine(); 
 14         }
 15 
 16         static MediaPlayer mp3Player = new MediaPlayer();
 17         void MediaPlayerDemo()
 18         {
 19             string mp3Path = Directory.GetCurrentDirectory() + @"\MediaResource\StarSky.mp3";
 20             mp3Player.Open(new Uri(mp3Path));
 21             mp3Player.MediaEnded += Mp3Player_MediaEnded;
 22             Console.WriteLine(" Start:Y;Exit:Q;Amplify:A;Decrease:D;Pause:P;C:Acclerate;R:Replay from begining!");
 23             string line;
 24             while ((line = Console.ReadLine()) != null)
 25             {
 26                 ControlMediaPlayer(line[0]);
 27             }
 28         }
 29 
 30         private bool isMP3PlayerCompleted = false;
 31 
 32         public event PropertyChangedEventHandler PropertyChanged;
 33         private void OnPropertyChanged(string propName)
 34         {
 35             if(PropertyChanged!=null)
 36             {
 37                 PropertyChanged(this, new PropertyChangedEventArgs(propName));
 38             }
 39         }
 40 
 41         public bool ISMP3PlayerCompleted
 42         {
 43             get
 44             {
 45                 return isMP3PlayerCompleted;
 46             }
 47             set
 48             {
 49                 if(value!=isMP3PlayerCompleted)
 50                 {
 51                     isMP3PlayerCompleted = value;
 52                     OnPropertyChanged("ISMP3PlayerCompleted");
 53                 }
 54 
 55                 if(isMP3PlayerCompleted)
 56                 {
 57                     MessageBox.Show("Finished!");
 58                 }
 59             }
 60         }
 61         private  void Mp3Player_MediaEnded(object sender, EventArgs e)
 62         {
 63             ISMP3PlayerCompleted = true;
 64         }
 65 
 66         void ControlMediaPlayer(char c)
 67         {
 68             if (mp3Player != null && mp3Player.HasAudio)
 69             {
 70                 string jsonContent = ""; 
 71                 switch (c)
 72                 {
 73                     //Start
 74                     case 'Y':
 75                         mp3Player.Play();
 76                         jsonContent = JsonConvert.SerializeObject(mp3Player, Formatting.Indented);
 77                         ShowMsg(jsonContent);
 78                         break;
 79 
 80                     //Exit
 81                     case 'Q':
 82                         mp3Player.Stop();
 83                         jsonContent = JsonConvert.SerializeObject(mp3Player, Formatting.Indented);
 84                         ShowMsg(jsonContent);
 85                         break;
 86 
 87                     //Amplify 
 88                     case 'A':
 89                         mp3Player.Volume = mp3Player.Volume + 0.1;
 90                         jsonContent = JsonConvert.SerializeObject(mp3Player, Formatting.Indented);
 91                         ShowMsg(jsonContent);
 92                         break;
 93 
 94                     //Decrease
 95                     case 'D':
 96                         mp3Player.Volume = mp3Player.Volume - 0.1;
 97                         jsonContent = JsonConvert.SerializeObject(mp3Player, Formatting.Indented);
 98                         ShowMsg(jsonContent);
 99                         break;
100 
101                     //Pause
102                     case 'P':
103                         mp3Player.Pause();
104                         jsonContent = JsonConvert.SerializeObject(mp3Player, Formatting.Indented);
105                         ShowMsg(jsonContent);
106                         break;
107 
108                     //Accelerate
109                     case 'C':
110                         mp3Player.SpeedRatio += 0.3;
111                         jsonContent = JsonConvert.SerializeObject(mp3Player, Formatting.Indented);
112                         ShowMsg(jsonContent);
113                         break;
114 
115                     case 'L':
116                         mp3Player.SpeedRatio -= 0.3;
117                         jsonContent = JsonConvert.SerializeObject(mp3Player, Formatting.Indented);
118                         ShowMsg(jsonContent);
119                         break;
120 
121                     //Replay
122                     case 'R':
123                         mp3Player.Position = default(TimeSpan);
124                         mp3Player.Play();
125                         jsonContent = JsonConvert.SerializeObject(mp3Player, Formatting.Indented);
126                         ShowMsg(jsonContent);
127                         break;
128                 }
129             }
130         }
131 
132         void ShowMsg(string msg)
133         {
134             Console.WriteLine(msg);
135         }
136         void ShowStatus(MediaPlayer mPlayer)
137         {
138             if(mPlayer!=null)
139             {
140                 string msg = $"Position:{mp3Player.Position},SpeedRatio:{mp3Player.SpeedRatio},IsMuted:{mp3Player.IsMuted}," +
141                     $"ScrubbingEnabled:{mp3Player.ScrubbingEnabled},Balance:{mp3Player.Balance},NaturalVideoWidth:{mp3Player.NaturalVideoWidth}"
142                     + $"Source:{mp3Player.Source}";
143                 Console.WriteLine(msg);
144             }
145         }
146 }

 

标签:case,break,jsonContent,C#,mp3Player,MediaPlayer,ShowMsg,JsonConvert
来源: https://www.cnblogs.com/Fred1987/p/11784027.html