Ejemplo n.º 1
0
        public async Task <IEnumerable <StorageFile> > GetStorageFilesInFolderAsync(StorageFolder folder)
        {
            //Get query options with which we search for files in the specified folder
            var options = DirectoryWalker.GetQueryOptions();

            /*
             * options.FileTypeFilter.Add(".mp3");
             * options.FileTypeFilter.Add(".wav");
             * options.FileTypeFilter.Add(".ogg");
             * options.FileTypeFilter.Add(".flac");
             * options.FileTypeFilter.Add(".m4a");
             * options.FileTypeFilter.Add(".aif");
             * options.FileTypeFilter.Add(".wma");*/

            //this is the query result which we recieve after querying in the folder
            StorageFileQueryResult queryResult = folder.CreateFileQueryWithOptions(options);

            //'count' is for total files got after querying.
            uint count = await queryResult.GetItemCountAsync();

            //the event for files changed
            queryResult.ContentsChanged += QueryResult_ContentsChanged;

            if (count == 0)
            {
                string error = "No songs found!";
                BLogger.Logger.Error("No songs were found!");
                await SharedLogic.NotificationManager.ShowMessageAsync(error);

                return(null);
            }

            return(await queryResult.GetFilesAsync());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Add folder to Library asynchronously.
        /// </summary>
        /// <param name="queryResult">The query result after querying in a specific folder.</param>
        /// <returns></returns>
        public static async Task <IEnumerable <Mediafile> > GetSongsFromFolderAsync(StorageFolder folder, bool useIndexer = true, uint stepSize = 20)
        {
            StorageFileQueryResult queryResult = folder.CreateFileQueryWithOptions(DirectoryWalker.GetQueryOptions(null, useIndexer));

            uint index = 0;
            IReadOnlyList <StorageFile> files = await queryResult.GetFilesAsync(index, stepSize);

            index += stepSize;

            var count = await queryResult.GetItemCountAsync();

            if (count <= 0)
            {
                BLogger.I("No songs found.");
                await SharedLogic.Instance.NotificationManager.ShowMessageAsync("No songs found! Please try again.");

                return(null);
            }
            var   tempList = new List <Mediafile>((int)count);
            short progress = 0;

            try
            {
                // Note that I'm paging in the files as described
                while (files.Count != 0)
                {
                    var fileTask = queryResult.GetFilesAsync(index, stepSize).AsTask();
                    for (int i = 0; i < files.Count; i++)
                    {
                        if (files[i]?.IsAvailable == true)
                        {
                            progress++;
                            Messenger.Instance.NotifyColleagues(MessageTypes.MsgUpdateSongCount, progress);
                            Mediafile mp3File = await TagReaderHelper.CreateMediafile(files[i], false).ConfigureAwait(false); //the core of the whole method.

                            if (mp3File != null)
                            {
                                mp3File.FolderPath = Path.GetDirectoryName(files[i].Path);
                                await SaveSingleFileAlbumArtAsync(mp3File, files[i]).ConfigureAwait(false);

                                SharedLogic.Instance.NotificationManager.ShowStaticMessage(progress + "\\" + count + " Song(s) Loaded");
                                tempList.Add(mp3File);
                            }
                        }
                    }
                    files  = await fileTask;
                    index += stepSize;
                }
            }
            catch (Exception ex)
            {
                string message = ex.Message + "||" + ex.InnerException;
                BLogger.E("Error while importing folder.", ex);
                await SharedLogic.Instance.NotificationManager.ShowMessageAsync(message);
            }
            return(tempList.DistinctBy(f => f.OrginalFilename));
        }
Ejemplo n.º 3
0
        private static async Task <IEnumerable <Mediafile> > GetSongsInFolderAsync(List <string> songs, StorageFolder folder)
        {
            StorageFileQueryResult queryResult = folder.CreateFileQueryWithOptions(DirectoryWalker.GetQueryOptions(null, true, FolderDepth.Shallow));
            uint index = 0, stepSize = 40;
            IReadOnlyList <StorageFile> files = await queryResult.GetFilesAsync(index, stepSize);

            index += stepSize;
            var tempList = new List <Mediafile>(songs.Count);

            // Note that I'm paging in the files as described
            while (files.Count != 0 && tempList.Count <= songs.Count)
            {
                var fileTask = queryResult.GetFilesAsync(index, stepSize).AsTask();
                for (int i = 0; i < files.Count; i++)
                {
                    try
                    {
                        if (songs.Contains(files[i].Path))
                        {
                            Mediafile mp3File = await TagReaderHelper.CreateMediafile(files[i], false).ConfigureAwait(false); //the core of the whole method.

                            if (mp3File != null)
                            {
                                mp3File.FolderPath = Path.GetDirectoryName(files[i].Path);
                                await LibraryHelper.SaveSingleFileAlbumArtAsync(mp3File, files[i]).ConfigureAwait(false);

                                tempList.Add(mp3File);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        BLogger.E("Loading of a playlist song in folder failed.", ex);
                    }
                }
                files = await fileTask.ConfigureAwait(false);

                index += stepSize;
            }

            return(tempList);
        }
Ejemplo n.º 4
0
        public async void DropFiles(object sender, DragEventArgs e)
        {
            if (e.DataView.Contains(StandardDataFormats.StorageItems))
            {
                var items = await e.DataView.GetStorageItemsAsync();

                if (items.Any())
                {
                    foreach (var item in items)
                    {
                        if (item.IsOfType(StorageItemTypes.File) && Path.GetExtension(item.Path) == ".mp3")
                        {
                            Mediafile mp3file  = null;
                            string    path     = item.Path;
                            var       tempList = new List <Mediafile>();
                            if (TracksCollection.Elements.All(t => t.Path != path))
                            {
                                try
                                {
                                    mp3file = await CreateMediafile(item as StorageFile);

                                    await SettingsViewModel.SaveSingleFileAlbumArtAsync(mp3file).ConfigureAwait(false);

                                    AddMediafile(mp3file);
                                }
                                catch (Exception ex)
                                {
                                    BLogger.Logger.Error("Error occured while drag/drop operation.", ex);
                                }
                            }
                        }
                        else if (item.IsOfType(StorageItemTypes.Folder))
                        {
                            await SettingsVM.AddFolderToLibraryAsync((item as StorageFolder).CreateFileQueryWithOptions(DirectoryWalker.GetQueryOptions()));
                        }
                    }
                    Messenger.Instance.NotifyColleagues(MessageTypes.MSG_ADD_ALBUMS, "");
                }
            }
        }
        public async void SetupDirectoryWatcher(IEnumerable <StorageFolder> folderCollection)
        {
            await Task.Delay(10000);

            foreach (var folder in folderCollection)
            {
                StorageFileQueryResult queryResult = folder.CreateFileQueryWithOptions(DirectoryWalker.GetQueryOptions());
                var files = await queryResult.GetItemCountAsync();

                queryResult.ContentsChanged += QueryResult_ContentsChanged;;
            }
        }