Stephen Starkie Stephen Starkie - 28 days ago 15
C# Question

Xamarin.iOS: MPMusicPlayerController.NowPlayingItem property disappears

I am trying to write a Xamarin.Forms-based app for playing music. I am using the standard Xamarin Forms Dependency Service pattern to create a platform-specific player for iOS which uses the MPMusicPlayerController as documented by Xamarin. When I write the code the MPMusicPlayerController.NowPlayItem property is accessible;

NowPlayingItem is accessible

and when I first tested it everything worked OK. I then started trying to listen to notifications from the MPMusicPlayerController and it 'disappeared'; when I debug, the property is missing and when I try to access it I get a deep mono SIGSEGV exception;

Property has disappeared

Quite often the debugger tries to reference the property and also crashes. So; I took out the notifications and it still happens!

Answer

I recreated the part of the code causing the problem in Xcode and Objective C. Though looking at the Device log in Xcode after running and crashing from Xamarin would have shown the problem as well.

More recent versions of iOS expect an Info.plist entry; Privacy - Apple Music Usage Description to be provided for anything that accesses the Media Library. Interestingly it seems that playback is allowed and it feels like the NowPlayingItem property shouldn't be allowed but somehow, sometimes is.

In the end, I found I had to put the plist entry in and also had to check for and sometimes ask for authorisation as in this post.

Once I had done both of these, the NowPlayingItem property works and I get notifications.

Comments