示例#1
0
文件: Media.cs 项目: philwo/crossfade
        /**
         * Writes the changed fields to the ID3v2 tag using TagLib
         * Only works on local audio files.
         * */
        public void commitToTag()
        {
            if (this.uri.Scheme != Uri.UriSchemeFile) return;

            Media akt_media = Playlist.Instance[Playlist.Instance.Cursor];
            float last_position = 0;

            bool wasPlayling = Player.Instance.isPlaying;
            if (akt_media != null && akt_media.uri == this.uri && Player.Instance.isPlaying)
            {
                last_position = Player.Instance.Position;
                Player.Instance.stop();
            }
            try
            {
                LibraryTagLib tl = new LibraryTagLib(this.uri);
                tl.Tracknr = this.TrackNr;
                tl.Title = this.Title;
                tl.Album = this.Album;
                tl.Artist = this.Artist;
                tl.Year = this.Year;
                tl.Genre = this.Genre;
                tl.Comment = this.Comment;
                tl.Composer = this.Composer;
                tl.Copyright = this.Copyright;
                tl.Rating = this.Rating;
                tl.save();
            }
            catch (IOException ex)
            {
                throw ex;
            }

            if (akt_media != null && akt_media.uri == this.uri && wasPlayling)
            {
                Player.Instance.open(ref akt_media);
                Player.Instance.play();
                Player.Instance.Position = last_position;
            }
        }
示例#2
0
        /**
         * Main function which gets passed a media object and tries to complete the missing information about this object. It calls TagLib to read ID3v2 tags, fiHash
         * to calculate the SHA1-hashvalue, etc.
         *
         * \param media The media object which should get completed.
         * \return The media object with hopefully more information that before.
         * */
        public Media getInfo(Media media)
        {
            if (media.uri.Scheme == Uri.UriSchemeFile)
            {
                System.IO.FileInfo fi = new System.IO.FileInfo(media.uri.LocalPath);

                if (media.MTime != fi.LastWriteTime)
                {
                    System.Diagnostics.Debug.Print("MTime modified, re-reading FileInfo: " + media.uri);

                    /* Filehash */
                    try
                    {
                        if (media.Type != Media.MediaType.Video)
                        {
                            fiHash hash = new fiHash(media.uri);
                            media.SHA1Sum = hash.SHA256;
                        }

                        if (media.uri.LocalPath.ToLower().EndsWith(".cda"))
                        {
                            media.Album = "Unknown Audio CD";
                            media.Artist = "Unknown";
                            media.Title = media.uri.LocalPath.ToString();

                            /* fixme, get length of audio track here */
                            //media.Length = ;

                            string trackname = Path.GetFileNameWithoutExtension(media.uri.LocalPath);
                            string alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.";
                            string tracknr = trackname.Trim(alphabet.ToCharArray());

                            try
                            {
                                media.TrackNr = Convert.ToUInt32(tracknr);
                            }
                            catch (InvalidCastException)
                            {
                                media.TrackNr = 1;
                            }
                        }
                        else if (media.Type == Media.MediaType.Audio)
                        {
                            /* Taglib */
                            LibraryTagLib tl = new LibraryTagLib(media.uri);
                            media.TrackNr = tl.Tracknr;
                            media.Title = tl.Title;
                            media.Album = tl.Album;
                            media.Artist = tl.Artist;
                            media.Year = tl.Year;
                            media.Genre = tl.Genre;
                            media.Comment = tl.Comment;
                            media.Composer = tl.Composer;
                            media.Copyright = tl.Copyright;
                            media.Length = tl.Length;
                            media.Rating = tl.Rating;
                            if (media.Album == "" && media.Title == "" && media.Artist == "")
                            {
                                string media_file = media.uri.LocalPath;
                                media.Title = media_file.Substring(media_file.LastIndexOf("\\") + 1);
                            }
                        }
                        else if (media.Type == Media.MediaType.Picture)
                        {
                            string media_file = media.uri.LocalPath;
                            media.Title = media_file.Substring(media_file.LastIndexOf("\\") + 1);
                            media.Artist = "(Image)";
                            media.Length = 10000;
                        }
                        else if (media.Type == Media.MediaType.Video)
                        {
                            string media_file = media.uri.LocalPath;
                            media.Title = media_file.Substring(media_file.LastIndexOf("\\") + 1);
                            media.Artist = "(Video)";
                        }
                        else
                        {
                            media.Title = "(Unknown)";
                            media.Artist = "(Unknown)";
                            media.Album = "(Unknown)";
                        }

                        /* LastWriteTime */
                        media.MTime = fi.LastWriteTime;
                    }
                    catch (IOException ex)
                    {
                        throw ex;
                    }
                }
                else
                {
                    System.Diagnostics.Debug.Print("MTime not modified, skipping FileInfo: " + media.uri);
                }
            }

            return media;
        }