为什么我的视频无法在AVPlayer的滚动视图中的单元格中显示

问题描述 投票:0回答:1

我知道我的代码到目前为止还不是很有效,我只是想让它真正起作用,然后再对其进行优化。

基本上,我有一个函数FeaturedPostsManager.shared.getFeaturedPosts从服务器获取视频并将其存储在应用程序的缓存中,并将本地URL存储到视频中。在我的代码中,我对两个不同的视频做了两次,所以我有两个指向两个不同视频的网址。然后,我在自定义数据数组中添加它们的网址,以供滚动视图解开每个单元格,并将其各自的视频添加到其单元格中(例如,将视频一到单元一,将视频二到单元二添加到AVPlayer中)。没有抛出任何错误,什么也没有显示,并且此消息已发送给我:

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>>.

我不知道这意味着什么,但这是我的完整代码:

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")
}
}
ios swift uiscrollview uicollectionviewcell avplayer
1个回答
0
投票

您需要在这里重新加载

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")
    }
})

添加

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()
}
}

然后

var data: CustomData?{
    didSet{
        guard let data = data else { return }
        bg.image = data.image
        self.reload()
    }
© www.soinside.com 2019 - 2024. All rights reserved.