2017-09-15 75 views
0

我想在表格視圖單元格中顯示和播放視頻。我的代碼是: 在的cellForRowAtIndexPath:在UITableViewCell中播放視頻iOS

NSURL *videoURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",path,[[arrAboutUs objectAtIndex:indexPath.row] valueForKey:@"content"]]]; 
    MPMoviePlayerController* moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL]; 
    [moviePlayer setControlStyle:MPMovieControlStyleNone]; 
    moviePlayer.scalingMode = MPMovieScalingModeAspectFit; 
    [moviePlayer.view setFrame:cell.frame]; 
    [cell.contentView addSubview:moviePlayer.view]; 
    moviePlayer.view.hidden = NO; 
    [moviePlayer prepareToPlay]; 
    [moviePlayer play]; 

但它顯示空白。我的視頻是MP4格式的。

+0

看看下面這個例子https://github.com/Gagan5278/TableCellStreaming 是舊的,但應該與您合作。 –

+0

它需要花費很多時間來播放和顯示。 –

+0

我的代碼中有什麼問題。 –

回答

1

謝謝@米蘭你的寶貴建議。我使用AVPlayerViewController,它工作正常。該代碼:

AVPlayer*player = [[AVPlayer alloc] initWithURL:videoURL]; 

     AVPlayerViewController* playerController = [[AVPlayerViewController alloc] init]; 


     playerController.player = player; 
     [self addChildViewController:playerController]; 

     [cell.contentView addSubview: playerController.view]; 
     playerController.view.frame = cell.frame; 

     [player play];