Well, going through pain is just part of a new platform. Unfortunately for MonoTouch there isn’t a huge community like there is for C# in general. This makes the pain greater because you need to fart around trying to figure out things more. But, I have finally figured out 1 of the cryptic things that has been blocking me in my iPhone development…
MPMediaItem. It lets you get information about, well, a media item. (I so hate the word “item” as it’s just overused.) You get an MPMediaItem from an MPMusicPlayerController like this:
1: MPMusicPlayerController _musicPlayer;
2: _musicPlayer = new MPMusicPlayerController();
3: // Stuff to load and play the song...
4: // Get it like this:
5: MPMediaItem item = _musicPlayer.NowPlayingItem;
Now, the signature to get something from the MPMediaItem is:
MPMediaItem.ValueForProperty(string property)
So, naturally I expected to pass in a string… Wrong. You pass in an MPMediaItemProperty enum. A string will just break. This is how you do it:
1: NSString artist = (NSString)_musicPlayer.NowPlayingItem.ValueForProperty(MPMediaItemProperty.Artist);
2: NSUrl url = (NSUrl)_musicPlayer.NowPlayingItem.ValueForProperty(MPMediaItemProperty.AssetUrl);
That shows getting an artist and the original URL (path) for the file. You get at those values something like this:
1: txtShowYou.Text += "\n" + artist.ToString();
2: txtShowYou.Text += "\n" + url.AbsoluteString;
And so there you have it. That’s how to get media meta data out of an MPMediaItem in C# and MonoTouch. Hope that helps someone.
Like this:
Be the first to like this post.