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

时间:2016-07-02 21:11:09

标签: ios objective-c mpmusicplayercontroller

初次启动应用时,第一次选择任何歌曲时,该应用永远不会播放所选的实际歌曲

由于某种原因,应用程序将开始播放音乐应用中最后播放的任何歌曲。即使我将所选歌曲传递给它,一切都在控制台中登录。

但是从那时起一切正常,应用程序会播放所选的歌曲。

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

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

1 个答案:

答案 0 :(得分:2)

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

[musicPlayer setCurrentPlaybackTime: 0];

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

相关问题