Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在iOS中打开洗牌模式时,NowPlayingItem的索引错误_Ios_Cocoa Touch_Shuffle_Mpmediaitem - Fatal编程技术网

在iOS中打开洗牌模式时,NowPlayingItem的索引错误

在iOS中打开洗牌模式时,NowPlayingItem的索引错误,ios,cocoa-touch,shuffle,mpmediaitem,Ios,Cocoa Touch,Shuffle,Mpmediaitem,我现在正在播放iPod库中的歌曲,该歌曲通过iPodMusicLayer加载到myArray中 我们可以使用indexOfNowPlayingItem从正在播放的音乐中获取索引 但是当iShuffle Mode处于打开状态时,indexOfNowPlayingItem属性的返回索引是完全错误的 在禁用ShuffleMode之前,可以使用并更正indexOfNowPlayingItem 但是,当ShuffleMode处于启用状态时,indexOfNowPlayingItem计数仅增加1(++) 那

我现在正在播放iPod库中的歌曲,该歌曲通过
iPodMusicLayer
加载到myArray中

我们可以使用
indexOfNowPlayingItem
从正在播放的音乐中获取索引

但是当i
Shuffle Mode
处于
打开状态时,
indexOfNowPlayingItem
属性的返回索引是完全错误的

在禁用
ShuffleMode
之前,可以使用并更正
indexOfNowPlayingItem

但是,当
ShuffleMode
处于启用状态时,indexOfNowPlayingItem计数仅增加1(++)

那样

indexOfNowPlayingItem++;
对于启用的
ShuffleMode
,此选项不正确

那么,当ShuffleMode打开时,如何获得正确的索引呢


谢谢您的帮助。

我的解决方案是,当您设置播放列表时,还要设置一个包含播放列表(index,mpmediateEntityPropertyTypersistentId)的NSMutableDictionary

当您需要歌曲索引时,只需使用歌曲持久id即可

- (void) setPlaylistIndexDictionary:(MPMediaItemCollection *)playlistItems
{
    playlistIndexDict = [[NSMutableDictionary alloc] init];
    NSNumber *count = [[NSNumber alloc] initWithInt:0];
    for (MPMediaItem *item in [playlistItems items]) {
        [playlistIndexDict setObject:count forKey:[item valueForProperty:MPMediaEntityPropertyPersistentID]];
        count = [NSNumber numberWithInt:count.intValue + 1];
    }
}


- (NSString *) getCurrentSongId
{
    NSString* songId = [[musicPlayer nowPlayingItem] valueForProperty:MPMediaEntityPropertyPersistentID];

    return songId;
}
使用:

NSString *songId = [musicController getCurrentSongId];
int songIndex = [[[musicController playlistIndexDict] objectForKey:songId] intValue];

彼此彼此。。。。你解决了这个问题吗?任何帮助都会非常感激的。我找不到任何解决办法(