我正在尝试创建一个音乐播放器,这是我将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
为什么要归还9223372036854775807
? NSNotFound
(例如selectedIndex
)位于4
数组中。
这也是歌曲的1/3倍。
非常感谢您的帮助。
答案 0 :(得分:1)
64位系统上NSNotFound的价值是多少?什么是2 ^ 63 - 1?
答案 1 :(得分:0)
在设置nowPlayingItem之前尝试暂停或停止音乐播放器。有时Shuffle模式也可以改变索引,尝试将其关闭。