private void AppTimer_Tick(object sender, EventArgs e) { if (AudioFileReader != null) { // Change the Label Text to the Music current Time LabelMusicCurrentTimeState.Text = AudioFileReader.CurrentTime.ToString(@"mm\:ss"); // Change the PlaybackBarControl value to the Music current Time PlaybackBarControl.Val = Convert.ToInt32(ConvertFrom.TimeToSeconds(AudioFileReader.CurrentTime)); // Check if the Music end, then start the Next one or repeat it if (LabelMusicCurrentTimeState.Text == LabelMusicEndTime.Text) { if (LoopState == LoopState.One) { AudioFileReader.Position = 0; } else if (LoopState == LoopState.Off || LoopState == LoopState.All) { MusicState = MusicState.Pause; ButtonPlayPause.BackgroundImage = Properties.Resources.Play; PlayAndPauseToolStripMenuItem.Text = "Play"; ButtonNext.PerformClick(); } } } }
private void ChangedProgressPanel_MouseMove(object sender, MouseEventArgs e) { if (AudioFileReader != null) { if (PlaybackBarControl.IsMouseDown) { AudioFileReader.CurrentTime = ConvertFrom.SecondsToTime(PlaybackBarControl.Val); } } else { PlaybackBarControl.Val = 0; } }
private void MusicInitialize(string path) { if (WaveOutEvent != null) { WaveOutEvent.Dispose(); } if (AudioFileReader != null) { AudioFileReader.Dispose(); } try { AudioFileReader = new AudioFileReader(path); WaveOutEvent = new WaveOutEvent(); WaveOutEvent.Init(AudioFileReader); WaveOutEvent.Play(); if (!AppTimer.Enabled) { AppTimer.Start(); } MusicState = MusicState.Play; ButtonPlayPause.BackgroundImage = Properties.Resources.Pause; PlayAndPauseToolStripMenuItem.Text = "Pause"; PlaybackBarControl.Max = Convert.ToInt32(ConvertFrom.TimeToSeconds(AudioFileReader.TotalTime)); AudioFileReader.Volume = TrackBarVolumeState.Value / 10f; // This condition is related to the (Exception) Code Part // The Path Variable will be changed to the CurrentPlayingMusic, for get the original audio meatdata if (IsFileGenerateException) { path = Music[CurrentPlayingMusicIndex]; } // Change the Label Text to the Music Total Time // Change the PictureBox BackgroundImage to the Music Cover LabelMusicEndTime.Text = AudioFileReader.TotalTime.ToString(@"mm\:ss"); PictureBoxMusicCover.BackgroundImage = TagFile.GetCover(path); // Change the Form Title to the Music Title + Artist this.Text = TagFile.GetArtists(path) + " - " + TagFile.GetTitle(path); // Change the Current MusicPanel BackgroundColor, and reset the others foreach (MusicPanel musicPanel in FlowLayoutPanelMusic.Controls) { if (musicPanel.MusicPath == path) { musicPanel.BackColor = Color.FromArgb(28, 28, 28); } else { musicPanel.BackColor = Color.Transparent; } } // Reset the "IsFileGenerateException" value to false IsFileGenerateException = false; } catch { try { // If Audio File GenerateException, Convert it to 'wav' and save it // on a Temp File, so we can play it later AudioFileReader = null; using (var reader = new MediaFoundationReader(Music[CurrentPlayingMusicIndex])) { if (!Directory.Exists("TempFiles/")) { Directory.CreateDirectory("TempFiles/"); } // Save the 'wav' audio on the Temp File WaveFileWriter.CreateWaveFile("TempFiles/temp.wav", reader); } // Change the "IsFileGenerateException" value to true, because the File is Generate Exception IsFileGenerateException = true; // Try to play the Temp File, if isn't played alert a message to the user MusicInitialize("TempFiles/temp.wav"); } catch (Exception Ex) { MessageBox.Show("This song is hard for us to play it for you, Please play another one." + Environment.NewLine + "(" + Ex.Message + ")", "We are sorry!", MessageBoxButtons.OK, MessageBoxIcon.Information); } } }