正如我的问题所说,我想在Twitter等UITableViewCell中嵌入视频.没有声音的自动播放.
这是我在VideoCell.m中的setSelected方法
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
NSURL *urlVideoFile = [NSURL URLWithString:self.urlVideo];
NSAssert(urlVideoFile, @"Expected not nil video url");
_playerViewController = [[AVPlayerViewController alloc] init];
_playerViewController.player = [AVPlayer playerWithURL:urlVideoFile];
_playerViewController.view.frame = self.viewVideo.bounds;
_playerViewController.showsPlaybackControls = YES;
[self.viewVideo addSubview:_playerViewController.view];
self.viewVideo.autoresizesSubviews = YES;
_playerViewController.player.play;
}
Run Code Online (Sandbox Code Playgroud)
但显然不是最好的方法,因为加载视频非常缓慢,而且连续性,桌子的滚动不断被中断!
我想知道这是最好的做法!谢谢!
对不起我的英语不好!
编辑:
我用dispatch_assync方法解决了de load issue,即阻塞表滚动的问题.
我删除了setSelected中的代码并将其放在主视图控制器中的cellForRow中
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@", [arrayVideos objectAtIndex:indexPath.row]]];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
cell.videoItem = [AVPlayerItem playerItemWithURL:url];
dispatch_sync(dispatch_get_main_queue(), ^{
cell.videoPlayer …Run Code Online (Sandbox Code Playgroud)