Guest User

NAudio

a guest
Jun 8th, 2017
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.51 KB | None | 0 0
  1.         private void bgwTimeManager_DoWork(object sender, DoWorkEventArgs e)
  2.         {
  3.             IsDefaultQueuePlaying = Operations.IsQueueEmpty();
  4.             if (IsDefaultQueuePlaying)
  5.             {
  6.                 var song = Operations.GetDefaultQueueSong();
  7.                 foreach (DataRow songRow in song.Rows)
  8.                 {
  9.                     SetAlbumText(songRow["artist"].ToString());
  10.                     SetPlaybackText(songRow["duration"].ToString());
  11.                     SetTitleText(songRow["title"].ToString());
  12.                     _path = songRow["path"].ToString();
  13.                 }
  14.                 if (song.Rows.Count > 0)
  15.                 {
  16.                     MethodInvoker mi = delegate { trackBar1.Enabled = true; };
  17.                     if (InvokeRequired)
  18.                         Invoke(mi);
  19.  
  20.                     PlayMp3File(e);
  21.                 }
  22.                 else
  23.                 {
  24.                     SetAlbumText("Playlist empty...");
  25.                     SetPlaybackText("00:00");
  26.                     SetTitleText("Playlist empty...");
  27.                     MethodInvoker mi = delegate { trackBar1.Enabled = false; };
  28.                     if (InvokeRequired)
  29.                         Invoke(mi);
  30.                 }
  31.             }
  32.             else
  33.             {
  34.                 DataTable queueTopSong = Operations.GetQueueTopSong();
  35.                 if (queueTopSong.Rows.Count > 0)
  36.                 {
  37.                    
  38.                     var song = Operations.GetQueueSong(queueTopSong.Rows[0]["s_id"].ToString());
  39.                     foreach (DataRow songRow in song.Rows)
  40.                     {
  41.                         SetAlbumText(songRow["artist"].ToString());
  42.                         SetPlaybackText(songRow["duration"].ToString());
  43.                         SetTitleText(songRow["title"].ToString());
  44.                         _path = songRow["path"].ToString();
  45.                     }
  46.                     if (song.Rows.Count > 0)
  47.                     {
  48.                         MethodInvoker mi = delegate { trackBar1.Enabled = true; };
  49.                         if (InvokeRequired)
  50.                             Invoke(mi);
  51.  
  52.                         ReadDedication(queueTopSong);
  53.                         PlayMp3File(e);
  54.                         Operations.DeleteRowFromQueue(queueTopSong.Rows[0]["q_id"].ToString());
  55.                     }
  56.                     else
  57.                     {
  58.                         SetAlbumText("Playlist empty...");
  59.                         SetPlaybackText("00:00");
  60.                         SetTitleText("Playlist empty...");
  61.                         MethodInvoker mi = delegate { trackBar1.Enabled = false; };
  62.                         if (InvokeRequired)
  63.                             Invoke(mi);
  64.                     }
  65.                 }
  66.             }
  67.         }  
  68.  
  69.         private void ReadDedication(DataTable queueTopSong)
  70.         {
  71.             _readDedication = new SpeechSynthesizer();
  72.             _readDedication.SelectVoiceByHints(VoiceGender.Female, VoiceAge.Adult);
  73.             _readDedication.Rate = 1;
  74.             DataTable dedication = Operations.GetDedication(queueTopSong.Rows[0]["q_id"].ToString());
  75.             for (var index = 0; index < dedication.Rows.Count; index++)
  76.             {
  77.                 var msg = index == 0 ? "The Upcoming song is " : "This song is also ";
  78.  
  79.                 DataRow dedicationRow = dedication.Rows[index];
  80.                 msg += $"dedicated to {dedicationRow["dedicatedto"]}";
  81.                 _readDedication.Speak(msg);
  82.                 Thread.Sleep(10);
  83.                 msg = $"By {dedicationRow["dedicatedby"]}";
  84.                 _readDedication.Speak(msg);
  85.                 Thread.Sleep(10);
  86.                 msg = $"Here is the dedication message of {dedicationRow["dedicatedby"]}";
  87.                 _readDedication.Speak(msg);
  88.                 Thread.Sleep(10);
  89.  
  90.                 _readDedication.Speak(dedicationRow["dedicationmessage"].ToString());
  91.                 Thread.Sleep(10);
  92.                 Operations.DeleteDedication(dedicationRow["d_id"].ToString());
  93.             }
  94.         }
  95.  
  96.         private void PlayMp3File(DoWorkEventArgs e)
  97.         {
  98.             try
  99.             {
  100.                 _reader = new AudioFileReader(_path);
  101.                 waveOut = new WaveOut();
  102.                 var meter = new MeteringSampleProvider(_reader);
  103.                 meter.StreamVolume += Meter_StreamVolume;
  104.                 var vol = 60;
  105.                 MethodInvoker mi = delegate { vol = trackBar1.Value; };
  106.                 if (InvokeRequired)
  107.                     Invoke(mi);
  108.                 waveOut.Volume = (float)vol / 100;
  109.                 waveOut.Init(_reader);
  110.                 waveOut.PlaybackStopped += WaveOutOnPlaybackStopped;
  111.                 waveOut.Play();
  112.  
  113.                 //int secs = int.Parse(lblPlaybackTime.Text.Split(':')[1].Trim());
  114.                 //int mins = int.Parse(lblPlaybackTime.Text.Split(':')[0].Trim());
  115.  
  116.                 while (_reader.CurrentTime != _reader.TotalTime)
  117.                 {
  118.                     SetPlaybackText(_reader.CurrentTime.ToString("hh':'mm':'ss"));
  119.                     //secs -= 1;
  120.  
  121.                     if (bgwTimeManager.CancellationPending)
  122.                     {
  123.                         e.Cancel = true;
  124.                         waveOut.Stop();
  125.                         return;
  126.                     }
  127.  
  128.                     Thread.Sleep(1000);
  129.                 }
  130.             }
  131.             catch (Exception ex)
  132.             {
  133.                 MessageBox.Show(ex.Message);
  134.             }
  135.         }
Add Comment
Please, Sign In to add comment