I am trying to receive information about the currently played track in a iOS app. This works pretty fine while the iPhone is not connected to an accessory. If I connect it to my car (Opel Astra, iPhone jack), the following code stops to work as described in the documentation:
If you create an iPod music player and the user plays an item from another library using Home Sharing, the value of this property is
nil
.
Code:
// nil while connected to an accessory MPMediaItem *nowPlayingMediaItem = [[MPMusicPlayerController iPodMusicPlayer] nowPlayingItem]; // Works while not connected to an accessory NSString *title = [nowPlayingMediaItem valueForProperty:MPMediaItemPropertyTitle];
I even tried "hacky" stuff like to access "private" properties (original code):
MPMediaQuery *query=nil; MPMediaItemCollection *collection=nil; id internalPlayer=nil; Ivar internalPlayeriVar = object_getInstanceVariable(iPod, "_internal", NULL); internalPlayer = object_getIvar(iPod, internalPlayeriVar); NSLog(@"internalPlayer: %@", internalPlayer); Ivar queryIvar = object_getInstanceVariable(internalPlayer, "_query", NULL); query = object_getIvar(internalPlayer, queryIvar); // nil everytime Ivar collectionIvar = object_getInstanceVariable(internalPlayer, "_itemCollection", NULL); collection = object_getIvar(internalPlayer, collectionIvar); // nil everytime
or to call private methods:
// Same behaviour like [iPod nowPlayingItem], works // only while no accessory is connected MPMediaItem *nowPlayingMediaItem = [iPod nowPlayingItemAtIndex:[iPod indexOfNowPlayingItem]]; // Works while not connected to an accessory NSString *title = [nowPlayingMediaItem valueForProperty:MPMediaItemPropertyTitle];
Its also no solution to access the new MPNowPlayingInfoCenter
, its nil
all the time.
[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo
My car plays my music directly without using a iPhone app and it seems my iPhone knows what the car is currently playing because it displays the title, artist and cover icon on the lock screen (and only there). Also the internal play count gets increased.
If I check the playback state, it returns also YES
if the car plays music:
[[MPMusicPlayerController iPodMusicPlayer] playbackState] == MPMusicPlaybackStatePlaying
So, is there any way (may be through calling private methods) to access the song, the car is currently playing?
Swipe up from the bottom bezel onto the screen to bring up Control Center. Swipe from right to left to get to the Now Playing panel, if you're not there already. Tap the cover art or media description to go straight to the app that's playing it.
If you're using an Android device, you can download the Tracker Detect app to find an AirTag or Find My network accessory that's separated from its owner and might be traveling with you.
However, Tracker Detect currently only operates when the app is open. Anyone who is alerted to the presence of an unknown AirTag, either through Apple's notification system or by using Tracker Detect, can trigger an audible chime to help them locate the device.
The Health app gathers health data from your iPhone, Apple Watch, and apps that you already use, so you can view all your progress in one convenient place. Health automatically counts your steps, walking, and running distances. And, if you have an Apple Watch, it automatically tracks your Activity data.
Are you using threads? If so run the code on the main thread. If not then register for the MPMusicPlayerController notifications for item change. That way when the song changes your app will know what the new song is. Also be sure this runs on the main thread as well.
If your playback state is updating while connected but your nowPlayingItem isn't, this would confirm it is a bug. I would submit a bug report for this issue.
EDIT: Visit https://developer.apple.com/support/resources/bug-reporting.html and scroll to the bottom. The last question says you can contact TSI for bug work arounds. You get 2 TSI requests for being a developer for free so you can use one of those to ask them if they have a work around using a private library until the bug is fixed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With