为什么我的视频没有在AVPlayer的滚动视图中的单元格中显示 [英] Why aren't my videos showing in cells in my scroll view with AVPlayer

查看:87
本文介绍了为什么我的视频没有在AVPlayer的滚动视图中的单元格中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

推荐答案

您需要在此处重新加载

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) {被调用一次.所以添加

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

然后

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

这篇关于为什么我的视频没有在AVPlayer的滚动视图中的单元格中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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