Пример #1
0
        public async Task LoadPlaylist(StorageFile file)
        {
            Playlist playlist = new Playlist {
                Name = file.DisplayName
            };

            using (var streamReader = new StreamReader(await file.OpenStreamForReadAsync()))
            {
                PlaylistService service = new PlaylistService(new KeyValueStoreDatabaseService(SharedLogic.DatabasePath, "", ""));
                await service.AddPlaylistAsync(playlist);

                List <Mediafile> playlistSongs = new List <Mediafile>();
                string           line;
                int  index       = 0;
                int  failedFiles = 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(async() =>
                        {
                            try
                            {
                                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.
                                }

                                var accessFile = await StorageFile.GetFileFromPathAsync(path);
                                var token      = StorageApplicationPermissions.FutureAccessList.Add(accessFile);

                                Mediafile mp3File = await SharedLogic.CreateMediafile(accessFile); //prepare Mediafile
                                await SettingsViewModel.SaveSingleFileAlbumArtAsync(mp3File, accessFile);
                                await SharedLogic.NotificationManager.ShowMessageAsync(index + " songs sucessfully added into playlist: " + file.DisplayName);
                                playlistSongs.Add(mp3File);
                                StorageApplicationPermissions.FutureAccessList.Remove(token);
                            }
                            catch
                            {
                                failedFiles++;
                            }
                        });
                    }
                    await service.InsertTracksAsync(playlistSongs, playlist);

                    string message = string.Format("Playlist \"{3}\" successfully imported! Total Songs: {0} Failed: {1} Succeeded: {2}", index, failedFiles, index - failedFiles, file.DisplayName);
                    await SharedLogic.NotificationManager.ShowMessageAsync(message);
                }
            }
        }
Пример #2
0
        public async Task LoadPlaylist(StorageFile file)
        {
            //Core.CoreMethods.LibVM.Database.CreatePlaylistDB(file.DisplayName);
            //Dictionary<Playlist, IEnumerable<Mediafile>> PlaylistDict = new Dictionary<Models.Playlist, IEnumerable<Mediafile>>();
            Playlist playlist = new Playlist {
                Name = file.DisplayName
            };

            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 <Mediafile> playlistSongs = new List <Mediafile>();
                PlaylistService  service       = new PlaylistService(new KeyValueStoreDatabaseService(SharedLogic.DatabasePath, "", ""));
                await service.AddPlaylistAsync(playlist);

                while ((line = reader.ReadLine()) != null)
                {
                    lines.Add(line);
                    nr++;
                    if (line == "[playlist]")
                    {
                        hdr = true;
                    }
                    else if (!hdr)
                    {
                        return;
                    }
                    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(async() =>
                    {
                        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.
                            }
                            var accessFile = await StorageFile.GetFileFromPathAsync(path);
                            var token      = StorageApplicationPermissions.FutureAccessList.Add(accessFile);

                            Mediafile mp3File = await SharedLogic.CreateMediafile(accessFile); //prepare Mediafile
                            await SettingsViewModel.SaveSingleFileAlbumArtAsync(mp3File, accessFile);

                            await SharedLogic.NotificationManager.ShowMessageAsync(i + " of " + noe + " songs added into playlist: " + file.DisplayName);
                            playlistSongs.Add(mp3File);
                            StorageApplicationPermissions.FutureAccessList.Remove(token);
                        }
                        catch
                        {
                            failedFiles++;
                        }
                    });

                    await service.InsertTracksAsync(playlistSongs, playlist);
                }
                string message = string.Format("Playlist \"{3}\" successfully imported! Total Songs: {0} Failed: {1} Succeeded: {2}", count, failedFiles, count - failedFiles, file.DisplayName);
                await SharedLogic.NotificationManager.ShowMessageAsync(message);
            }
        }