2013-01-10 7 views
2

재생 목록 노래가 NSArray이고 다음 음악이 내 UITableView에 다음 그림과 같이 표시됩니다. 내가 UITableView에서 한 곡을 선택할 때 내 경우ios에서 MPMusicPlayerController로 재생 목록에서 노래 재생

enter image description here

은 내가 MPMusicPlayerController'sapplicationMusicPlayer와 함께 그 노래를 재생합니다.

나는 내가 UITableView에서 미국 바보을 선택할 때, 내가 MPMusicPlayerController's

과 미국의 바보를 재생하려는 그리고 내가 건너 뛰기 버튼을 아래로 누를 때, 그것은 교외의 예수와 같은 놀이 다음 노래해야 뜻.

여기 난 당신이 무슨 뜻인지 이해 생각

self.player = [MPMusicPlayerController applicationMusicPlayer]; 

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

    [query addFilterPredicate:predicate]; 

    self.arrayOfSongs = [query items]; 

를 jQuery과 노래를 불러 내 ​​코드입니다. 음악 응용 프로그램에서 iOS 빌드와 같은 모든 음악 단추 작업을 수행하고 노래를 선택할 때 노래를 선택하려면 UITableView에서 노래를 선택하고 싶습니다.

나는 anyting을 시도하고 있지만 내 문제를 해결할 수있는 해결책을 찾지 못했습니다.

저를 그렇게 도와주세요.

감사합니다. :)

+0

iPod 음악에서 선택한 재생 목록을 가져올 수 있습니까? – SRI

답변

2

NSArray가 MPMediaItemCollection의 MPMediaItems로 채워 졌다고 가정하면 구성해야 할 항목을 알면 실제로는 매우 간단합니다. 먼저 현재 재생중인 트랙의 인덱스를 저장하기 위해 iVar, NSUInteger를 생성하십시오. 이것은 하나의 트랙에서 다른 트랙으로 순서대로 갈 필요가 있습니다.

둘째, 트랙 제목이 컬렉션의 미디어 항목에서 읽히거나 게시물이 테이블에 정적으로 배치되어있는 경우 게시자에게 알리기는 어렵지만 다음과 같은 방법의 예가 포함되어 있습니다. 배열의 미디어 항목에서 트랙 제목을 읽고 valueForProperty을 사용하여 해당 값을 셀 텍스트로 설정하십시오.

그리고 마지막으로, didSelectRowAtIndexPath은 이미 생성 된 부호없는 정수를 선택한 셀의 값으로 보여주기 위해 아래에 구성됩니다. didSelectRowAtIndexPath과 내가 만든 두 개의 IBActions 모두이 인덱스의 값을 수정 한 다음 현재 재생중인 트랙을 중지하고 해당 인덱스에있는 개체에 대한 형식 변환 된 MPMediaItem을 다시 만든 다음 "stopCurrentTrackAndPlaySelection"을 만든 void를 호출 한 다음 다시 재생을 시작합니다 .

그냥 :)

사이드 메모를 물어 더 설명이 필요한 경우 : 내가 추천을 당신이 직접하는 대신있는 NSArray의 NSMutableArray를, 또는 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

내 코드 형제를 편집했습니다. 내가 잘못한 곳을 확인해 주시겠습니까? –