iOS - get programatically queue of items currently

2019-04-22 23:47发布

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

4条回答
闹够了就滚
2楼-- · 2019-04-23 00:27

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]?
查看更多
孤傲高冷的网名
3楼-- · 2019-04-23 00:31

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.

查看更多
smile是对你的礼貌
4楼-- · 2019-04-23 00:31

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.

查看更多
闹够了就滚
5楼-- · 2019-04-23 00:43

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

查看更多
登录 后发表回答