2013-09-28 4 views
6

Sto riproducendo un video in UITableViewCell. Per questo sto usando il seguente codice:Come riprodurre un video (riproduzione automatica) in UITableViewCell in 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; 
} 

la riproduzione del video correttamente, ma voglio giocare un solo video alla volta. Riproduci il video della cella che è completamente visibile.

risposta

-4

Dovresti scrivere il tuo codice di gioco nel metodo didselect così come selezioni la riga non suonerà solo altro.

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

Non voglio fare sull'evento click, voglio riprodurre automaticamente il video quando la cella è visibile. – San007

+0

Quindi se vuoi giocare in quel modo non dovresti scrivere il tuo codice in cellforrowatindexpath e scriverlo in viewWillAppear e prendere il ciclo per array e riprodurlo dall'indice dell'array. Ottieni il tempo pieno del brano e, una volta completato, carica il brano successivo da quell'array. Usa la tabella solo per scopi di visualizzazione con una certa logica che suona la canzone. – user2826529

+1

Non hai problemi, grazie per il tuo impegno. – San007

3

Utilizzare questo metodo di scorrimento e gestione del video. Questo metodo due chiamerà in un modo o nell'altro quando tableview interrompe lo scorrimento

-(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

che cos'è il metodo converRect? –