我正在尝试在其播放列表中找出MPMusicPlayerController的nowPlayingItem的曲目编号。这将是超级简单的,除了相同的歌曲可以在同一播放列表中多次出现的事实(因为它们都指向同一个文件)。例如,如果播放列表由歌曲1,歌曲2,歌曲1,歌曲3,歌曲2组成,我就会坚持如何确定nowPlayingItem是否是播放列表中的第一个或第三个项目。 / p>
我最接近解决问题的方法是为MPMediaItem创建一个类别,它添加一个包含“播放列表中的数字”值的关联对象(NSNumber)和一个为每个MPMediaItem设置该值的MPMusicPlayerController的类别。将播放列表添加到队列之前的播放列表。
MPMediaItem + playlistNumber.h
#import <Foundation/Foundation.h>
#import <MediaPlayer/MediaPlayer.h>
#import <objc/runtime.h>
@interface MPMediaItem (playlistNumber)
-(NSNumber*)numberInPlaylist;
-(void)setNumberInPlaylist:(NSNumber *)number;
@end
MPMediaItem + playlistNumber.m
@implementation MPMediaItem (playlistNumber)
static NSString * numberInPlaylistKey;
-(NSNumber*)numberInPlaylist{
return objc_getAssociatedObject(self, &numberInPlaylistKey);
}
-(void)setNumberInPlaylist:(NSNumber *)number{
objc_setAssociatedObject(self, &numberInPlaylistKey, number, OBJC_ASSOCIATION_RETAIN);
}
@end
MPMusicPlayerController + NumberTracks.h
#import <Foundation/Foundation.h>
#import "MPMediaItem+PlaylistNumber.h"
#include <objc/runtime.h>
@interface MPMusicPlayerController (NumberTracks)
-(void)setQueueAndAddNumbersToItemCollection:(MPMediaItemCollection*)collection;
@end
MPMusicPlayerController + NumberTracks.m
#import "MPMusicPlayerController+NumberTracks.h"
@implementation MPMusicPlayerController (NumberTracks)
-(void)setQueueAndAddNumbersToItemCollection:(MPMediaItemCollection*)collection{
/*
NSEnumerator *enumerator = [[collection items] objectEnumerator];
MPMediaItem * object;
int numberInPlaylist = 1;
while (object = [enumerator nextObject]) {
NSNumber * number = [[NSNumber alloc] initWithInt:numberInPlaylist];
[object setNumberInPlaylist:number];
NSLog(@"%@", [object numberInPlaylist]);
NSLog(@"%@", [[[collection items] objectAtIndex:numberInPlaylist-1] numberInPlaylist]);
NSLog(@"%@", [[[collection items] objectAtIndex:numberInPlaylist-1] valueForProperty:MPMediaItemPropertyTitle]);
numberInPlaylist++;
}
*/
/*
for (int numberInPlaylist = 1; numberInPlaylist <= collection.count; numberInPlaylist++) {
NSNumber * number = [[NSNumber alloc] initWithInt:numberInPlaylist];
[[[collection items] objectAtIndex:numberInPlaylist-1] setNumberInPlaylist:number];
NSLog(@"%@", [[[collection items] objectAtIndex:numberInPlaylist-1] numberInPlaylist]);
NSLog(@"%@", [[[collection items] objectAtIndex:numberInPlaylist-1] valueForProperty:MPMediaItemPropertyTitle]);
[number release];
}
*/
NSArray * items = [collection items];
NSUInteger number = 1;
for (MPMediaItem * item in items) {
[item setNumberInPlaylist:[NSNumber numberWithInt: number++]];
NSLog(@"%@", item.numberInPlaylist);
}
[self setQueueWithItemCollection:collection];
[self play];
[self pause];
[self skipToBeginning];
//The play/pause/skipToBeginnng is so that the nowPlayingItem is actually the first item in the queue
}
@end
在NumberTracks类别实现中,我尝试了3种不同的方式(两个大的注释部分和一个大的未注释部分)来实现numberInPlaylist“property”的赋值。但是,当我尝试获取nowPlayingItem MPMediaItem时,numberInPlaylist值始终返回(null)。 它实际上可以实现我想要做的事情吗?在这一点上,我正逐渐失去信心。
答案 0 :(得分:2)
MPMusicPlayerController中有一个名为indexOfNowPlayingItem的隐藏属性。不幸的是,我无法使用它,因为Apple将拒绝我的应用程序使用私有财产/方法。