音乐播放器不播放所选项目

Music player does not play item selected

应用首次启动时,第一次选择任何歌曲时,应用从未播放实际选择的歌曲

由于某种原因,该应用程序将开始播放上次在“音乐”应用程序中播放的任何歌曲。即使我将选定的歌曲传递给它并且一切都在控制台中正常登录。

但此后一切正常,应用会播放所选歌曲。

虽然我不知道发生了什么,有什么想法吗?

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if([segue.identifier isEqualToString:@"nowPlaying"]){
        // send to now playing
        NSUInteger selectedSection = [[self.tableView indexPathForSelectedRow] section];
        NSUInteger selectedIndex = [[self.tableView indexPathForSelectedRow] row];        
        NSArray *albumTracksArray = [self albumTracksForSegue:[[albumsArrayForTVC objectAtIndex:selectedSection] representativeItem]];
        MPMediaItem *rowItemSong = [[albumTracksArray objectAtIndex:selectedIndex] representativeItem];
        MPMusicPlayerController *musicPlayer = [MPMusicPlayerController systemMusicPlayer];
        [musicPlayer setQueueWithItemCollection:[MPMediaItemCollection collectionWithItems:albumTracksArray]];

    if ([musicPlayer nowPlayingItem] == rowItemSong) {
        // Nothing
        NSLog(@"These songs are equivalent: %@", [musicPlayer nowPlayingItem]);
        [musicPlayer setNowPlayingItem:rowItemSong];
        [musicPlayer play];
        NSLog(@"Row Item Song Same: %@", rowItemSong);
    } else {
        [musicPlayer setNowPlayingItem:rowItemSong];
        [musicPlayer play];
        NSLog(@"Row Item Song Different: %@", rowItemSong);
        }
    }
}

在为音乐播放器分配队列时会出现一些有趣的边缘情况,它们因系统版本而异。一个经常有用的技巧是在你设置队列之后,调用

[musicPlayer setCurrentPlaybackTime: 0];

试一试,看看它是否适合你。