UICollectionViewCell中的AVPlayerViewController错误? [英] AVPlayerViewController in UICollectionViewCell bug?

查看:132
本文介绍了UICollectionViewCell中的AVPlayerViewController错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的UICollectionView中,我有一个单元格。这是我的 cellForItemAtIndexPath

In my UICollectionView i have a cell. This is my cellForItemAtIndexPath

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
    var videoCell = collectionView.dequeueReusableCellWithReuseIdentifier("cellVideo", forIndexPath: indexPath) as? CollectionViewCell

    let post = self.arrayOfDetails[indexPath.row]

    var myUrl = post.image // post.image is image url

    let fileUrl = NSURL(string: post.image)

    aPlayer = AVPlayer(URL: fileUrl)

    moviePlayerController.player = aPlayer
    moviePlayerController.view.frame = CGRectMake(8, 8, videoCell!.frame.size.width-16, 310)
    moviePlayerController.videoGravity = AVLayerVideoGravityResizeAspectFill
    moviePlayerController.view.sizeToFit()
    moviePlayerController.showsPlaybackControls = true
    videoCell!.addSubview(moviePlayerController.view)

    return videoCell!
}

问题是AVPlayerViewController没有出现在每个单元格上。但是只在我看到的那个下面的单元格上,所以如果我在我的UICollectionView中向下滚动并且一旦屏幕上出现一个新单元格,AVPlayerViewController就在那个单元格而不是屏幕中间的单元格。有什么建议可能是我的错误吗?

The problem is that the AVPlayerViewController does not appear on each cell. But only on the cell that is under the one at i looking at, so if i scroll down in my UICollectionView and once a new cell appear on the screen, the AVPlayerViewController is on that cell and not the cell in the middel of screen. Any suggestions what might me wrong here?

视频: https ://vid.me/e/fU5X

推荐答案

你需要一个新的aPlayer和moviePlayerController用于每个单元格有视频。这样的事情应该这样做:

You need a new aPlayer and moviePlayerController for each cell which has the video in it. Something like this should do it:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
    {
        var videoCell = collectionView.dequeueReusableCellWithReuseIdentifier("cellVideo", forIndexPath: indexPath) as? CollectionViewCell

        var aPlayer = AVPlayer()
        let moviePlayerController = AVPlayerViewController()

        let post = self.arrayOfDetails[indexPath.row]

        var myUrl = post.image // post.image is image url

        let fileUrl = NSURL(string: post.image)

        var aPlayer = AVPlayer()
        let moviePlayerController = AVPlayerViewController()

        aPlayer = AVPlayer(URL: fileUrl)

        moviePlayerController.player = aPlayer
        moviePlayerController.view.frame = CGRectMake(8, 8, videoCell!.frame.size.width-16, 310)
        moviePlayerController.videoGravity = AVLayerVideoGravityResizeAspectFill
        moviePlayerController.view.sizeToFit()
        moviePlayerController.showsPlaybackControls = true
        videoCell!.addSubview(moviePlayerController.view)

        return videoCell!
    }

这篇关于UICollectionViewCell中的AVPlayerViewController错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