单元格左侧的AVPlayer工作正常。单元格右侧的AVPlayer不起作用

时间:2016-04-22 06:52:55

标签: ios uitableview avplayer

我有UITableView单元,每个单元有2个AVPlayers。一个在右侧,一个在左侧。并且与此同时,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];
}

这里唯一的区别是添加了背景颜色变化,因此我可以看到每个视图/图层的位置。目前,单元格右侧的背景为橙色 - 这意味着redView2确实具有正确的帧。在此之后,我将playerLayer2添加为子图层 - 但它根本不显示任何内容。当我尝试改变它的背景颜色时,它不会改变任何东西。我已经尝试设置一个keyValue观察器来播放视频一旦准备就绪,这也没有帮助。

这里可能有什么问题?我很困惑,因为这两种情况几乎完全相同,除了帧。它根本不起作用。

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

@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

任何人都知道会发生什么事情?

1 个答案:

答案 0 :(得分:0)

我已经弄清楚了我的问题。在真正的白痴时代,这是一个非常非常简单的解决方案:)

在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原点从屏幕一侧开始关闭,因此不可见。