コード例 #1
0
ファイル: MusicLib.cs プロジェクト: jgreenlee24/hTunes
        //    
        //
        /// <summary>
        /// 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.
        /// </summary>
        /// <param name="songId">Song ID to search for</param>
        /// <param name="song">Song data that has possibly changed</param>
        /// <returns>true if the song with the song ID was found, false otherwise</returns>
        public bool UpdateSong(int songId, Song song)
        {
            DataTable table = musicDataSet.Tables["song"];

            // Only one row should be selected
            foreach (DataRow row in table.Select("id=" + songId))
            {
                row["title"] = song.Title;
                row["artist"] = song.Artist;
                row["album"] = song.Album;
                row["genre"] = song.Genre;
                row["length"] = song.Length;
                row["filename"] = song.Filename;

                return true;
            }

            // Must not have found the song ID
            return false;
        }
コード例 #2
0
ファイル: MusicLib.cs プロジェクト: jgreenlee24/hTunes
        /// <summary>
        /// Add a song pointed to by the filename.  Returns the newly added song.
        /// </summary>
        /// <param name="filename">MP3 filename</param>
        /// <returns>Song created from the MP3</returns>
        public Song AddSong(string filename)
        {
            // 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);
            String name = filename.Substring(filename.LastIndexOf('\\')+1, filename.LastIndexOf('.') - filename.LastIndexOf('\\') - 1);
            Song s = new Song
            {
                Title = file.Tag.Title == null ? name : file.Tag.Title,
                Artist = file.Tag.AlbumArtists.Length == 0 ? "Unknown" : file.Tag.AlbumArtists[0],
                Album = file.Tag.Album == null ? "Unknown" : file.Tag.Album,
                Genre = file.Tag.Genres.Length == 0 ? "Unknown" : file.Tag.Genres[0],
                Length = file.Properties.Duration.Minutes + ":" + file.Properties.Duration.Seconds,
                Filename = filename
            };

            AddSong(s);
            return s;
        }
コード例 #3
0
ファイル: MusicLib.cs プロジェクト: jgreenlee24/hTunes
        /// <summary>
        /// Return a Song for the given song ID. Returns null if the song was not found.
        /// </summary>
        /// <param name="songId">ID of song to search for</param>
        /// <returns>The song matching the songId or null if the song wasn't found</returns>
        public Song GetSong(int songId)
        {
            DataTable table = musicDataSet.Tables["song"];

            // Only one row should be selected
            foreach (DataRow row in table.Select("id=" + songId))
            {
                Song song = new Song();
                song.Id = songId;
                song.Title = row["title"].ToString();
                song.Artist = row["artist"].ToString();
                song.Album = row["album"].ToString();
                song.Genre = row["genre"].ToString();
                song.Length = row["length"].ToString();
                song.Filename = row["filename"].ToString();

                return song;
            }

            // Must not have found this song ID
            return null;
        }
コード例 #4
0
ファイル: MusicLib.cs プロジェクト: jgreenlee24/hTunes
        /// <summary>
        /// Adds a song to the music library and returns the song's ID. The Song's ID
        /// is also updated to reflect the song's auto-assigned ID.
        /// </summary>
        /// <param name="s">Song to add</param>
        /// <returns>The song's ID</returns>
        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;
            row["url"] = s.AboutUrl;
            row["albumImage"] = s.AlbumImageUrl;
            table.Rows.Add(row);

            // Update this song's ID
            s.Id = Convert.ToInt32(row["id"]);

            return s.Id;
        }
