iOS - get programatically queue of items currently

2019-04-23 00:05发布

问题:

I want to get programatically 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?

Thank you

回答1:

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



回答2:

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.



回答3:

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]?


回答4:

I've been looking into this and suddenly realized how simple it is!

- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection

mediaItemCollection is the current playlist!

hope this helps.