Load Avplayer with large amount of data (decrypted output of a local file)

399 Views Asked by At

Am using below code for decrypting the VideoFile.mp4(CTR/Nopadding encrypted file) and playing it in Avplayer. This code works for small files. But its crashing for big video files due to memory issues.

How to solve this issue. Please help

guard let content = Bundle.main.url(forResource: "videoFile", withExtension: "mp4") else{
                return
            }
 
  let vidoeData = NSData(contentsOf: content)!
            
   let vidoeDatadencrypted = vidoeData.decryptData(vidoeData as Data, withKey: aeskey.data(using: .utf8))
    
    let tmpFileURL1 = URL(fileURLWithPath:NSTemporaryDirectory()).appendingPathComponent("video").appendingPathExtension("mp4")
            let wasFileWritten1 = (try? vidoeDatadencrypted!.write(to: tmpFileURL1, options: [.atomic])) != nil
    
   if wasFileWritten1{
    let player = AVPlayer(url: tmpFileURL1)
    let playerLayer = AVPlayerLayer(player: player)
    playerLayer.frame = self.view.bounds
    self.view.layer.addSublayer(playerLayer)
    player.play()
   }

update:

Able to achive this for smaller files using the above shared code.

how to load avplayer with NSStream?

1

There are 1 best solutions below

2
Phil Dukhov On

you need to open your file using InputStream, pass the data chunk to CommonCrypto lib, write the output chunk to the OutputStream and repeat until file ends.

I haven't seen any guide to do so, but as a reference I've found a couple of examples for you: first, second, third.

Related questions for you to check out: first, second