iOS - get programmatically queue of items currently playing

我与影子孤独终老i 提交于 2020-01-02 01:11:09

问题


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?


回答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.



来源:https://stackoverflow.com/questions/10092589/ios-get-programmatically-queue-of-items-currently-playing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!