Exemplo n.º 1
0
        /// <summary>
        /// Add a new media data to the registry from an actual media file.
        /// </summary>
        /// <param name="file">THe supplied media file. Make sure it is in a supported format.</param>
        public static async void AddMedia(StorageFile file)
        {
            try
            {
                MediaType mediaType = GetMediaType(file.FileType); // Figure out the type of file and add the right media data.

                switch (mediaType)
                {
                case MediaType.Audio:
                    AddMedia(await NBAudio.FromStorageFileAsync(file).ConfigureAwait(true));
                    break;

                case MediaType.Video:
                    AddMedia(await NBVideo.FromStorageFileAsync(file).ConfigureAwait(true));
                    break;

                case MediaType.Image:
                    AddMedia(await NBImage.FromStorageFileAsync(file).ConfigureAwait(true));
                    break;

                default:
                    throw new ArgumentException("This file is either unsupported or invalid.");     // In case all else fails, throw an error.
                }
            }
            catch (Exception e)
            {
                await new MessageDialog("Something went wrong, so we could not add this media at all: " + e.Message).ShowAsync(); // If something goes wrong, notify the users.
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// From the storage folder, import the saved media from .nbme files. Ignores all invalid and corrupted files.
        /// For the format of each .nbme file, see: <see cref="SaveNewMedia(NBMedia)"/>
        /// </summary>
        public static async void LoadMedias()
        {
            StorageFolder dataStorage = ApplicationData.Current.LocalFolder;
            StorageFolder mediasStorageFolder;

            try
            {
                mediasStorageFolder = await dataStorage.GetFolderAsync(MEDIA_STORAGE_NAME);
            }
            catch (FileNotFoundException)
            {
                mediasStorageFolder = await dataStorage.CreateFolderAsync(MEDIA_STORAGE_NAME);
            }

            IReadOnlyList <StorageFile> foundFiles = await mediasStorageFolder.GetFilesAsync(); // Get the files.

            int problemFilesCount = 0;                                                          // Stores the number of corrupted files to display later.

            foreach (StorageFile file in foundFiles)
            {
                if (file.FileType == MEDIA_STORAGE_FORMAT) // Invalid files are ignored and do not count towards the problem files count.
                {
                    try                                    // Read the lines of the file.
                    {
                        IList <string> contentLines = await FileIO.ReadLinesAsync(file);

                        string targetFormat = contentLines[0];
                        string targetPath   = contentLines[1];

                        StorageFile mediaFile = await StorageFile.GetFileFromPathAsync(targetPath); // This errors if the path is invalid. Suitable for a validity check.

                        MediaType mediaType = GetMediaType(targetFormat);                           // Get the type of media and import it.

                        NBMedia resultantMedia;
                        switch (mediaType)
                        {
                        case MediaType.Audio:
                            resultantMedia = await NBAudio.FromStorageFileAsync(mediaFile).ConfigureAwait(true);

                            break;

                        case MediaType.Video:
                            resultantMedia = await NBVideo.FromStorageFileAsync(mediaFile).ConfigureAwait(true);

                            break;

                        case MediaType.Image:
                            resultantMedia = await NBImage.FromStorageFileAsync(mediaFile).ConfigureAwait(true);

                            break;

                        default:
                            throw new ArgumentException("This file is either unsupported or invalid.");     // In case all else fails, throws the error to make it count towards the problem files.
                        }

                        resultantMedia.DataFile = file;
                        medias.Add(resultantMedia);
                    }
                    catch (Exception)
                    {
                        problemFilesCount++;
                        // new MessageDialog("Something went wrong while opening a media data file.").ShowAsync();
                    }
                }
            }

            if (problemFilesCount > 0)                                            // If there are any corrupted files, notify users about that.
            {
                string fileWordForm = (problemFilesCount > 1) ? "files" : "file"; // I like grammar and spelling. 1 problem file will return "file" and multiple problem files will return "files".
                await new MessageDialog($"Something went wrong while opening {problemFilesCount} media data {fileWordForm}. All problematic files are ignored due to possible corruption or denied access.\n\nPlease make sure to turn on File System Access for the Nebula Player app in\nSettings > Privacy > File System.\n\nIf it is allowed, please clear the list by clicking on the Clear List button and reimport all media.").ShowAsync();
            }
        }