iCarousel AVPlayerItem实例未正确显示

时间:2014-08-07 04:21:57

标签: ios objective-c video avplayer icarousel

我想要实现的目标

我想展示一个独立播放的视频轮播。一次不超过10个。以及每个轮播对象都有自己的播放按钮可以独立播放。

我得到的是什么 在转盘中显示3个项目后,它会一遍又一遍地显示相同的三个项目,直到它到达10个项目的末尾。

我正在分配如下的AVAsset:

AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:[_videoURLS objectAtIndex:index]] options:nil];

_videoURLS是NSStrings的NSMutableArray,它返回10个计数来填充轮播中的10个项目,如下所示。

- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
//return the total number of items in the carousel
return [_videoURLS count];
}

现在填充视频视图我正在执行以下操作:

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{

AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:[_videoURLS objectAtIndex:index]] options:nil];


//create new view if no view is available for recycling
if (view == nil)
{
    //don't do anything specific to the index within
    //this `if (view == nil) {...}` statement because the view will be
    //recycled and used with other index values later
    UIView *mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 370.0f)]; 
    view = mainView;
    CGFloat mainViewWidth = mainView.bounds.size.width;

    //Video Player View
    _videoView = [[UIView alloc] initWithFrame:CGRectMake(0,0, mainViewWidth, 220)];
    _videoView.backgroundColor = [UIColor colorWithRed:123/255.0 green:123/255.0 blue:123/255.0 alpha:1.0];
    _videoView.center = CGPointMake(100, 170);//170
    _videoView.tag = 20;
    [view addSubview:_videoView];

    //AV Asset Player
    AVPlayerItem * playerItem = [[AVPlayerItem alloc] initWithAsset:asset];
    _player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
    playerLayer.frame = _videoView.bounds;
    [_videoView.layer addSublayer:playerLayer];
    [_player seekToTime:kCMTimeZero];


    //Play Button
    _playButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60.0f, 60.0f)];
    [_playButton setBackgroundImage:[UIImage imageNamed:@"play-icon-grey.png"] forState:UIControlStateNormal];
    [_playButton addTarget:self action:@selector(playVideo:) forControlEvents:UIControlEventTouchUpInside];
    _playButton.center = CGPointMake(100, 160);
    _playButton.tag = 1;
    [view addSubview:_playButton];


}
else
{
    //get a reference to the label in the recycled view
    _playButton = (UIButton *) [view viewWithTag:1];
    _videoView = (UIView *) [view viewWithTag:20];
}

//set item label
//remember to always set any properties of your carousel item
//views outside of the `if (view == nil) {...}` check otherwise
//you'll get weird issues with carousel item content appearing
//in the wrong place in the carousel

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didFinishPlaying:) name:MPMoviePlayerPlaybackDidFinishNotification object:_player];

return view;
}

我处理如下的播放按钮:

-(IBAction)playVideo:(id)sender {
if (_playButton.isHidden) {
    [_playButton setHidden:NO];
}else{
    [_playButton setHidden:YES];
    [_player play];
}
}

虽然当我按下播放按钮时,它会播放旋转木马中的第二个视频。 处理这个问题的正确方法是什么,让所有视频单手播放?

0 个答案:

没有答案