Search code examples
swiftxcodeswift3avfoundationavaudioplayer

Play a Video in Collection View failed


I am working on an app where I am getting videos from a firebase link. To be able to display and play the video I am using this extension:

func generateThumbnailForVideoAtURL(filePathLocal: NSString) -> UIImage? {

    let vidURL = NSURL(fileURLWithPath:filePathLocal as String)
    let asset = AVURLAsset(url: vidURL as URL)
    let generator = AVAssetImageGenerator(asset: asset)
    generator.appliesPreferredTrackTransform = true
    let timestamp = CMTime(seconds: 1, preferredTimescale: 60)
    do {
        let imageRef = try generator.copyCGImage(at: timestamp, actualTime: nil)
        return UIImage(cgImage: imageRef)
    } catch let error as NSError {
        print("Image generation failed with error \(error)")
        return nil
    }
}

And then using it like this in cellAtIndexPath:

if posts[indexPath.row].videoUrl != nil {
            cell.videoView.generateThumbnailForVideoAtURL(filePathLocal: self.posts[indexPath.row].videoUrl as! NSString)
            print("VIDEO IS: \(posts[indexPath.row].videoUrl)")
}

I am getting this error:

Image generation failed with error Error Domain=NSURLErrorDomain Code=-1102 "You do not have permission to access the requested resource." UserInfo={NSLocalizedDescription=You do not have permission to access the requested resource., NSUnderlyingError=0x1c4046ff0 {Error Domain=NSOSStatusErrorDomain Code=-12660 "(null)"}}

It seems like a UserInfo description, but I can not find anything in my plist to add for permission. It may just sound stupid for somebody else because this is how I feel now as I can't find a way to make it work. Please help!


Solution

  • You should use this

    let vidURL = URL(string:filePathLocal as String)
    

    instead of

    let vidURL = NSURL(fileURLWithPath:filePathLocal as String)