1

我试图找出 MPMusicPlayerController 在其播放列表中的 nowPlayingItem 的曲目编号。这将非常简单,除了同一首歌曲可以在同一个播放列表中出现多次(因为它们都指向同一个文件)。例如,如果一个播放列表由 Song 1、Song 2、Song 1、Song 3、Song 2 组成,我会纠结于如何确定 nowPlayingItem 是播放列表中的第一个项目还是第三个项目。

我最接近解决问题的方法是为 MPMediaItem 创建一个类别,该类别添加一个关联对象(一个 NSNumber),其中包含一个“播放列表中的数字”值和一个 MPMusicPlayerController 类别,该类别为之前播放列表中的每个 MPMediaItem 设置该值将其添加到队列中。

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“属性”的分配。但是,当我尝试获取 nowPlayingItem MPMediaItem 时,numberInPlaylist 值始终返回 (null)。实际上有可能实现我想要做的事情吗?在这一点上,我慢慢失去了它可以做到的信念。

4

1 回答 1

2

MPMusicPlayerController 中有一个名为 indexOfNowPlayingItem 的隐藏属性。不幸的是,我不能使用它,因为 Apple 会拒绝我的应用程序使用私有财产/方法。

于 2010-09-24T19:16:20.790 回答