Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

API to add playlists in Zune

Original Question (for windows phone 7): I am using windows phone 7 and would like to add downloaded podcasts to a play list so that I can listen to them in a single go. Unfortunately UI does not allow this. I would like to know whether there are any API to do this.

Modified Question (for windows phone 8): I need "add to playlist" api for windows phone 8

For being entitled for bounty please provide and API reference here. Other than working API reference link or sample will not be accepted as a correct answer.

("Not available / not supported" also will not be accepted as answer. Please do not bother to write these kind of answer)

like image 493
Krishna Kumar Avatar asked Dec 08 '10 09:12

Krishna Kumar


2 Answers

As I mentioned on twitter, in Windows Phone 8 you can add or remove songs from the device's music library using MediaLibraryExtensions. The new capability is mentioned on MSDN here. However, I couldn't find any documentation for the APIs, so here's the API printout for the new Microsoft.Xna.Framework.MediaLibraryExtensions.dll:

//Microsoft.Xna.Framework.MediaLibraryExtensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553

namespace Microsoft.Xna.Framework.Media.PhoneExtensions {
    public static class MediaLibraryExtensions {
        public static void Delete(MediaLibrary library, Song song);
        public static String GetPath(Picture picture);
        public static String GetPathFromToken(MediaLibrary library, String token);
        public static Stream GetPreviewImage(Picture picture);
        public static Song SaveSong(MediaLibrary library, Uri filename, SongMetadata songMetadata, SaveSongOperation operation);
    }

    public enum SaveSongOperation {
        CopyToLibrary, 
        MoveToLibrary
    }

    public sealed class SongMetadata {
        public SongMetadata();

        public Uri AlbumArtistBackgroundUri { get; set; }
        public String AlbumArtistName { get; set; }
        public Uri AlbumArtUri { get; set; }
        public String AlbumName { get; set; }
        public DateTime AlbumReleaseDate { get; set; }
        public Uri ArtistBackgroundUri { get; set; }
        public String ArtistName { get; set; }
        public TimeSpan Duration { get; set; }
        public String GenreName { get; set; }
        public String Name { get; set; }
        public Int32 TrackNumber { get; set; }
    }
}

You can use this new API by invoking SaveSong with a local URI and by potentially overriding the ID3 metadata by including a custom SongMetadata. This API only allows you to store new songs, but I guess you can group your podcasts under a factious artist. Quick note about this API is to make sure to add the new DLL reference MediaLibraryExtensions DLL. You can also can keep SongMetadata as null and have the WP8 OS infer ID3 metadata.

Here's a simple code snippet:

private async void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    var sourceFile = await Package.Current.InstalledLocation.GetFileAsync("ChargeOfTheLightBridge.mp3");
    CopyFileIntoIsoStore(sourceFile);

    var library = new MediaLibrary();
    library.SaveSong(new Uri(sourceFile.Name, UriKind.RelativeOrAbsolute),
                        new SongMetadata()
                        {
                            ArtistName = "My Custom Artist",
                            AlbumArtistName = "My Custom Artist",
                            Name = "My Custom Track Name",
                            AlbumName = "clubbing baby seals in the face",
                            Duration = TimeSpan.FromSeconds(29),
                            TrackNumber = 1,
                            AlbumReleaseDate = DateTime.Now,
                            GenreName = "Podcasts"
                        },
                        SaveSongOperation.CopyToLibrary);
}

private async void CopyFileIntoIsoStore(StorageFile sourceFile)
{
    using (var s = await sourceFile.OpenReadAsync())
    using (var dr = new DataReader(s))
    using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())
    using (var targetFile = isoStore.CreateFile(sourceFile.Name))
    {
        var data = new byte[s.Size];
        await dr.LoadAsync((uint) s.Size);
        dr.ReadBytes(data);
        targetFile.Write(data, 0, data.Length);
    }
}

Note that we had to save a file in IsoStore to use this API. Also note that the Uri isn't well-formed or in a standard IsoStore Uri. It's just the file name.

When we run this code snippet we can see the following:

Artist list with custom artistAlbum list with custom artistAlbum view for custom artistplaying a custom song

like image 51
JustinAngel Avatar answered Nov 02 '22 08:11

JustinAngel


There are no default means of accessing the Zune API. You can do this via undocumented ways (native layer), but that will ultimately get your application rejected from the Marketplace.

like image 30
Den Delimarsky Avatar answered Nov 02 '22 08:11

Den Delimarsky