简体   繁体   English

UITableViewCell中的AVPlayer是黑色的,无法查看

[英]AVPlayer in UITableViewCell is black and can't view

I've gone through every post and article but have yet to solve my problem and I'm not sure why.我已经浏览了每篇文章和文章,但还没有解决我的问题,我不知道为什么。 I have a table view which if the piece of content is an image, it will show an image in the TableViewCell, otherwise it will show a video in it.我有一个表格视图,如果内容是图像,它将在 TableViewCell 中显示一个图像,否则它将在其中显示一个视频。 For the video, I have the link printed so it's there and the link works, but for some reason in the TableViewCell the video doesn't play and it's just black.对于视频,我打印了链接,所以它在那里并且链接有效,但是由于某种原因在 TableViewCell 中视频没有播放,它只是黑色的。

Here is the code for the TableViewCell:这是 TableViewCell 的代码:

import UIKit
import AVFoundation

class ViewImageCell: UITableViewCell {

var avQueuePlayer   : AVQueuePlayer?
var avPlayerLayer   : AVPlayerLayer?



let postContentView: UIView = {
    let view = UIView()

    return view
}()

   let contentImageView: UIImageView = {
       let imageView = UIImageView()
       imageView.translatesAutoresizingMaskIntoConstraints = false
       imageView.layer.masksToBounds = true
       imageView.autoresizesSubviews = true
       imageView.contentMode = .scaleAspectFit
       imageView.image = UIImage(named: "ProfileIcon")

      return imageView
   }()


override func awakeFromNib() {
    super.awakeFromNib()


    // Initialization code


}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}


override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: .default, reuseIdentifier: reuseIdentifier)

    addSubview(postContentView)
    addSubview(contentImageView)
    postContentView.anchor(top: self.topAnchor, left: self.leftAnchor, bottom: self.bottomAnchor, right: self.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)
    contentImageView.anchor(top: self.topAnchor, left: self.leftAnchor, bottom: self.bottomAnchor, right: self.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)
}


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

func addPlayer(for url: URL) {
    self.avQueuePlayer = AVQueuePlayer(url: url)
    self.avPlayerLayer = AVPlayerLayer(player: self.avQueuePlayer!)
    self.avPlayerLayer?.frame = self.postContentView.bounds
    self.avPlayerLayer?.fillMode = .both
    self.postContentView.layer.addSublayer(self.avPlayerLayer!)
    self.avQueuePlayer?.play()
}

   func bindData(content: NSDictionary) {

    if content[kPICTURE] != nil {

        let data = content[kPICTURE] as! String
        let picData = NSData(base64Encoded: data, options: [])

        if let image = UIImage(data: picData! as Data) {
            contentImageView.image = image
        } else {
            print("There was an error loading the pic")
        }

    } else {

    }

    if content[kVIDEO] != nil {
        contentImageView.isHidden = true
        let videoData = content[kVIDEO] as! String
           let url = NSURL(fileURLWithPath: videoData)
        addPlayer(for: url as URL)
           print("Here is a video")

    } else {
        //
    }

}
}

Here is a piece of the code for the TableView:这是 TableView 的一段代码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.section == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: imageCellID, for: indexPath) as! ViewImageCell

        if singleContent != nil {

            cell.bindData(content: singleContent)

        } else {
            print("Can't find singleContent")
        }

        return cell
    }




    return UITableViewCell()
}

Let me know if you have any questions and need more clarification.如果您有任何问题并需要更多说明,请告诉我。 All help is greatly appreciated.非常感谢所有帮助。

EDIT: Here is an example of the type of video path I'm using.编辑:这是我正在使用的视频路径类型的示例。 I'm taking this from my Firebase Database, not a local file in my project.我是从我的 Firebase 数据库中获取的,而不是我项目中的本地文件。

file:///private/var/mobile/Containers/Data/PluginKitPlugin/49C0EAA4-9825-4D06-8CFB-6136FF5BCC3C/tmp/trim.AA010490-81F0-43C8-AE8A-1029D5AF55E1.MOV file:///private/var/mobile/Containers/Data/PluginKitPlugin/49C0EAA4-9825-4D06-8CFB-6136FF5BCC3C/tmp/trim.AA010490-81F0-43C8-AE8A-1029D5AF55E1。

I also get the error code below.我也得到下面的错误代码。 I looked and think it means that the file doesn't exist, but I can still view that file else where in a different view it plays the video.我查看并认为这意味着该文件不存在,但我仍然可以在其他视图中查看该文件,该文件在其他视图中播放视频。

