public async Task <IEnumerable <Mediafile> > LoadPlaylist(StorageFile file) { using (var streamReader = new StreamReader(await file.OpenStreamForReadAsync())) { List <string> playlistSongs = new List <string>(); string line; int index = 0; bool ext = false; while ((line = streamReader.ReadLine()) != null) { if (line.ToLower() == "#extm3u") //m3u header { ext = true; } else if (ext && line.ToLower().StartsWith("#extinf:")) //extinfo of each song { continue; } else if (line.StartsWith("#") || line == "") //pass blank lines { continue; } else { await Task.Run(() => { index++; FileInfo info = new FileInfo(file.Path); //get playlist file info to get directory path string path = line; if (!File.Exists(line) && line[1] != ':') // if file doesn't exist then perhaps the path is relative { path = info.DirectoryName + line; //add directory path to song path. } playlistSongs.Add(path); }); } } return(await PlaylistHelper.GetSongsInAllFoldersAsync(playlistSongs).ConfigureAwait(false)); } }
public async Task <IEnumerable <Mediafile> > LoadPlaylist(StorageFile file) { using (var reader = new StreamReader(await file.OpenStreamForReadAsync())) { bool hdr = false; //[playlist] header string version = ""; //pls version at the end int noe = 0; //numberofentries at the end. int nr = 0; int failedFiles = 0; //int count = 0; string line; //a single line in stream List <string> lines = new List <string>(); List <string> playlistSongs = new List <string>(); while ((line = reader.ReadLine()) != null) { lines.Add(line); nr++; if (line == "[playlist]") { hdr = true; } else if (!hdr) { return(null); } else if (line.ToLower().StartsWith("numberofentries=")) { noe = Convert.ToInt32(line.Split('=')[1]); } else if (line.ToLower().StartsWith("version=")) { version = line.Split('=')[1]; } } string[,] tracks = new string[noe, 3]; nr = 0; foreach (string l in lines) { var _l = l.ToLower(); if (_l.StartsWith("file") || _l.StartsWith("title") || _l.StartsWith("length")) { int tmp = 4; int index = 0; if (_l.StartsWith("title")) { tmp = 5; index = 1; } else if (_l.StartsWith("length")) { tmp = 6; index = 2; } string[] split = l.Split('='); int number = Convert.ToInt32(split[0].Substring(tmp)); if (number > noe) { continue; } tracks[number - 1, index] = split[1]; } //else if (!_l.StartsWith("numberofentries") && _l != "[playlist]" && !_l.StartsWith("version=")) //{ //} } for (int i = 0; i < noe; i++) { await Task.Run(() => { try { string trackPath = tracks[i, 0]; FileInfo info = new FileInfo(file.Path); //get playlist file info to get directory path string path = trackPath; if (!File.Exists(trackPath) && line[1] != ':') // if file doesn't exist then perhaps the path is relative { path = info.DirectoryName + line; //add directory path to song path. } playlistSongs.Add(path); } catch { failedFiles++; } }); } return(await PlaylistHelper.GetSongsInAllFoldersAsync(playlistSongs).ConfigureAwait(false)); } }