如何在AVPlayer的自定义数据模型中引用视频

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

我已经设法为我的自定义数据模型引用了图像,该图像用于向唯一的单元格显示其不同的信息。我已经能够对图像进行处理,因为它非常容易,我不知道如何对视频进行处理。我的数据模型如下所示:

fileprivate let data = [
    CustomData(title: "Test", image: #imageLiteral(resourceName: "ss-1"), url: "this-will-be-a-local-url-in-cache-to-a-video"),
    CustomData(title: "Test2", image: #imageLiteral(resourceName: "done-button"), url: "this-will-be-a-local-url-in-cache-to-a-video"),
    CustomData(title: "Test2", image: #imageLiteral(resourceName: "notificationIcon"), url: "this-will-be-a-local-url-in-cache-to-a-video")
]

以及一些最相关的代码,用于在滚动视图中为每个单元格分配不同的信息:

class CustomCell: UICollectionViewCell{

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

    }
}

fileprivate let bg: UIImageView = {
    let iv = UIImageView()
    iv.image = #imageLiteral(resourceName: "splash_icon")
    iv.translatesAutoresizingMaskIntoConstraints = false
    iv.contentMode = .scaleAspectFill
    iv.clipsToBounds = true
    iv.layer.cornerRadius = 10
    return iv
}()

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

    contentView.addSubview(bg)
    bg.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
    bg.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true
    bg.trailingAnchor.constraint(equalTo: contentView.trailingAnchor).isActive = true
    bg.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
}

现在我知道我需要与用于图像的功能相同:

    fileprivate let bg: UIImageView = {
    let iv = UIImageView()
    iv.image = #imageLiteral(resourceName: "splash_icon")
    iv.translatesAutoresizingMaskIntoConstraints = false
    iv.contentMode = .scaleAspectFill
    iv.clipsToBounds = true
    iv.layer.cornerRadius = 10
    return iv
}()

像视频一样,我只是不知道如何在AVPlayer函数中以某种方式引用url,并以与在CustomData类中相同的方式引用它:

    fileprivate let video: AVURLAsset = {

    }()

[谢谢,如果您需要更多信息,请发表评论,我已经在寻找AVPlayer函数允许我执行类似(AVPlayer.url =“ URL”)的工作,以便从数据模型中为avplayer分配正确的url,但显然不是那么简单啊哈。

内森

编辑:

我要问的是我将如何实现与该图像功能相同的功能:

 fileprivate let bg: UIImageView = {
    let iv = UIImageView()
    iv.image = #imageLiteral(resourceName: "splash_icon")
    iv.translatesAutoresizingMaskIntoConstraints = false
    iv.contentMode = .scaleAspectFill
    iv.clipsToBounds = true
    iv.layer.cornerRadius = 10
    return iv
}()

所以我可以在自定义数据类中引用它:

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

    }
}

因此它可以从自定义数据数组中解包数据,以便我初始化它并使用此功能在视图中显示它:

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

    contentView.addSubview(bg)
    bg.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
    bg.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true
    bg.trailingAnchor.constraint(equalTo: contentView.trailingAnchor).isActive = true
    bg.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
}
swift uiscrollview avplayer swift5
1个回答
0
投票

我认为您可以使用AVPlayer.replaceCurrentItem(with:)解决您所描述的问题:

if let urlString = data?.url, let url = URL(string: urlString) {
    myPlayer.replaceCurrentItem(with: AVPlayerItem(url: url))
}
© www.soinside.com 2019 - 2024. All rights reserved.