Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - get programmatically queue of items currently playing

I want to get programmatically queue currently played in native Music App. I can use MPMusicPlayerController to get currently playing item but I want to get not only the item but whole playing queue. Is it possible to do it using AVFoundation or any other library?

like image 513
Martin Pilch Avatar asked Apr 10 '12 16:04

Martin Pilch


3 Answers

I'm pretty sure this is not possible through any public API. The Ecoute app that @sooper mentions must be using private APIs. I did a little experiment in the codebase of my own music app. First I used this code to list all the methods in the iPod music player (put #import <objc/runtime.h> at the top):

int i=0;
unsigned int mc = 0;
Method * mlist = class_copyMethodList([MPMusicPlayerController iPodMusicPlayer].class, &mc);
NSLog(@"%d methods for class", mc);
for(i=0;i<mc;i++) {
    NSLog(@"\tMethod no #%d: %s", i, sel_getName(method_getName(mlist[i])));
}
free(mlist);

This turned up some intriguing method names like numberOfItems and nowPlayingItemAtIndex:. So I added this category at the top of the file:

@interface MPMusicPlayerController (Private)

- (NSInteger)numberOfItems;
- (MPMediaItem*)nowPlayingItemAtIndex:(NSInteger)index;

@end

and I ran this code:

NSInteger numberOfItems = [[MPMusicPlayerController iPodMusicPlayer] numberOfItems];
for (NSInteger i = 0; i < numberOfItems; i++) {
    MPMediaItem* mi = [[MPMusicPlayerController iPodMusicPlayer] nowPlayingItemAtIndex:i];
    NSLog(@"%@", [mi valueForProperty:MPMediaItemPropertyTitle]);
}

and sure enough, it printed out the playlist that I had queued up in the Music app!

Of course, if you call these methods this way, Apple will reject your app, but there's a way to hide private API calls from Apple.

like image 99
bugloaf Avatar answered Oct 31 '22 17:10

bugloaf


if you can to use Apple private api. this should be best.

let player = MPMusicPlayerController.systemMusicPlayer
let items = (player.value(forKey: "queueAsQuery") as! MPMediaQuery).items
//[MPMediaItem]?
like image 41
大渕雄生 Avatar answered Oct 31 '22 16:10

大渕雄生


I'm afraid this is not possible. Apple does not give us access to this information from any libraries.

like image 2
amergin Avatar answered Oct 31 '22 16:10

amergin