Search code examples
iosvideoavplayer

What is the best way to play video in UITableViewCell


I'm trying to make auto-play video in UITableViewCell depending on cell position.

I'm using the AVPlayer

Here is my code:

    __weak typeof(self)this = self;

    NSString* videoPath = @"http://test.com/test.mp4";
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL URLWithString:videoPath] options:nil];
    NSArray* keys = [NSArray arrayWithObjects:@"playable",nil];

    [asset loadValuesAsynchronouslyForKeys:keys completionHandler:^(){
        AVPlayerItem* playerItem = [AVPlayerItem playerItemWithAsset:asset];
        this.avPlayer = [AVPlayer playerWithPlayerItem:playerItem];
        this.avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:this.avPlayer];
        this.avPlayerLayer.frame = _videoContent.frame;

        dispatch_async(dispatch_get_main_queue(),^{
            [this.videoContent.layer addSublayer:this.avPlayerLayer];
            [this.avPlayer play];
        });
    }];

But my UITableView is frozen when i scroll the table. I think there are many time-consuming work but most biggest thing is

[this.avPlayer play]

So my question is that AVPlayer is the best way in this situation? And is there any way to improve the performance?


Solution

  • Use the below link , this suits for your question.

    https://github.com/PRX/PRXPlayer