コード例 #5
0
ファイル: MusicLib.cs プロジェクト: tstone4/Htunes
        private Song InfoFromAPI(Song s, string artist, string title)
        {
            String url = "http://ws.audioscrobbler.com/2.0/?method=track.getInfo&api_key=" + API_KEY + "&" +
                "artist=" + WebUtility.UrlEncode(artist) + "&track=" + WebUtility.UrlEncode(title);
            try
            {
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                using (WebResponse response = request.GetResponse())
                {
                    Stream strm = response.GetResponseStream();
                    using (XmlTextReader reader = new XmlTextReader(strm))
                    {
                        while (reader.Read())
                        {
                            if (reader.IsStartElement())
                            {
                                Console.WriteLine(reader.Name);
                                //Console.WriteLine(reader.ReadString());
                                if (reader.Name == "image")
                                {
                                    if (reader.GetAttribute("size") == "medium")
                                        s.AlbumImageUrl = reader.ReadString();
                                }
                                if (reader.Name == "url")
                                {
                                    if(s.AboutUrl == "" || s.AboutUrl == null)
                                    {
                                        s.AboutUrl = reader.ReadString();
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (WebException e)
            {
                // A 400 response is returned when the song is not in their library
                Console.WriteLine("Error: " + e.Message);
            }

            return s;
        }
コード例 #6
0
ファイル: MusicLib.cs プロジェクト: tstone4/Htunes
        /// <summary>
        /// Add a song pointed to by the filename.  Returns the newly added song.
        /// </summary>
        /// <param name="filename">MP3 filename</param>
        /// <returns>Song created from the MP3</returns>
        public Song AddSong(string filename)
        {
            // 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],
                Filename = filename
            };

            if (file.Properties.Duration.Seconds > 9)
            {
                s.Length = file.Properties.Duration.Minutes + ":" + file.Properties.Duration.Seconds;
            }
            else
            {
                s.Length = file.Properties.Duration.Minutes + ":0" + file.Properties.Duration.Seconds;
            }

            s = InfoFromAPI(s, s.Artist, s.Title);

            AddSong(s);
            return s;
        }
コード例 #7
0
ファイル: MainWindow.xaml.cs プロジェクト: jgreenlee24/hTunes
 private void PlayMusic()
 {
     DataRowView row = (DataRowView)dataGrid.SelectedItems[0];
     song = musicLib.GetSong(Int32.Parse(row["Id"].ToString()));
     mediaPlayer.Open(new Uri(song.Filename));
     mediaPlayer.Play();
 }
コード例 #8
0
ファイル: MusicLib.cs プロジェクト: zachatrocity/hTunes
        /// <summary>
        /// Add a song pointed to by the filename.  Returns the newly added song.
        /// </summary>
        /// <param name="filename">MP3 filename</param>
        /// <returns>Song created from the MP3</returns>
        public Song AddSong(string filename)
        {
            // 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);

            //get image url
            string imageurl = "";
            string url = "http://ws.audioscrobbler.com/2.0/?method=track.getInfo&api_key=e1d7cac3d825c39c69e1e0f2a73ca7f8&artist=" + WebUtility.UrlEncode(file.Tag.AlbumArtists[0]) + "&track=" + WebUtility.UrlEncode(file.Tag.Title);
            try
            {
                           HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                           using (WebResponse response = request.GetResponse())
                           {
                              Stream strm = response.GetResponseStream();
                              using (XmlTextReader reader = new XmlTextReader(strm))
                              {
                                 while (reader.Read())
                                 {
                                    if (reader.NodeType == XmlNodeType.Element)
                                    {
                                       //Console.WriteLine(reader.Name);
                                       //Console.WriteLine(reader.ReadString());
                                       if (reader.Name == "image")
                                       {
                                          if (reader.GetAttribute("size") == "medium")
                                             imageurl = reader.ReadString();
                                       }
                                    }
                                 }
                              }
                           }
            }
            catch (WebException e)
            {
                           // A 400 response is returned when the song is not in their library
                           Console.WriteLine("Error: " + e.Message);
            }

            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,
                AboutUrl = "http://www.last.fm/music/" + WebUtility.UrlEncode(file.Tag.AlbumArtists[0]) + "/" + WebUtility.UrlEncode(file.Tag.Album),
                AlbumImageUrl = imageurl
            };

            AddSong(s);
            return s;
        }