我很难通过Xamarin.iOS获取iPhone的播放列表集合。我想用播放列表描述填充我的自定义对象列表。我在Apple文档中找到了一个关于如何在Obj-C中执行此操作的页面。但是,无论我在Xamarin中做什么,我都会得到无效的强制转换异常或其他错误。 将播放列表添加到集合中就可以了。但是,在问题出现之后,它会得到播放列表的名称。
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;
}
我的另一个变化如下(但在主题的迭代中再次收到无效的强制转换异常,即 mp1中的MediaPlaylist ):
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() });
}
正如我对代码进行了评论,我可以通过查询中的collections属性获取播放列表的句柄。但是,我无法获取播放列表的名称/标题/说明。这就是为什么我试图使用 MPMediaPlaylist 投射。
在即时窗口和观察窗口中,我尝试投射(MPMediaPlaylist)mq.collections [0] 但是再次获得了无效的强制转换异常。
I came across the following documentation in apple documentation for Obj-C但是我没有成功复制它,如上所示。如果有人能够关注我上面的内容以及这个Objective-C链接并提出建议,我将非常感激。
非常感谢,
托马斯
答案 0 :(得分:1)
我只想把它放在这里....如果有人在将来遇到这个问题!
因此,要获取播放列表列表,您应该执行以下操作:
MPMediaQuery playlistQuery = MPMediaQuery.PlaylistsQuery;
MPMediaItemCollection[] playlistCollection = playlistQuery.Collections;
foreach(MPMediaItemCollection collection in playlistCollection){
NSString key = MPMediaPlaylistProperty.Name;
string name = collection.ValueForKey(key).ToString();
//Do whatever you need with the info
}