我正在尝试制作一个音乐播放器,这是我放置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
的UITableView
:
-(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 中,它播放第一首歌的原因currentIndex
是9223372036854775807
为什么会回来NSNotFound
?(selectedIndex
例如4
)在数组中albumTracks
。这也发生在歌曲的 1/3 次。非常感激你的帮助。