1

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?

Bhavesh Odedra
  • 10,990
  • 12
  • 33
  • 58
Eunchul Jeon
  • 169
  • 2
  • 13
  • You can use MPMoviePlayerController instead of AVPlayer. You can refer [here](http://stackoverflow.com/questions/15754933/playing-videos-in-uitableviewcell), for more specific details of how to use it in cell. – iYoung Apr 22 '15 at 05:26
  • @RajatDeepSingh I have tried but I think that MPMoviePlayerController is not faster than AVPlayer. Thanks :) – Eunchul Jeon Apr 22 '15 at 08:12

2 Answers2

0

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.

Tim Johnsen
  • 1,471
  • 14
  • 33
-4

Use the below link , this suits for your question.

https://github.com/PRX/PRXPlayer

Mahesh Babu
  • 3,395
  • 9
  • 48
  • 97