Get list of playlists

TomasWinstonTomasWinston IEMember ✭✭

Hi,
I am having difficulty in obtaining a collection of playlists for the iPhone with Xamarin.iOS. I want to populate a list of my custom objects with the playlist descriptions. I found a page in Apple documentation on how to do it in Obj-C here
. However, whatever I do in Xamarin I either get invalid cast exceptions or some other error. Getting the playlists into a collection is OK. However, it is getting the name of the playlist after that where the problem lies.

private List<Playlist> GetPlaylists()
    {
        MPMediaQuery mq = MPMediaQuery.playlistsQuery;
        // At this point mq.Collections has a reference to the playlists. However I have no way of getting the Name/Description of the playlist. 
        // Thats why I was attempting to use MPMediaPlaylist
        // MPMediaPlaylist[] mp1 = mq.Collections as MPMediaPlaylist[]; - doesn't work - results in mp1 being null


        List<Playlist> playlists = (from p in mq.Collections
                                    select new Playlist () { PlaylistDescription = ((MPMediaPlaylist)p).ValueForProperty("MPMediaPlaylistPropertyName").ToString()}
                                   ).ToList();
        return playlists;
    }

Another variation I had was as follows (but again received an invalid cast exception in the iteration of the forearch i.e. MediaPlaylist in mp1):

        MPMediaQuery mq = MPMediaQuery.playlistsQuery;
        Array mp1 = mq.Collections as Array;
        List<Playlist> playlists = new List<Playlist> ();
        foreach (MPMediaPlaylist playlist in mp1) {
            playlists.Add (new Playlist () { PlaylistDescription = playlist.ValueForProperty ("MPMediaPlaylistPropertyName").ToString() });
        }

As I have commented i the code, I can get a handle on the playlists through the collections property on the query. However, I cannot get the Name/Title/Description of the playlist. That is why I was attempting to use the MPMediaPlaylist casting.

In the immediate window and watch window I tried to cast (MPMediaPlaylist)mq.collections[0] but again got an invalid cast exception.

As I said above, I came across the following documentation in apple documentation for Obj-C but I have been unsuccessful in reproducing it as you can see above. I'd be very grateful if someone could cast an eye over what I have above and also this Objective-C link and advise.

Many thanks,

Tomás

Answers

  • JakobDinesenJakobDinesen DKMember ✭✭

    Hello

    Have you solved the problem ?

    I am getting error even in executing this line

    MPMediaQuery mq = MPMediaQuery.playlistsQuery;

    Can you please help me?
    Thanks

  • TomasWinstonTomasWinston IEMember ✭✭

    Hi,

    This was somewhat of an experiment for me - which I never really completed but I do believe I at least managed to get the playlists returned. I am not in a position to test the following code at the moment to confirm it works but I am pretty sure it should do the trick. Try it and let me know how you get on.

                List<Playlist> IDevicePlaylistService.GetPlaylists()
                {
                    MPMediaQuery playlistQuery = MPMediaQuery.PlaylistsQuery;
                    MPMediaItemCollection[] playlistCollection = playlistQuery.Collections;
                    _playlists = new List<Playlist>();
                    foreach (MPMediaPlaylist playlist in playlistCollection)
                    {
                        NSString key = MPMediaPlaylistProperty.Name;
                        string name = playlist.ValueForKey(key).ToString();
                        _playlists.Add(new Playlist() { PlaylistName = name });
                    }
                    return _playlists.ToList();
                }
    

    T

Sign In or Register to comment.