Why aren't my videos showing in cells in my scroll view with AVPlayer

↘锁芯ラ 提交于 2020-01-16 08:27:13

问题


I know my code isn't efficient as of yet, I just want it to work really before I optimise it.

Basically I have a function FeaturedPostsManager.shared.getFeaturedPosts to get videos from my server and store it in the app's cache and store the local url to the video. Which in my code I do it twice for two different videos, so I have two urls that go to two different videos. I then add them urls in my custom data array for my scroll view to unwrap for each cell and add it's respective video to it's cell (eg. video one to cell one, video two to cell two) into an AVPlayer. No errors are thrown, it's just nothing is shown and this message is given to me:

The behavior of the UICollectionViewFlowLayout is not defined because:
2020-01-07 09:48:05.095012+0000 Clipify[9131:368986] the item height must be less than the 
height of the UICollectionView minus the section insets top and bottom values, minus the 
content insets top and bottom values.
2020-01-07 09:48:05.095574+0000 Clipify[9131:368986] The relevant UICollectionViewFlowLayout 
instance is <UICollectionViewFlowLayout: 0x7f8be8525c20>, and it is attached to 
<UICollectionView: 0x7f8be9063a00; frame = (16 150; 343 171.667); clipsToBounds = YES; 
gestureRecognizers = <NSArray: 0x600003f4c1e0>; layer = <CALayer: 0x60000319b100>; 
contentOffset: {0, 0}; contentSize: {0, 0}; adjustedContentInset: {0, 0, 0, 0}; layout: 
<UICollectionViewFlowLayout: 0x7f8be8525c20>; dataSource: <Clipify.HomeViewController: 
0x7f8be850c240>>.

I don't know if that means anything, but this is my full code:

import UIKit
import AVKit
import Firebase

struct CustomData {
    var title: String
    var image: UIImage
    var url: String
}

var videoURL = ""

class HomeViewController: UIViewController {

    var itemOne: String?
    var itemTwo: String?

override func viewDidLoad() {
    super.viewDidLoad()

    view.addSubview(collectionView)
    collectionView.backgroundColor = .white
    collectionView.topAnchor.constraint(equalTo: view.topAnchor, constant: 150).isActive = true
    collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16).isActive = true
    collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16).isActive = true
    collectionView.heightAnchor.constraint(equalTo: collectionView.widthAnchor, multiplier: 0.5).isActive = true 

    collectionView.delegate = self
    collectionView.dataSource = self

    getPosts()
}

func getPosts(){
    FeaturedPostsManager.shared.getFeaturedPosts(index: 0, completion:  { url in
        if url != nil{
            self.itemOne = url!
            print(self.itemOne)
        }else{
            print("error")
        }
    })
    FeaturedPostsManager.shared.getFeaturedPosts(index: 1, completion:  { url in
        if url != nil{
            self.itemTwo = url!
            print(self.itemTwo)
        }else{
            print("error")
        }
    })
}

var data: [CustomData] {
    return [
        CustomData(title: "Test", image: #imageLiteral(resourceName: "ss-1"), url: itemOne!),
        CustomData(title: "Test2", image: #imageLiteral(resourceName: "done-button"), url: itemTwo!)
    ]
}

fileprivate let collectionView: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .horizontal
    let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
    cv.translatesAutoresizingMaskIntoConstraints = false
    cv.register(CustomCell.self, forCellWithReuseIdentifier: "cell")
    return cv
}()
}

extension HomeViewController: UICollectionViewDelegateFlowLayout, UICollectionViewDataSource{

public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: collectionView.frame.width, height: collectionView.frame.width)
}
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return data.count
}

public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CustomCell
    cell.data = self.data[indexPath.row]
    return cell
}
}

class CustomCell: UICollectionViewCell{

    var data: CustomData?{
        didSet{
            guard let data = data else { return }
            bg.image = data.image
        }
    }

override init(frame: CGRect) {
    super.init(frame: frame)

    if let unwrappedURLString = data?.url, let url = URL(string: unwrappedURLString) {
        let player = AVPlayer(url: url)
        let playerLayer = AVPlayerLayer(player: player)
        playerLayer.frame = contentView.bounds
        contentView.layer.addSublayer(playerLayer)
        player.play()
    }
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
}

回答1:


You need a reload here

FeaturedPostsManager.shared.getFeaturedPosts(index: 0, completion:  { url in
    if url != nil{
        self.itemOne = url!
        print(self.itemOne)
        DispatchQueue.main.async { self.collectionView.reloadData() }
    }else{
        print("error")
    }
})
FeaturedPostsManager.shared.getFeaturedPosts(index: 1, completion:  { url in
    if url != nil{
        self.itemTwo = url!
        print(self.itemTwo)
        DispatchQueue.main.async { self.collectionView.reloadData() }
    }else{
        print("error")
    }
})

override init(frame: CGRect) { is called once . so Add

func reload() {
 if let unwrappedURLString = data?.url, let url = URL(string: unwrappedURLString) {
    let player = AVPlayer(url: url)
    let playerLayer = AVPlayerLayer(player: player)
    playerLayer.frame = contentView.bounds
    contentView.layer.addSublayer(playerLayer)
    player.play()
 }
}

Then

var data: CustomData?{
    didSet{
        guard let data = data else { return }
        bg.image = data.image
        self.reload()
    }
}


来源:https://stackoverflow.com/questions/59626633/why-arent-my-videos-showing-in-cells-in-my-scroll-view-with-avplayer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!