2013-01-10 65 views
2

我在NSArray中播放了歌曲列表,並在我的UITableView中顯示歌曲,如下圖所示。在iOS中使用MPMusicPlayerController從播放列表中播放和跳過下一首歌曲

enter image description here

對我來說,當我選擇從UITableView一首歌曲,我想打這首歌與MPMusicPlayerController'sapplicationMusicPlayer

我的意思是,當我從UITableView選擇美國白癡,我想打美國白癡與MPMusicPlayerController's

當我點擊下面的跳過按鈕,它必須像郊區的耶穌播放下一首歌曲。

這裏是我的代碼,加載歌曲的UITableView

self.player = [MPMusicPlayerController applicationMusicPlayer]; 

    MPMediaQuery *query = [MPMediaQuery playlistsQuery]; 
    MPMediaPredicate *predicate = [MPMediaPropertyPredicate predicateWithValue:@"GreenDay" forProperty:MPMediaPlaylistPropertyName comparisonType:MPMediaPredicateComparisonContains]; 

    [query addFilterPredicate:predicate]; 

    self.arrayOfSongs = [query items]; 

我想你明白我的意思。 我想做所有的音樂按鈕工作,如iOS的音樂應用程序內建和播放歌曲,當我從UITableView選擇歌曲。

我正在嘗試,但我沒有找到可以解決我的問題的解決方案。

請幫我這樣做。

謝謝你。 :)

+0

我們可以從iPod音樂中選取播放列表嗎? – SRI

回答

2

假設您的NSArray使用MPMediaItemCollection中的MPMediaItems填充,這實際上很簡單,只要您知道需要配置什麼即可。首先,創建一個iVar,最好是NSUInteger來存儲當前正在播放的曲目的索引。這將有必要從一個軌道到另一個軌道。第二,很難從你的發佈者那裏得知曲目標題是從集合中的媒體項目讀取的,還是隻是靜態地放在桌子上,但我已經包含了一個如何從陣列中的媒體項目讀取曲目標題,並使用valueForProperty將該值設置爲單元格文本。

最後,在下面配置didSelectRowAtIndexPath以演示您已創建的無符號整數與所選單元格的值。 didSelectRowAtIndexPath和我創建的兩個IBActions都修改了此索引的值,然後調用void創建了「stopCurrentTrackAndPlaySelection」,它停止當前播放的音軌,將MPMediaItem類型轉換爲該索引處的對象,然後再次開始播放。

如果你需要更多的澄清,只是問:)

附註:我建議你在一個NSMutableArray的,而不是NSArray的,或者MPMediaItemCollection直接存儲媒體採摘選擇(MPMediaItemCollection)的副本。這將允許您即時添加或刪除曲目,而無需停止播放。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [myArrayOfTracks count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    [[cell textLabel] setText:[(MPMediaItem *)[myArrayOfTracks objectAtIndex:indexPath.row] valueForProperty:MPMediaItemPropertyTitle]]; 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    indexOfCurrentlyPlayingTrack = indexPath.row; 
    [self stopCurrentTrackAndPlaySelection]; 
} 

- (IBAction)nextTrack:(UIButton *)sender 
{ 
    indexOfCurrentlyPlayingTrack ++; 
    [self stopCurrentTrackAndPlaySelection]; 
} 

- (IBAction)previousTrack:(UIButton *)sender 
{ 
    indexOfCurrentlyPlayingTrack --; 
    [self stopCurrentTrackAndPlaySelection]; 
} 

- (void)stopCurrentTrackAndPlaySelection 
{ 
    [myMPMusicPlayerController stop]; 
    [myMPMusicPlayerController setNowPlayingItem:(MPMediaItem *)[myArrayOfTracks objectAtIndex:indexOfCurrentlyPlayingTrack]]; 
    [myMPMusicPlayerController play]; 
} 
+0

謝謝你的回答。我正在測試你的代碼。 但是,當我從UItableView中選擇歌曲時,它不會播放任何內容。我該怎麼做那個兄弟? –

+0

@Onigiri我提供的代碼演示瞭如何做到這一點,以及下一個/後退功能。 –

+0

我編輯了我的代碼兄弟。請檢查我錯在哪裏? –

相關問題