MPMediaPlayer indexOfNowPlayingItem给出当前索引9223372036854775807

时间:2015-09-11 14:45:01

标签: ios objective-c uitableview mpmediaplayercontroller

我正在尝试创建一个音乐播放器,这是我将NSLog显示当前索引的地方:

- (void) handle_NowPlayingItemChanged: (id) notification
{
    MPMediaItem *currentItem = [musicPlayer nowPlayingItem];
    NSUInteger currentIndex = [musicPlayer indexOfNowPlayingItem];
    UIImage *artworkImage = [UIImage imageNamed:@"NoSongImage.png"];
    MPMediaItemArtwork *artwork = [currentItem valueForProperty:MPMediaItemPropertyArtwork];

if (artwork) {
    artworkImage = [artwork imageWithSize: CGSizeMake (200, 200)];
}

[self.artwork setImage:artworkImage];

NSString *titleString = [currentItem valueForProperty:MPMediaItemPropertyTitle];
if (titleString) {
    self.songName.text = [NSString stringWithFormat:@"%@",titleString];
} else {
    self.songName.text = @"Unknown title";
}

    NSLog(@"%li", currentIndex);
}

这是我didSelectRowAtIndexPath的{​​{1}}:

UITableView

BOOLS确定要加载哪个表-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ if (songsList == YES){ NSUInteger selectedIndex = [[self.tableView indexPathForSelectedRow] row]; MPMediaItem *selectedItem = [[songs objectAtIndex:selectedIndex] representativeItem]; [musicPlayer setQueueWithItemCollection:[MPMediaItemCollection collectionWithItems:[songsQuery items]]]; [musicPlayer setNowPlayingItem:selectedItem]; [musicPlayer play]; songsList = NO; } else if (albumsList == YES && albumsSongsList == NO){ albumsSongsList = YES; NSUInteger selectedIndex = [[self.tableView indexPathForSelectedRow] row]; MPMediaItem *selectedItem = [[albums objectAtIndex:selectedIndex] representativeItem]; albumTitle = [selectedItem valueForProperty:MPMediaItemPropertyAlbumTitle]; MPMediaItemArtwork *albumArtwork = [selectedItem valueForProperty:MPMediaItemPropertyArtwork]; albumSelected = [albumArtwork imageWithSize: CGSizeMake (44, 44)]; [self.tableView reloadData]; [self.tableView setContentOffset:CGPointZero animated:NO]; } else if (albumsSongsList == YES){ albumsSongsList = NO; MPMediaQuery *albumQuery = [MPMediaQuery albumsQuery]; MPMediaPropertyPredicate *albumPredicate = [MPMediaPropertyPredicate predicateWithValue: albumTitle forProperty: MPMediaItemPropertyAlbumTitle]; [albumQuery addFilterPredicate:albumPredicate]; NSArray *albumTracks = [albumQuery items]; NSUInteger selectedIndex = [[self.tableView indexPathForSelectedRow] row]; MPMediaItem *selectedItem = [[albumTracks objectAtIndex:selectedIndex] representativeItem]; [musicPlayer setQueueWithItemCollection:[MPMediaItemCollection collectionWithItems:[albumQuery items]]]; [musicPlayer setNowPlayingItem:selectedItem]; [musicPlayer play]; } } 。 以上都正确显示tableView willDisplayCell正确的内容。 问题是,当我打开专辑并选择一首歌时,它似乎会播放专辑的第一首歌(位于UITableView的顶部)。如果我再次返回相册并选择其他歌曲,则会选择正确的歌曲。 然后,如果我再次回去并选择一首歌曲,那么第一首歌曲将再次播放并重复播放......

在我的NSLog中,由于tableView currentIndex

而播放第一首歌的原因

为什么要归还9223372036854775807NSNotFound(例如selectedIndex)位于4数组中。 这也是歌曲的1/3倍。 非常感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

64位系统上NSNotFound的价值是多少?什么是2 ^ 63 - 1?

答案 1 :(得分:0)

在设置nowPlayingItem之前尝试暂停或停止音乐播放器。有时Shuffle模式也可以改变索引,尝试将其关闭。