2014-10-19 60 views
1

我在我的应用程序中有一个播放列表屏幕,用户可以选择播放列表中的任何歌曲,应用程序将从该歌曲开始播放。如何使用MPMusicPlayerController从特定索引播放MPMediaItemCollection?

我使用下面的代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    MPMediaItem *selectedSong = [currentQueue.items objectAtIndex:indexPath.row]; 
    [audioCoordinator.musicController setNowPlayingItem: selectedSong]; 
    [audioCoordinator playAllSound]; 
} 

它与所有不同歌曲的播放列表效果很好,但清单,因为这允许重复的歌曲,来这里的问题。对于这个播放列表:

  1. 很久很久以前
  2. 我的十二月
  3. 很久很久以前

当用户点击第3首歌曲,应用程序将发挥第一首歌,因为他们有相同的名字,这不是正确的行为。

我们如何提供MPMusicPlayerController的setNowPlayingItem函数一个索引,以便它可以从那里开始播放?

回答

1

找到了解决办法:

在功能playAllSound:

while (self.musicController.indexOfNowPlayingItem != self.currentSongIndex) { 
    [self.musicController skipToNextItem]; 
} 

在这里,self.currentSongIndex具有与indexPath.row相同的值,因为tableViewController的didSelectCell函数中有一行audioCoordinator.currentSongIndex = indexPath.row

0

它是可见的,关于indexPath.row selectedSong基地,登录indexPath.row您检查playAllSound我认为它shud B播放

+0

对不起,我不明白你想说什么,但你的“日志indexPath.row和检查playAllSound”启发我,我找到了解决方案,所以谢谢。 – 2014-10-19 19:37:19

+0

我的意思是说,我们选择了正确的歌曲,并参考了单元格,但是您使用的是playAllSound,在我看来,它会播放一首歌 – ZAZ 2014-10-19 19:40:17

+0

对不起,playAllSound是一个可以播放一首歌的函数歌曲,但它叫“AllSong”,因为它不仅可以播放歌曲,还可以同时播放其他歌曲。 – 2014-10-19 19:45:19

相关问题