What is the best way to play video in UITableViewCell

一个人想着一个人 提交于 2019-12-13 12:21:10

问题


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?


回答1:


Use the below link , this suits for your question.

https://github.com/PRX/PRXPlayer




回答2:


Are you sure that creating the AVPlayerItem, AVPlayer, and AVPlayerLayer can all be performed off the main thread? You might want to try putting those inside the block that dispatches on the main queue.



来源:https://stackoverflow.com/questions/29788233/what-is-the-best-way-to-play-video-in-uitableviewcell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!