单元格左侧的AVPlayer可以完美工作.单元格右侧的AVPlayer不起作用 [英] AVPlayer on left side of cell works perfectly. AVPlayer on right side of cell does not work

查看:72
本文介绍了单元格左侧的AVPlayer可以完美工作.单元格右侧的AVPlayer不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有UITableView单元格,每个单元格都有2个AVPlayers.一个在右侧,一个在左侧.并与此配合,使用2个AVPlayerLayers,2个AVPlayerItems等.在单元的每一侧都进行完全相同的设置.否则,该单元格右侧的视频将无法播放.以下是恰好在左侧播放视频的代码:

I have UITableView cells, each with 2 AVPlayers. One on the right side, and one on the left side. And to go along with this, 2 AVPlayerLayers, 2 AVPlayerItems, etc. Exact same setup on each side of the cell. Except, the video on the right side of the cell does not play. Here is the code that plays the video just perfectly on the left side:

- (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];
}

正如我上面说的,这很好.对于单元格的右侧,我有几乎相同的代码,只是移动框架使其在右侧:

As I said above, that works just fine. For the right side of the cell, I have nearly identical code, just moving the frames around to make it on the right side:

- (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观察器以在准备就绪后播放视频,但这也无济于事.

The only difference here is the added background color changes so I can see where each view/layer is at. Currently, the background on the right side of the cell is orange--meaning that the redView2 has indeed the correct frame. After this, I add the playerLayer2 as the sublayer--but it simply doesn't display anything. When I try to change the background color of that, it doesn't change anything. I have tried also setting a keyValue observer to play the video once it becomes ready, and that did not help either.

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

What could possibly be wrong here? I'm very confused, as these two situations are nearly exactly identical except for the frames. And it simply doesn't work.

只要需要任何其他信息,这就是我的UITableViewCell类:

Just if any further information is needed, here is my 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

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

Anyone have any idea what could be going on?

推荐答案

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

I have figured out my problem. And in true idiot fashion, it was a very, very simple fix :)

在playVideoOnRightFromCache函数中,我只需要更改此行:

In the playVideoOnRightFromCache function, I just need to change this line:

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原点从屏幕侧面开始偏移,因此不可见.

The problem is that the sublayer I was adding it to already started at halfway across the screen. So when I made the x origin of the playerLayer2 frame to be the value of (videoCell.frame.size.width / 2), I was actually making the playerLayer2 origin start OFF the side of the screen, therefore not visible.

这篇关于单元格左侧的AVPlayer可以完美工作.单元格右侧的AVPlayer不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