Check if user added new music on Windows Phone 8.1

六月ゝ 毕业季﹏ 提交于 2020-01-02 05:46:09

问题


I'm trying to find out if a user added new music to the Music folder on the phone since app was last used.

I try to do this by checking the DateModified of the Music folder (which updates correctly on the computer when adding new music to the phone):

async void GetModifiedDate ()
{
    BasicProperties props = await KnownFolders.MusicLibrary.GetBasicPropertiesAsync();
    Debug.WriteLine("DATEMODIFIED: " + props.DateModified.ToString());
}

Unfortunately this returns:

DATEMODIFIED: 1/1/1601 1:00:00 AM +01:00

Am I doing something wrong or is there another quick way to check if user added new music?


回答1:


KnownFolders.MusicLibrary is a virtual location. Therefore I think, may be a problem in getting its properties.

The other problem is that DateModified may be a bad idea, as it may stay the same when user adds a file. You cannot relay on it. (some information). You can check it - when I've tried to move files in folders, their DateModified hadn't changed.

So in this case, I'm afraid you will have to list your files in MusicLibrary and then decide, what to save for future comparison. The sum of filesizes can be a good idea, hence there is a little chance that two different music files would be the same size. It depends also if you want to be notified if the user had moved file from one folder to another (total size won't change). If you want to ensure more you can remember the whole list of Tuple<file.FolderRelativeId, fileSize> (for ecample).

As FileQueries are not yet available for Windows Phone, you will have to retrive files recursively the simple code can look like this:

// first - a method to retrieve files from folder recursively 
private async Task RetriveFilesInFolder(List<StorageFile> list, StorageFolder parent)
{
    foreach (var item in await parent.GetFilesAsync()) list.Add(item);
    foreach (var item in await parent.GetFoldersAsync()) await RetriveFilesInFolder(list, item);
}

private async Task<List<StorageFile>> GetFilesInMusic()
{
    StorageFolder folder = KnownFolders.MusicLibrary;
    List<StorageFile> listOfFiles = new List<StorageFile>();
    await RetriveFilesInFolder(listOfFiles, folder);
    return listOfFiles;
}

Once you have a list of your files, you can decide what to remember for further comparison upon next app launch.




回答2:


You can check the folder size instead:

ulong musicFolderSize = (ulong)ApplicationData.Current.LocalSettings.Values["musicFolderSize"];

BasicProperties props = await KnownFolders.MusicLibrary.GetBasicPropertiesAsync();
if (props.Size != musicFolderSize)
{
    //  ...
    ApplicationData.Current.LocalSettings.Values["musicFolderSize"] = props.Size;
}



回答3:


Adding onto Romansz's answer:

The only way to guarantee that you know of a file change would be to track the files on the device and then compare if there is a change between launches.

A lazier way to get all of the files would be to use KnownFolders.MusicLibrary.GetFilesAsync(CommonFileQuery.OrderByName); which is a deep query by default. It will grab all of the files in one query, but can be really slow on massive directories. You then have to page through all of the files as shown

        //cache the virtual storage folder for the loop
        StorageFolder musicLibrary = KnownFolders.MusicLibrary; 
        uint stepSize= 50;
        uint startIndex = 0;
        while (true)
        {
            IReadOnlyList<StorageFile> files = await musicLibrary.GetFilesAsync(CommonFileQuery.OrderByName,startIndex,stepSize);

            foreach (var file in files)
            {
                //compare to see if file is in your list
            }
            if (files.Count < stepSize) break; 
            startIndex += stepSize;
        }


来源:https://stackoverflow.com/questions/23724001/check-if-user-added-new-music-on-windows-phone-8-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!