问题
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];
}
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.
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?
回答1:
I have figured out my problem. And in true idiot fashion, it was a very, very simple fix :)
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);
to this:
videoCell.playerLayer2.frame = CGRectMake(0, 0, (videoCell.frame.size.width / 2), 255);
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.
来源:https://stackoverflow.com/questions/36786914/avplayer-on-left-side-of-cell-works-perfectly-avplayer-on-right-side-of-cell-do