2016-04-22 15 views
1

私は2つのAVPlayersを持つUITableViewセルを持っています。右側に1つ、左側に1つ。これと一緒に行くには、2 AVPlayerLayers、2 AVPlayerItemsなど、セルの両側に正確に同じ設定。ただし、セルの右側のビデオは再生されません。セルの左側のAVPlayerは完全に機能します。セルの右側のAVPlayerが動作しない

- (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オブザーバーが準備ができたらビデオを再生するように設定しようとしましたが、それはどちらも助けにはなりませんでした。

ここで間違いがありますか?私は非常に混乱しています。これらの2つの状況は、フレームを除いてほぼ同じです。そして、それは単に動作しません。

@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 

誰もが起こっていることができるものの任意のアイデアを持っている:任意のより詳細な情報が必要な場合

ただ、ここに私のUITableViewCellクラスですか?

+0

ビデオは同じ層でも違うのですか? 'self.videoChallengeID1'を両方のプレーヤーにパスまたはURLとして渡しているからです。 – Lion

+0

それぞれのプレーヤーのビデオが異なっています – joey

+0

なぜあなたは両方に同じURLを渡していますか? – Lion

答えて

0

私は自分の問題を理解しました。これに

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); 

と真の馬鹿方式で、それは非常に、非常に簡単な修正:) playVideoOnRightFromCache機能で

だった、私はこの行を変更する必要があります問題は、私がサブレイヤを追加して、画面の半分のところですでに始まっていることです。ですから、playerLayer2フレームのx原点を(videoCell.frame.size.width/2)の値にすると、実際にはplayerLayer2原点を画面の横から始めるようにしていたので、見えません。

関連する問題