NSURLConnection finished with error - code -1100 NSURLConnection 以错误结束 - 代码 -1100

Here is where the video path is stored in the database, not using Firebase Storage at all.这是视频路径存储在数据库中的位置,根本不使用 Firebase 存储。 在此处输入图片说明

If you want to play video URL in webview use below code: (manage code as per your requirement)如果您想在 webview 中播放视频 URL,请使用以下代码:(根据您的要求管理代码)

func bindData(content: NSDictionary)
{

    if content[kPICTURE] != nil {

        let data = content[kPICTURE] as! String
        let picData = NSData(base64Encoded: data, options: [])

        if let image = UIImage(data: picData! as Data) {
            contentImageView.image = image
        } else {
            print("There was an error loading the pic")
        }

    } else {

    }

    if content[kVIDEO] != nil {
        contentImageView.isHidden = true
        let videoData = content[kVIDEO] as! String

        //let url = NSURL(fileURLWithPath: videoData)


        let storageRef = Storage.storage().reference(forURL: <YourFilePathURL>)
storageRef.getData(maxSize: INT64_MAX) { (data, error) in
    if let error = error {
        print("Error downloading image data: \(error)")
        return
    }
    storageRef.getMetadata(completion: { (metadata, metadataErr) in

        if let error = metadataErr {
            print("Error downloading metadata: \(error)")
            return
        }
        if (metadata?.contentType == "image/gif") {
            print("It is Gif")
        } else {
            let downloadUrl = metadata?.downloadURL()
            if downloadUrl != nil{
 print(downloadUrl)
       //You will get your Video Url Here
        self.PlayVideoInWEbView(downloadUrl)
        print("Here is a video")

    }

        }
    })
}
    } else {
        //
    }

}

func PlayVideoInWEbView(_ videoURL : String)
{
    let myWebView:UIWebView = UIWebView(frame: CGRect(x: 20, y: 20, width: self.viewCell.frame.width, height: 200))



    myWebView.delegate = self

    let myURL = URL(string: videoURL)
    let myURLRequest:URLRequest = URLRequest(url: myURL!)
    myWebView.loadRequest(myURLRequest)

    self.viewCell.addSubview(myWebView)

}

@Jaqueline Try like below If you have or stored a recorded video into the local document directory. @Jaqueline 像下面这样尝试如果您已经或将录制的视频存储到本地文档目录中。

func bindData(content: NSDictionary) {

    if content[kPICTURE] != nil {

        let data = content[kPICTURE] as! String
        let picData = NSData(base64Encoded: data, options: [])

        if let image = UIImage(data: picData! as Data) {
            contentImageView.image = image
        } else {
            print("There was an error loading the pic")
        }

    } else {
    }

    if content[kVIDEO] != nil {
        contentImageView.isHidden = true
        //let videoData = content[kVIDEO] as! String

        //let url = NSURL(fileURLWithPath: videoData)

        let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
        let url = NSURL(fileURLWithPath: path)
        let filePath = url.appendingPathComponent("/video.mp4")!.path

        let videoData = URL(fileURLWithPath: filePath)

        //addPlayer(for: url as URL) //remove this line and add below line

        self.PlayVideoInWEbView(videoData)
        print("Here is a video")

    } else {
        //
    }
}

func PlayVideoInWEbView(_ videoURL : String) {
    let myWebView:WKWebView = WKWebView(frame: CGRect(x: 20, y: 20, width: self.viewCell.frame.width, height: 200))

    //myWebView.delegate = self

    myWebView.uiDelegate = self
    myWebView.navigationDelegate = self

    let myURL = URL(string: videoURL)
    let myURLRequest:URLRequest = URLRequest(url: myURL!)
    //myWebView.loadRequest(myURLRequest)

    myWebView.load(myURLRequest)

    self.viewCell.addSubview(myWebView)
}

//MARK: - WKNavigationDelegate
extension YourViewControllerClass: WKNavigationDelegate {

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        //Here hide your loading indicator
    }

    func webViewWebContentProcessDidTerminate(_ webView: WKWebView) {

        DDLogDebug("webViewWebContentProcessDidTerminate ==> \(webView)")
    }

    func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
        //Here hide your loading indicator
    }
}

Hope it will help your required output!!!.希望它会帮助您所需的输出!!!。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM