私は音楽プレーヤーを作ろうとしています、これは私が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 回発生します。よろしくお願いいたします。