API to add playlists in Zune
Asked Answered
T

2

5

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)

Tribalism answered 8/12, 2010 at 9:19 Comment(2)
@Ozgur normally this is an invalid edit but caused by your bounty I aproved it.Edana
@KrishnaKumar I struggled this problem in my music player app, but with no success. If you not care about publish your app, you can use DLLImport to access restricted APIs #14029888Fiddler
M
13

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 artist Album list with custom artist Album view for custom artist playing a custom song

Mealy answered 2/1, 2013 at 5:30 Comment(4)
Unfortunately this is not the correct answer. What you are doing here is just adding song to SongCollection. My question is how to add into PlayList ("add to playlist"). I already reached this point before I started bounty for this question. Please revisit your answer if you have any suggestion for PlayListAllyson
As Dennis said, that's not possible in either WP7 or WP8. What is possible is to create a mock artist in the Music & Video hub that creates a pseudo-playlist.Mealy
JustinAngel, even though this is not the correct answer, thanks for your efforts. At least you have created a sample for copying song into media library.Allyson
Hi JustinAngel, any hint on what's the token meaning in GetPathFromToken(MediaLibrary library, String token)? if I want to reverse search of a given Song object from MediaLibrary and get its uri? (songs stored in device somewhere)Parish
L
1

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.

Liberty answered 9/4, 2011 at 3:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.