示例#1
0
 // Adds a song to the music library and returns the song's ID. The song
 // parameter's ID is also set to the auto‐generated ID.
 public int AddSong(Song s)
 {
     DataTable table = musicDataSet.Tables["song"];
     DataRow row = table.NewRow();
     row["title"] = s.Title;
     row["artist"] = s.Artist;
     row["album"] = s.Album;
     row["filename"] = s.Filename;
     row["length"] = s.Length;
     row["genre"] = s.Genre;
     table.Rows.Add(row);
                 // Update this song's ID
                 s.Id = Convert.ToInt32(row["id"]);
     return s.Id;
 }
 private void addButton_Click(object sender, RoutedEventArgs e)
 {
     // Add the selected file to the music library
                 Song s = new Song
     {
         Title = titleTextBox.Text,
         Artist = artistTextBox.Text,
         Album = albumTextBox.Text,
         Genre = genreTextBox.Text,
         Length = lengthTextBox.Text,
         Filename = filenameTextBox.Text
     };
     string id = musicLib.AddSong(s).ToString();
                 // Add the song ID to the combo box
                 songIdComboBox.IsEnabled = true;
     (songIdComboBox.ItemsSource as ObservableCollection<string>).Add(id);
     songIdComboBox.SelectedIndex = songIdComboBox.Items.Count‐ 1;
                 // There is at least one song that can be deleted
                 deleteButton.IsEnabled = true;
 }
        private Song GetSongDetails(string filename)
        {
            try
            {
                // PM> Install-Package taglib
                // http://stackoverflow.com/questions/1750464/how-to-read-and-write-id3-tags-to-an-mp3-in-c
                TagLib.File file = TagLib.File.Create(filename);

                Song s = new Song
                {
                    Title = file.Tag.Title,
                    Artist = file.Tag.AlbumArtists[0],
                    Album = file.Tag.Album,
                    Genre = file.Tag.Genres[0],
                    Length = file.Properties.Duration.Minutes + ":" + file.Properties.Duration.Seconds,
                    Filename = filename
                };

                return s;
            }
            catch (Exception ex)
            {
                DisplayError(ex.Message);
                return null;
            }
        }
示例#4
0
 // Update the given song with the given song ID.  Returns true if the song
 // was updated, false if it could not because the song ID was not found.
 public bool UpdateSong(int songId, Song song)
 {
     return true;
 }