我在桌面视图中使用AVPlayer。我想在滚动表视图时开始视频播放停止视频播放。请告诉我可能的方法是什么?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *tableIdentifier = @"tableIdentifier";
CustomTableCell *cell = [self.videoTableView dequeueReusableCellWithIdentifier:tableIdentifier];
if (cell == nil) {
cell = [[[NSBundle mainBundle]loadNibNamed:@"VideoCell" owner:self options:nil]objectAtIndex:0];
}
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
AVAsset *avAssert = [AVAsset assetWithURL:[NSURL URLWithString:@"url"]];
cell.playerItem =[[AVPlayerItem alloc]initWithAsset:avAssert];
dispatch_sync(dispatch_get_main_queue(), ^{
if (!cell.avPlayer) {
cell.avPlayer = [AVPlayer playerWithPlayerItem:cell.playerItem];
}else{
[cell.avPlayer replaceCurrentItemWithPlayerItem:cell.playerItem];
}
if (!cell.avPlayerLayer) {
cell.avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:cell.avPlayer];
cell.avPlayerLayer.frame = cell.videoPlayerView.layer.bounds;
cell.avPlayerLayer.videoGravity = AVLayerVideoGravityResize;
[cell.videoPlayerView.layer addSublayer: cell.avPlayerLayer];
if (indexPath.row == 0) {
cell.avPlayer.muted = NO;
[cell.avPlayer play];
}
}
});
});
return cell;
}
答案 0 :(得分:0)
以下是Apple在示例应用程序中做了类似的事情(我的方式不同):
/* When the app goes to the background, save the media url and time values
to the application preferences. */
- (void)applicationDidEnterBackground:(UIApplication*)application
{
if (self.playbackViewController)
{
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSURL* URL = [self.playbackViewController URL];
if (URL)
{
NSTimeInterval time = CMTimeGetSeconds([[self.playbackViewController player] currentTime]);
[defaults setURL:URL forKey:AVPlayerDemoContentURLUserDefaultsKey];
[defaults setDouble:time forKey:AVPlayerDemoContentTimeUserDefaultsKey];
}
else
{
[defaults removeObjectForKey:AVPlayerDemoContentURLUserDefaultsKey];
[defaults removeObjectForKey:AVPlayerDemoContentTimeUserDefaultsKey];
}
[defaults synchronize];
}
}
appDidFinishLaunching或appDidEnterForeground方法中的任何一个(或两者):
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
/* Restore saved media time value from defaults system. */
[[self.playbackViewController player] seekToTime:CMTimeMakeWithSeconds([defaults doubleForKey:AVPlayerDemoContentTimeUserDefaultsKey], NSEC_PER_SEC)];
}
return YES;
}