I am trying to process an audio library file which is about 350 KB. My application processes each song on the file gets its meta-data and displays it in a List Box.
The problem is that it takes too much time (about 1-2 mins). I need to make it faster like in the Windows Media Player. I tried to make it faster by saving meta data for each song in the library file. Now it takes about 30-50 secs. The application works fine; no performance issues.
If I use this method then it takes 1 min to load and process:
public void m3uplaylist()
{
try
{
song.Clear();
}
catch (Exception)
{
}
string fileName = null;
FileStream fStream = null;
StreamReader sReader = null;
if (File.Exists(filename))
{
fStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
sReader = new StreamReader(fStream);
while ((fileName = sReader.ReadLine()) != null)
{
if (fileName.Length > 0 && fileName.Substring(0, 1) != "#" && fileName.Substring(0, 1) != "\n") //Checks whether the first character of the line is not # or Enter
{
try
{
string[] row1 = { fileName.Substring(0, fileName.LastIndexOf("\\")) + "\\" + fileName.Substring(fileName.LastIndexOf("\\") + 1) }; //Stores the song details in string array so that it can be added to the Grid
for (int i = 0; i < row1.Length; i++)
{
Songss sing = new Songss(row1[i]);
song.Add(sing);
progress++;
workers.ReportProgress(progress, bar1);
}
TotalSongs = File.ReadAllText(filename).Length / 100;
}
catch (FileNotFoundException ex)
{
}
}
}
fStream.Close();
sReader.Close();
}
}
If I use this method the library loads in about 15 seconds:
public void mplplaylistload(string filename)
{
if (File.Exists(filename))
{
TotalSongs = File.ReadAllText(filename).Length / 100 - 500;
string file = System.IO.Path.GetFileNameWithoutExtension(filename);
// Create the query
var rowsFromFile = from c in XDocument.Load(
filename).Elements(
"playlist").Elements("abc123").Elements("Song")
select c;
// Execute the query
foreach (var row in rowsFromFile)
{
Songss sing = new Songss(row.Element("Path").Value);
sing.title = row.Element("Title").Value;
if (GetFullInfo == true)
{
sing.album = row.Element("Album").Value;
sing.artist = row.Element("Artist").Value;
sing.length = row.Element("Length").Value;
sing.size = row.Element("Size").Value;
if (File.Exists(row.Element("Image").Value))
{
BitmapImage img = new BitmapImage(new Uri(row.Element("Image").Value));
img.CacheOption = BitmapCacheOption.None;
sing.source = img;
}
}
song.Add(sing);
progress++;
workers.ReportProgress(progress);
}
}
}
The file from which it reads is like this:
<?xml version="1.0" encoding="utf-8"?>
<playlist>
<abc123>
<Song>
<Title>9</Title>
<Path>D:\MuziK\Music\9.mp3</Path>
<Album>Unkown Album</Album>
<Artist>Unknown Artist</Artist>
<Size>2,960KB</Size>
<Length>NA</Length>
<Image>C:\Users\The GaMeR\AppData\Local\Macalifa\albumarts\Unkown Album\9\art.jpg</Image>
</Song>
<Song>
<Title>Aa Bhi Ja Sanam</Title>
<Path>D:\MuziK\Music\Aa Bhi Ja Sanam.mp3</Path>
<Album></Album>
<Artist></Artist>
<Size>2,941KB</Size>
<Length>3:08</Length>
<Image>C:\Users\The GaMeR\AppData\Local\Macalifa\albumarts\\Aa Bhi Ja Sanam\art.jpg</Image>
</Song>
</abc123>
What I need is to optimize the time-taken by the library to load.
Note: I am using background worker to load the library. Is there any better approach than this?