Swift将视频上传到Firebase并在UITableViewCell中播放

时间:2017-10-30 19:51:16

标签: ios swift uitableview firebase avplayer

我正在尝试将从库中选择的视频上传到firebase存储,然后获取与视频相关联的网址,并通过点击播放按钮在桌面视频中播放。

我上传文件的方式是:

func postVideo(userId: String) {
    self.progressBar.isHidden = false
    let compressedURL = NSURL.fileURL(withPath: NSTemporaryDirectory() + NSUUID().uuidString + ".mov")
    VideoOperation.compressVideo(inputURL: videoUrlPassed!, outputURL: compressedURL, handler: { (exportSession) in
        guard let session = exportSession else {return}
        let compressedVideoUrl = session.outputURL
        let videoIdString = NSUUID().uuidString
        let storageRef = Storage.storage().reference(forURL: Configuration.STORAGE_MAIN_REF).child("posts").child("videos").child(userId).child(videoIdString + ".mov")
        let uploadTask = storageRef.putFile(from: compressedVideoUrl!, metadata: nil, completion: { (metadata, error) in
            if error != nil {
                ProgressHUD.showError(error?.localizedDescription)
                return
            }
            let videoUrl = metadata?.downloadURL()?.absoluteString
    })
}

然后我创建了一个模型来下载信息

extension Post {
static func videoPost(dictionary: [String:AnyObject]) -> Post {
    let post = Post()
    post._caption = dictionary["caption"] as? String
    post._videoUrl = dictionary["videoUrl"] as? String
    post._videoThumbUrl = dictionary["videoThumbUrl"] as? String
    post._isVideo = dictionary["isVideo"] as? Bool
    return post
}

并且在我有tableview的控制器中,我在viewdidload中加载信息:

func loadPosts() {
    Database.database().reference().child("posts").observe(.childAdded) { (snapshot) in
        if let dict = snapshot.value as? [String:AnyObject] {
            let newPhotoPost = Post.photoPost(dictionary: dict)

            if !newPhotoPost.isVideo {
                self.posts.append(newPhotoPost)
            } else {
                let newVideoPost = Post.videoPost(dictionary: dict)
                self.posts.append(newVideoPost)
            }
            self.tableView.reloadData()
        }
    }
}

然后在cellForRow中创建播放器:

if post.isVideo {
        let url = URL(string: post.videoUrl)
        cell.createPlayer(url: url!)
    }

这是在我的tableviewcell中创建播放器的函数:

    func createPlayer(url: URL) {
    self.playerCell = AVPlayer(url: url)
    self.playerLayerCell = AVPlayerLayer(player: playerCell)
    self.playerLayerCell?.frame = videoContainerView.bounds
    self.playerLayerCell?.videoGravity = AVLayerVideoGravity.resizeAspectFill
    self.videoContainerView.layer.insertSublayer(playerLayerCell!, at: 0)
}

视频正在播放,但我有两个问题: 1)当我关闭控制器时,视频继续播放 2)这是最烦人的一个我得到这个错误:

CredStore - performQuery - Error copying matching creds.  Error=-25300, query={
class = inet;
"m_Limit" = "m_LimitAll";
"r_Attributes" = 1;
sync = syna;

}

如果我按下播放按钮,视频正在播放,但我真的无法理解错误是什么!

如果有人能提供帮助我真的很高兴!

谢谢!

1 个答案:

答案 0 :(得分:1)

每次使用模型对象提供的url重新初始化AVPlayer时,都会调用tableview cellForRow。当滚动浏览表视图时初始化新单元格时,将为新单元格创建新的AVPlayer实例。

错误1:您可以通过调用UItableview的didEndDisplayingCell委托方法来停止播放器,并停止播放器播放器的速率不等于0.

if (cell.player.rate != 0) && (cell.player.error == nil)) {
     // player is playing
     cell.player = nil;
}

错误2:我不知道发生了什么。