我试图在UITableViewCell中使用AVPlayer播放来自网址的视频(不完全像Instagram一样,但足够接近)
我正在尝试一次播放一个视频。 当我点击单元格中的“播放”按钮时,视频开始播放但仅持续1或2秒,然后我得到一个AVPlayerItemPlaybackStalledNotification。
我在CustomTableViewCell.h中声明了
@property AVPlayer *player;
@property AVPlayerItem *playerItem;
@property AVPlayerLayer *playerLayer;
@property AVAsset *playerAsset;
@property NSString *playerUrl;
然后我的ViewController.m中有一个IBAction
- (IBAction)play:(id)sender {
CustomCell *clickedCell = (CustomCell *)[[sender superview] superview];
clickedCell.btnPlay.hidden = YES;
clickedCell.playerItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:clickedCell.playerUrl]];
clickedCell.player = [AVPlayer playerWithPlayerItem:clickedCell.playerItem];
clickedCell.playerLayer = [AVPlayerLayer playerLayerWithPlayer:clickedCell.player];
[[NSNotificationCenter defaultCenter]
addObserverForName:AVPlayerItemPlaybackStalledNotification
object:clickedCell.playerItem
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *note) {
NSLog(@"%@ -> %ld", @"AVPlayerItemPlaybackStalledNotification", (long)[clickedCell.playerItem status]);
}];
[clickedCell.playerLayer setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width)];
[clickedCell.iamgen.layer addSublayer:clickedCell.playerLayer];
[clickedCell.playerLayer setBackgroundColor:[UIColor clearColor].CGColor];
[clickedCell.iamgen.layer addSublayer:clickedCell.playerLayer];
[clickedCell.player play];
}
clickedCell.iamgen是因为我有一个UIIMageView,其中我提供了视频的预览图像。
为什么我总能得到停滞的通知?互联网连接?哪个是实现这种情况的最佳方式?
抱歉我的英文!
编辑: 我很确定因为缓冲区加载速度不够快。我认为这是因为如果我在AVPlayerItemPlaybackStalledNotification之后重复点击播放按钮就像视频一帧一帧前进。我认为这个想法是知道缓冲区何时足以让我继续播放。