【问题标题】:AVPlayerItem Play At Index?AVPlayerItem 在索引处播放?
【发布时间】:2014-08-08 18:00:30
【问题描述】:

我有一个 ICarousel 可以正确显示我的所有视频,但是当我按下播放按钮时,它会在下一个索引而不是我所在的索引上播放视频。

我希望它在指定的索引处播放,我尝试将[sender tag] 作为索引传递,并尝试一些技巧以在该索引处创建具有适当播放项的另一个子视图。

似乎没有任何效果,这是什么原因?!!

这是我的viewForItemAtIndex

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
      AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:[_videoURLS objectAtIndex:index]] options:nil];

     //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];

    //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 = index;
     [view addSubview:_playButton];

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

     return view;
}

这将显示甚至播放,尽管它​​只是不会播放当时专注的正确的。 我已经坚持了几天了,感谢任何帮助!

【问题讨论】:

  • 能否提供playVideo:方法的代码。然后我们可以看到你的问题。因为在 - (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view 中你初始化了资产。我认为您在 playVideo: 方法中也使用了相同的代码。一件事这个问题总是来的?如果 iCarousel 视图没有滚动
  • 如大家所说,请贴出playVideo方法的代码以解决您的问题

标签: ios objective-c indexing avplayer icarousel


【解决方案1】:

你能用 playVideo 方法更新代码吗?可能是这个方法有问题。检查这个方法中选择的url是否和我们在index处的视图中返回的一样。!!

【讨论】:

  • 从上面的代码中可以看出,AVPlayer 使用了视频 url,并将它们编码为字符串的 NSArray 以获得更好的结果。此时我应该只能说 [_player play];虽然那行不通。 @Sudeep 乔治
【解决方案2】:

首先,如果可能的话,我会使用- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index; 而不是按钮。其次 - 您没有重复使用视图并总是创建新视图(顺便说一句,您总是为视图设置相同的标签,这可能是播放错误视频的原因),这样性能可能会受到影响。

我会做什么: - 创建 UIView 子类(使用 xib 文件或以编程方式创建的接口 - 这是您环境中的偏好和/或标准的问题),创建像 - (void)configureWithAsset:(AVURLAsset *)asset- (void)configureWithURL:(NSURL *)url 这样的公共方法,并在此方法中传递所需的信息。在实现中,您应该使用所需的视频来初始化 AVPlayer。 在- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view 中调用此方法(您可以将 UIView 更改为您的自定义子类名称,因此您不需要强制转换) - 像这样

- (VideoView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(VideoView *)view
{
    if (view == nil) {
        view = [VideoView new]; // you may need to create another initializer
    }
    NSURL *url = get URL here;
    [view configureWithURL:url];
    return view;
}

然后当你需要播放视频时,调用[videoView playVideo](你也需要创建它,它应该只调用播放器对象上的播放)。

【讨论】:

    【解决方案3】:

    在与创建 iCarousel 的人取得联系后,我找到了解决方案。

    只需在 selctedItemAtIndex 中创建 AVPlayer 层,而不是在主视图中。 这让我可以使用

    _carousel.currentItemView && _carousel.currentItemAtIndex 
    

    将图层分配给特定轮播视图的子视频视图

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-26
      相关资源
      最近更新 更多