|
|||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionEver look for a program/code snippet to traverse your MP3 collection and create playlists along the way? The code in this article shows you how an ADO.NET DataSet, LINQ and a little recursion can make this happen quickly and easily. A Note About the Demo ProgramThe demo program, once launched, will start from the Working Directory (the directory in which it was started) and traverse each folder hunting for .mp3 files. If it finds a folder containing .mp3 files, it will create a new .m3u playlist file for that directory and all directories/files beneath it. It will also DELETE any existing .m3u files that might already exist! If you download and run the demo program, it will remove (delete) any existing .m3u files. BackgroundLike a lot of techie folks out there, I have converted my music collection into the digital world by ripping my CD tracks to MP3. I have my collection on a separate file-server which happens to be running IIS (6.0). Now I can access my music via SMB shares or via the "folder view" provided by IIS since I have the virtual directory configured for folder browsing. This worked out pretty well for a while. I could access all my music via a web browser from any machine in my house, but something was lacking. After getting tired of having to click every song I wanted to play or manually create a playlist for every album, I got to tinkering around with LINQ... The AlgorithmThe basic algorithm for the program is as follows:
Using the CodeNot a lot of code here! The bulk of the work for the meta data collection is done in the The interesting part is using LINQ to pull the info back out of the tables to do something constructive! First, let's iterate the // Loop through folders
foreach (var dr in _ds.dbFolders.Where(x => x.HasMp3s == true))
{
Notice the lambda expression to filter out the unwanted columns. Then we open a using (StreamWriter sr = new StreamWriter(Path.Combine(dr.Path, ALLM3U)))
{
NOTE: Now the fun part! var resSet = from d in _ds.dbFolders
join f in _ds.dbFiles on d.FolderId equals f.FolderId
where d.Path.StartsWith(dr.Path) && d.HasMp3s == true
select new { FullPath = Path.Combine(d.Path, f.Filename) };
foreach (var item in resSet)
{
sr.WriteLine(item.FullPath.Replace(dr.Path + @"\", ""));
}
We use LINQ to join the two tables together and filter out the records we don't want. Then we iterate the result set writing the lines of text into the current .m3u file. After each loop, we make sure to close the }
}
And that's it! Points of InterestIIS + .m3u + Windows Media Player = Cool Playlist CachingOne of the cool "extra's" I learned from this was that once you click the .m3u file and Media Player launches a song from the list, you can close the browser and still enjoy your list! Media Player caches the list so even return trips to Media Player will have the list saved. The list will dissolve if you launch something else with Media Player though... History
To Do:
|
||||||||||||||||||||||||||||||||||||||||