首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AVPlayerItem在Index?

AVPlayerItem在Index?
EN

Stack Overflow用户
提问于 2014-08-08 18:00:30
回答 3查看 653关注 0票数 1

我有一个ICarousel,它正确地显示了我所有的视频,但是当我按下Play按钮时,它将在下一个索引上播放视频,而不是在我所使用的索引上。

我希望它能在指定的索引处播放,我尝试将[sender tag]作为索引传递,并尝试使用该索引上适当的播放项创建另一个子视图。

似乎什么都不管用,这是什么原因?!!

这是我的viewForItemAtIndex

代码语言:javascript
复制
- (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;
}

这将显示,甚至发挥,虽然它只是不发挥一个正确的集中在当时。我已经被困在这几天了,任何帮助都是非常感谢的!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-08-15 18:00:50

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

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

代码语言:javascript
复制
_carousel.currentItemView && _carousel.currentItemAtIndex 

将该层分配给特定旋转木马视图的子videoView

票数 0
EN

Stack Overflow用户

发布于 2014-08-11 05:58:38

你能用playVideo方法更新代码吗?可能是这个method.Check的一个问题,从这个方法中选择的url是否与我们在索引视图中返回的相同。!!

票数 0
EN

Stack Overflow用户

发布于 2014-08-15 10:38:11

首先,如果可能的话,我会使用- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index;而不是按钮。第二,您没有重用视图,并且总是创建新的视图(而且BTW总是为视图设置相同的标记,这可能是播放错误视频的原因),这样可能会影响性能。

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

代码语言:javascript
复制
- (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] (您也需要创建它,它应该只需调用player对象)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25209531

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档