private void lstvLibrary_DoubleClick(object sender, EventArgs e) { int ctIndex = lstvLibrary.FocusedItem.Index; currentSong = currentList[ctIndex]; player.URL = currentSong.Path; timer1.Start(); DisplayTrackInfo(currentSong, lblCurrentlyPlaying); btnPause.Text = "Pause"; btnNext.Enabled = true; btnPrevious.Enabled = true; }
//Display currently playing private void DisplayTrackInfo(MP3 mp3, Label lbl) { if (lbl.InvokeRequired) { Action <MP3, Label> action = DisplayTrackInfo; lbl.Invoke(action, mp3, lbl); } else { lblCurrentlyPlaying.Text = mp3.ToString(); } }
//Play / Pause private void btnPause_Click(object sender, EventArgs e) { //If an item in the list is selected... if (lstvLibrary.SelectedIndices.Count > 0) { //And if the selected item is different from the // currently playing track... int sindex = lstvLibrary.FocusedItem.Index; if (currentSong != currentList[sindex]) { //start the new track player.URL = currentList[sindex].Path; //Save reference to current track currentSong = currentList[sindex]; DisplayTrackInfo(currentSong, lblCurrentlyPlaying); btnPause.Text = "Pause"; //Restart the timer timer1.Start(); } //Otherwise, if a track is playing... else if (player.controls.get_isAvailable("pause")) { //pause the current track player.controls.pause(); btnPause.Text = "Play"; timer1.Stop(); } //Else (If the current track is paused // and the user has not selected a different // one... else { //play the paused track player.controls.play(); btnPause.Text = "Pause"; timer1.Start(); } } //Play first song in current list else { currentSong = currentList[0]; player.URL = currentSong.Path; lstvLibrary.Items[0].Focused = true; lstvLibrary.Items[0].Selected = true; DisplayTrackInfo(currentSong, lblCurrentlyPlaying); timer1.Start(); } lstvLibrary.Focus(); btnNext.Enabled = true; btnPrevious.Enabled = true; }
//====================================================================================== //=====================================METHODS========================================== //====================================================================================== //Select folder to add contents to a List public void AddFolderToLibrary() { FolderBrowserDialog dialog = new FolderBrowserDialog(); //open window and execute when user selects "ok" if (dialog.ShowDialog() == DialogResult.OK) { //save folder path string folder = dialog.SelectedPath; //save all paths of mp3 files within the selected folder string[] mp3Paths = Directory.GetFiles(folder, "*.mp3", SearchOption.AllDirectories); //loop through files at saved paths and... foreach (string path in mp3Paths) { //access and save mp3 metadata... TagLib.File f = TagLib.File.Create(path); if (f.Tag.Performers.Count() > 0 && f.Tag.Genres.Count() > 0) { string title = f.Tag.Title; string artist = f.Tag.Performers[0]; string album = f.Tag.Album; uint track = f.Tag.Track; uint year = f.Tag.Year; string genre = f.Tag.Genres[0]; string filepath = path; //to create mp3 objects... MP3 mp3 = new MP3(title, artist, album, track, year, genre, filepath); //that will be saved to the library list mp3Library.Add(mp3); } else if (f.Tag.Genres.Count() < 1) { string title = f.Tag.Title; string artist = f.Tag.Performers[0]; string album = f.Tag.Album; uint track = f.Tag.Track; uint year = f.Tag.Year; string genre = "N/A"; string filepath = path; //to create mp3 objects... MP3 mp3 = new MP3(title, artist, album, track, year, genre, filepath); //that will be saved to the library list mp3Library.Add(mp3); } } } }
//Read List from BinaryFile private void ReadLibraryFromBinaryFile(List <MP3> lib) { if (File.Exists(library)) { FileStream fs = null; BinaryReader br = null; try { fs = new FileStream(library, FileMode.Open, FileAccess.Read); br = new BinaryReader(fs); while (br.BaseStream.Position != br.BaseStream.Length) { string title = br.ReadString(); string artist = br.ReadString(); string album = br.ReadString(); uint track = br.ReadUInt32(); uint year = br.ReadUInt32(); string genre = br.ReadString(); string path = br.ReadString(); MP3 mp3 = new MP3(title, artist, album, track, year, genre, path); mp3Library.Add(mp3); } } catch (ArgumentException ae) { MessageBox.Show(ae.Message); } catch (FileNotFoundException fne) { MessageBox.Show("Add folders to your music library.\n\n" + fne.Message); } catch (IOException ioe) { MessageBox.Show(ioe.Message); } finally { if (fs != null) { fs.Close(); } if (br != null) { br.Close(); } } } }
//previous song private void btnPrevious_Click(object sender, EventArgs e) { //When the current track is not the first in the list if (currentList.IndexOf(currentSong) > 0 && player.controls.get_isAvailable("pause")) { int ctindex = lstvLibrary.FocusedItem.Index; //play the previous one currentSong = currentList[ctindex - 1]; lstvLibrary.Items[ctindex - 1].Focused = true; lstvLibrary.Items[ctindex].Selected = false; lstvLibrary.Items[ctindex - 1].Selected = true; player.URL = currentSong.Path; DisplayTrackInfo(currentSong, lblCurrentlyPlaying); btnPause.Text = "Pause"; } lstvLibrary.Focus(); }
//next song private void btnNext_Click(object sender, EventArgs e) { //When the current track is not the last in the current list if (currentList.IndexOf(currentSong) < currentList.Count - 1 && player.controls.get_isAvailable("pause")) { //get the current track index int ctIndex = currentList.IndexOf(currentSong); //make next track the current track, // select and play it currentSong = currentList[ctIndex + 1]; lstvLibrary.Items[ctIndex + 1].Focused = true; lstvLibrary.Items[ctIndex].Selected = false; lstvLibrary.Items[ctIndex + 1].Selected = true; player.URL = currentSong.Path; DisplayTrackInfo(currentSong, lblCurrentlyPlaying); btnPause.Text = "Pause"; } lstvLibrary.Focus(); }
public void AddFilesToLibrary() { OpenFileDialog dialog = new OpenFileDialog(); dialog.Multiselect = true; if (dialog.ShowDialog() == DialogResult.OK) { string[] files = dialog.FileNames; foreach (string path in files) { TagLib.File f = TagLib.File.Create(path); string title = f.Tag.Title; string artist = f.Tag.Performers[0]; string album = f.Tag.Album; uint track = f.Tag.Track; uint year = f.Tag.Year; string genre = f.Tag.Genres[0]; string filepath = path; MP3 mp3 = new MP3(title, artist, album, track, year, genre, filepath); mp3Library.Add(mp3); } } }
//timer to show song position private void timer1_Tick(object sender, EventArgs e) { //update position and duration labels with timer tick // **(duration only displays with timer tick for some reason) lblPosition.Text = player.controls.currentPositionString; lblDuration.Text = player.currentMedia.durationString; //When the song ends, and it is not the last song in the list... if (player.controls.currentPosition > player.currentMedia.duration - 2 && currentList.IndexOf(currentSong) < currentList.Count - 1) { //get current track index int ctindex = currentList.IndexOf(currentSong); //get and play the next track currentSong = currentList[ctindex + 1]; player.URL = currentSong.Path; //redisplay song info and select new song lstvLibrary.Items[ctindex].Focused = false; lstvLibrary.Items[ctindex].Selected = false; lstvLibrary.Items[ctindex + 1].Focused = true; lstvLibrary.Items[ctindex + 1].Selected = true; DisplayTrackInfo(currentSong, lblCurrentlyPlaying); } }