vitorsdcs
vitorsdcs

Reputation: 692

Get list of recently listened music

I'm developing a Windows Phone app that needs to retrieve and manipulate information about the songs played on the device.

I know it is possible to get the song that is currently playing using MediaPlayer.Queue.ActiveSong.

However, what I really need is to have access to a list of recently played tracks.

MediaHistory and MediaHistoryItem classes don't seem to provide this.

Is is really possible? How?

Upvotes: 4

Views: 381

Answers (2)

jay_t55
jay_t55

Reputation: 11662

The current API, as @Igor has pointed out in his answer does not allow this. However, there is another way for us to reasonably assume that a particular media file has been played recently, by getting some information about the actual file.

We can use GetBasicPropertiesAsync() along with RetrievePropertiesAsync() which will give us the DateAccessed property for that file.

Here is a code snippet taken from this MSDN page:

public async void test()
{

    try
    {
        StorageFile file = await StorageFile.GetFileFromPathAsync("Filepath");
        if (file != null)
        {
            StringBuilder outputText = new StringBuilder();

            // Get basic properties
            BasicProperties basicProperties = await file.GetBasicPropertiesAsync();
            outputText.AppendLine("File size: " + basicProperties.Size + " bytes");
            outputText.AppendLine("Date modified: " + basicProperties.DateModified);

            // Specify more properties to retrieve
            string dateAccessedProperty = "System.DateAccessed";
            string fileOwnerProperty = "System.FileOwner";
            List<string> propertiesName = new List<string>();
            propertiesName.Add(dateAccessedProperty);
            propertiesName.Add(fileOwnerProperty);

            // Get the specified properties through StorageFile.Properties
            IDictionary<string, object> extraProperties = await file.Properties.RetrievePropertiesAsync(propertiesName);
            var propValue = extraProperties[dateAccessedProperty];
            if (propValue != null)
            {
                outputText.AppendLine("Date accessed: " + propValue);
            }
            propValue = extraProperties[fileOwnerProperty];
            if (propValue != null)
            {
                outputText.AppendLine("File owner: " + propValue);
            }
        }
    }
    // Handle errors with catch blocks
    catch (FileNotFoundException)
    {
        // For example, handle a file not found error
    }
}

Once you have the DateAccessed property in a variable, we can see if it is a recent date, say, yesterday, or maybe even 2 or 3 days ago. Then we'll know that if it's been accessed within a short period of time, it could have been played.

There are some caveats to this, though. Some virus scanners change the Timestamp properties on files and folders, and they also need to open files to scan them which I would assume would change the DateAccessed property. However, many new Antivirus apps that I've seen revert the Timestamp info back to the original, as if it had never touched the file.

I believe this is the best workaround for this problem at the moment. Unless you only care about when your app recently played a file. Then the answer to that question is as simple as you managing your own recently-played lists for media files.

Update


In order to retrieve the PlayCount for a specified song, you can access that song using the MediaLibrary class:

MediaLibrary library = new MediaLibrary();

Then just access the song like this:

Int32 playCount = library.Songs[0].PlayCount;

where [0] is the index of the song you'd like to get the PlayCount for. An easier way (depending on how you're accessing songs already, might be to do something like:

Int32 playCount = library.Artists[selectedArtistIndex].Albums[selectedArtistAlbumIndex].Songs[selectedSongInAlbumIndex].PlayCount;

Upvotes: 1

Igor Kulman
Igor Kulman

Reputation: 16359

Not possible with the current API. MediaHistoryItem only returns last item set by your application, so it is of no use.

Upvotes: 0

Related Questions