2013-09-28 9 views
6

Odtwarzam wideo w wersji UITableViewCell. Do tego używam następującego kodu:Jak odtwarzać wideo (autoodtwarzanie) na UITableViewCell w iOS

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *VideoCellIdentifier = @"VideoCell"; 

    NSDictionary *_response_data = [self.response objectAtIndex:indexPath.row]; 

    VideoCustomCell *cell = (VideoCustomCell *) [tableView dequeueReusableCellWithIdentifier:VideoCellIdentifier]; 

    if (cell == nil) { 
     NSArray *topLevelObjects; 
     topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"VideoCustomCell" owner:self options:nil]; 
     for (id currentObject in topLevelObjects){ 
      if ([currentObject isKindOfClass:[UITableViewCell class]]){ 
       cell = (VideoCustomCell *) currentObject; 
       cell.delegate = self; 
       break; 
      } 
     } 
    } 

    avPlayer = [[AVPlayer playerWithURL:[NSURL URLWithString:[_response_data valueForKey:@"media_id"]]] retain]; 
    avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayer]; 
    avPlayerLayer.frame = cell.video_player_view.layer.bounds; 
    avPlayerLayer.videoGravity = AVLayerVideoGravityResize; 
    [cell.video_player_view.layer addSublayer: avPlayerLayer]; 
    [avPlayer play]; 

    return cell; 
} 

Film jest odtwarzany poprawnie, ale chcę odtwarzać tylko jeden film na raz. Odtwórz wideo z komórki, która jest w pełni widoczna.

Odpowiedz

-4

Powinieneś napisać swój kod gry w metodzie didselect, tak aby wybrać rząd, który będzie odtwarzany nie tylko.

(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // your player code 
} 
+0

Nie chcę robić w przypadku kliknięcia, chcę automatycznie odtwarzać wideo, gdy komórka jest widoczna. – San007

+0

Więc jeśli chcesz grać w ten sposób, nie powinieneś pisać swojego kodu w cellforrowindindexpath i pisać go w viewWillAppear i wziąć pętlę for array i odtworzyć ją z indeksu tablicy. Uzyskaj pełny czas utworu i po jego zakończeniu załaduj następną piosenkę z tej tablicy. Używaj tabeli tylko do celów wyświetlania z pewną logiką, w którą grasz. – user2826529

+1

Nie dostaniesz mojego problemu, dziękuję za wysiłek. – San007

3

Użyj dwóch metod przewijania i obsługi wideo odtwarzania. Te dwie metody wywołają w dowolny sposób, gdy zatrzymasz przewijanie widoku tabeli

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { 
    if(![scrollView isDecelerating] && ![scrollView isDragging]){ 

     [self playVideo]; 
    } 
} 

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{ 
    if(!decelerate){ 

     [self playVideo]; 
    } 
} 


-(void)playVideo 
{ 
    if(aryTableData.count==0){ 
     return; 
    } 

    for(UITableViewCell *cell in [tblView visibleCells]) 
    { 
     VideoCell *ccell = (VideoCell*)cell; 

     CGRect ccellRect = [APP_DEL.window convertRect:ccell.bounds fromView:ccell]; 

     // NSLog(@"--Cell frame %f",ccellRect.origin.y); 

     //Set Condition of cell visible within some range 
     if(ccellRect.origin.y>-200) 
     { 
      // Handle Video Play 

      int row = [[tblView indexPathForCell:ccell] row]; 
      NSString *strUrl = [[aryTableData objectAtIndex:row] valueForKey:@"video_url"] ; 
      [ccell startVideoFromURL:strUrl]; //You can handle video play in cell or table view 
     } 
    } 
} 
+0

co to jest metoda ConverRect? –