0

After getting the path of an mp3 file in the database, I used that path to create a TagLib.File but couldn't read the string. My database I used is Microsoft SQL Server 2019.

using(var db= new KhangDuong())
            {
                var listTrack = db.Tracks.Where(x=>x.TrackID>1).ToList();
                List<Track> track = new List<Track>();
                track=listTrack;
                foreach (var item in track)
                {
                    TagLib.File tagFile = TagLib.File.Create(@item.Track_path);
                    AudioFileReader reader = new AudioFileReader(@item.Track_path);
                    TimeSpan duration = reader.TotalTime;
                    string artist = tagFile.Tag.Artists[0];
                    string album = tagFile.Tag.Album;
                    DAL_ListQueue create = new DAL_ListQueue();
                    create.createItem(floatLayout, item.TrackName, artist,album,duration.Minutes+":"+duration.Seconds);
                }

            }

Picture

I have copy a path for example: C:\Users\ACER\Music\Ahxello - Frisbee.mp3

I have tried many ways but it still doesn't work with the item.Track_path string, but a fixed string then it works:

using(var db= new KhangDuong())
            {
                var listTrack = db.Tracks.Where(x=>x.TrackID>1).ToList();
                List<Track> track = new List<Track>();
                track=listTrack;
                foreach (var item in track)
                {
                    string path = @"C:\Users\ACER\Music\Ahxello - Frisbee.mp3";
                    TagLib.File tagFile = TagLib.File.Create(path);
                    AudioFileReader reader = new AudioFileReader(path);
                    TimeSpan duration = reader.TotalTime;
                    string artist = tagFile.Tag.Artists[0];
                    string album = tagFile.Tag.Album;
                    DAL_ListQueue create = new DAL_ListQueue();
                    create.createItem(floatLayout, item.TrackName, artist,album,duration.Minutes+":"+duration.Seconds);
                }

            }

I also tried replacing the above fixed string with item.Track_path but it didn't work.

Here's how I enter the path into the database:

OpenFileDialog file = new OpenFileDialog();
            file.Filter = "All Media Files|*.mp3;*.flac;*.m4a;";
            file.Multiselect = true;

            if (file.ShowDialog() == DialogResult.OK)
            {
                List<string> files = new List<string>();
                files = file.FileNames.ToList();
                BUS_Track.addTrack(files, KhangDataView);
            }
  • And what are the contents of `@item.Track_path`? – orhtej2 Apr 26 '23 at 15:22
  • @item.Track_path will return a string, which is the path to my mp3 file, for example: C:\Users\ACER\Music\TheFatRat - No No No.mp3 – Khang Dương Apr 26 '23 at 15:31
  • Could it be that you have "illegal" characters in your path string? "Illegal" for windows file system for characters like : or ? or > or < and some more see https://stackoverflow.com/questions/1976007/what-characters-are-forbidden-in-windows-and-linux-directory-names - If yes, you have to validate/clean them first. – PeterCo Apr 26 '23 at 16:52
  • Just for completeness: You don't need to use an Audioreader for the duration. Just use the one provided from taglib, something like `tagFile.Properties.Duration`: see https://stackoverflow.com/questions/13089205/how-to-read-audio-mp3-tags-duration-and-subtitle-when-i-use-taglib#answers – PeterCo Apr 26 '23 at 16:56
  • I used Path.GetFullPath(), every way I can think but it not working. It only work with path I copy from database by Right Mouse + Copy and create a variable paste this string into, but a variable read string from database not working. @PeterCo – Khang Dương Apr 27 '23 at 03:36
  • What if you enclose your path in quotation marks (because your path include spaces)? Something like `tagFile = TagLib.File.Create("");` – PeterCo Apr 27 '23 at 07:19

1 Answers1

1

I found the error, my error, it was true that a string in my database, some utf-8 mp3 path was injected into my database and caused it to turn into '?'. My above code is working fine. Thanks to everyone who helped me.