单元格左侧的 AVPlayer 完美运行。单元格右侧的 AVPlayer 不起作用

AVPlayer on left side of cell works perfectly. AVPlayer on right side of cell does not work

我有 UITableView 单元格,每个单元格都有 2 个 AVPlayer。一个在右边,一个在左边。与此相伴的是 2 个 AVPlayerLayers、2 个 AVPlayerItems 等。单元格每一侧的设置完全相同。除了,单元格右侧的视频不播放。这是在左侧完美播放视频的代码:

- (void)playVideoOnLeftFromCache:(CompletedTableViewCell *)videoCell {
    videoCell.contentView.backgroundColor = [UIColor clearColor];
    videoCell.redView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, (videoCell.frame.size.width / 2), 255)];
    [videoCell.contentView insertSubview:videoCell.redView aboveSubview:videoCell.blueView];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *mov = @".mov";
    NSString *fullComponent = [NSString stringWithFormat: @"%@%@", self.videoChallengeID1, mov];
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:fullComponent];
    NSURL *url = [NSURL fileURLWithPath:fullPath];

    videoCell.item1 = [AVPlayerItem playerItemWithURL:url];
    videoCell.player1 = [[AVPlayer alloc] initWithPlayerItem:videoCell.item1];
    [videoCell.player1 setMuted:YES];
    videoCell.player1.actionAtItemEnd = AVPlayerActionAtItemEndNone;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(player1Finished:)
       name:AVPlayerItemDidPlayToEndTimeNotification object:[videoCell.player1 currentItem]]; videoCell.playerLayer1 = [AVPlayerLayer playerLayerWithPlayer:videoCell.player1];
    videoCell.playerLayer1.frame = CGRectMake(0, 0, (videoCell.frame.size.width / 2), 255);
    videoCell.playerLayer1.backgroundColor = [UIColor clearColor].CGColor;
    videoCell.playerLayer1.videoGravity = AVLayerVideoGravityResize;
    [videoCell.redView.layer addSublayer:videoCell.playerLayer1];
    [videoCell.player1 play];
}

正如我上面所说的那样,效果很好。对于单元格的右侧,我有几乎相同的代码,只是移动框架使其位于右侧:

- (void)playVideoOnRightFromCache:(CompletedTableViewCell *)videoCell {
videoCell.contentView.backgroundColor = [UIColor clearColor];
videoCell.redView2 = [[UIView alloc] initWithFrame:CGRectMake((videoCell.frame.size.width / 2), 0, (videoCell.frame.size.width / 2), 255)];
[videoCell.contentView insertSubview:videoCell.redView2 aboveSubview:videoCell.blueView2];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *mov = @".mov";
NSString *fullComponent = [NSString stringWithFormat: @"%@%@", self.videoChallengeID1, mov];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:fullComponent];
NSURL *url = [NSURL fileURLWithPath:fullPath];

videoCell.item2 = [AVPlayerItem playerItemWithURL:url];
videoCell.player2 = [[AVPlayer alloc] initWithPlayerItem:videoCell.item2];
[videoCell.player2 setMuted:YES];
videoCell.player2.actionAtItemEnd = AVPlayerActionAtItemEndNone;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(player1Finished:)
                                             name:AVPlayerItemDidPlayToEndTimeNotification object:[videoCell.player2 currentItem]];
videoCell.playerLayer2 = [AVPlayerLayer playerLayerWithPlayer:videoCell.player2];
videoCell.playerLayer2.frame = CGRectMake((videoCell.frame.size.width / 2), 0, (videoCell.frame.size.width / 2), 255);
videoCell.redView2.layer.backgroundColor = [UIColor orangeColor].CGColor;
videoCell.blueView2.backgroundColor = [UIColor blueColor];
videoCell.playerLayer2.videoGravity = AVLayerVideoGravityResize;
[videoCell.redView2.layer addSublayer:videoCell.playerLayer2];
 // [videoCell.player2 addObserver:videoCell forKeyPath:@"status" options:0 context:nil];
  [videoCell.player2 play];
}

这里唯一的区别是添加的背景颜色发生变化,因此我可以看到每个 view/layer 所在的位置。目前,单元格右侧的背景为橙色——这意味着 redView2 确实具有正确的框架。在此之后,我将 playerLayer2 添加为子层——但它根本不显示任何内容。当我尝试更改它的背景颜色时,它不会改变任何东西。我还尝试设置一个 keyValue 观察器,以便在视频准备就绪后播放它,但这也无济于事。

这里可能出了什么问题?我很困惑,因为这两种情况除了框架外几乎完全相同。而且根本行不通。

如果需要任何进一步的信息,这是我的 UITableViewCell class:

@interface CompletedTableViewCell : UITableViewCell


 // Video Players
  @property (strong, nonatomic) AVPlayer *player1;
  @property (strong, nonatomic) AVPlayer *player2;

@property (strong, nonatomic) AVPlayerItem *item1;
@property (strong, nonatomic) AVPlayerItem *item2;

@property (strong, nonatomic) AVPlayerLayer *playerLayer1;
@property (strong, nonatomic) AVPlayerLayer *playerLayer2;

@property (strong, nonatomic) UIView *redView;
@property (strong, nonatomic) UIView *blueView;

@property (strong, nonatomic) UIView *redView2;
@property (strong, nonatomic) UIView *blueView2;
@end

有人知道会发生什么吗?

我已经解决了我的问题。以真正的白痴方式,这是一个非常非常简单的修复:)

在playVideoOnRightFromCache函数中,我只需要改变这一行:

videoCell.playerLayer2.frame = CGRectMake((videoCell.frame.size.width / 2), 0, (videoCell.frame.size.width / 2), 255);

对此:

videoCell.playerLayer2.frame = CGRectMake(0, 0, (videoCell.frame.size.width / 2), 255);

问题是我添加它的子层已经在屏幕的中间开始了。因此,当我将 playerLayer2 帧的 x 原点设为 (videoCell.frame.size.width / 2) 的值时,我实际上是在让 playerLayer2 原点从屏幕一侧开始,因此不可见。