0

我正在尝试制作一个音乐播放器,这是我放置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);
}

这是我didSelectRowAtIndexPathUITableView

-(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];
    }
}

BOOLS 确定要tableView willDisplayCell加载到哪个表。以上都UITableView以正确的内容正确显示。问题是,当我打开专辑并选择一首歌曲时,它似乎播放专辑的第一首歌曲(在顶部tableView)。如果我再次返回专辑并选择不同的歌曲,则选择了正确的歌曲。再说一次,如果我再次返回并选择一首歌曲,第一首歌曲会再次播放并重复......

在我的 NSLog 中,它播放第一首歌的原因currentIndex9223372036854775807

为什么会回来NSNotFound?(selectedIndex例如4)在数组中albumTracks。这也发生在歌曲的 1/3 次。非常感激你的帮助。

4

2 回答 2

1

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

于 2015-09-11T14:47:52.293 回答
0

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

于 2015-09-17T15:05:54.937 回答