Save broadcast to app group container using RPBroadcastSampleHandler

I try to save video on my device using app groups, but when i use group directory it’s not working. When i call videoWriter.startWriting() with first variant of videoFileLocation() it fails, but when i use second variant – videoWriter start writing. What could be wrong with variant with group?

Here is my class fields:

import ReplayKit  class SampleHandler: RPBroadcastSampleHandler {          let appIdentifier = "group.CY-2"     let fileManager = FileManager.default     var isRecording = false          var videoWriterInput: AVAssetWriterInput!     var audioWriterInput: AVAssetWriterInput!     var videoWriter: AVAssetWriter!          var sessionAtSourceTime: CMTime!          var outputFileLocation: URL! 

Here is the not working code that use app groups called in setUpWriter(). As result:

videoWriter.startWriting() = false

videoWriter.status = failed

func videoFileLocation() -> URL {             let documentsPath = fileManager.containerURL(forSecurityApplicationGroupIdentifier: appIdentifier)!             let videoOutputUrl = documentsPath.appendingPathComponent("videoFile").appendingPathExtension("mov")             do {                 if fileManager.fileExists(atPath: videoOutputUrl.path) {                     try fileManager.removeItem(at: videoOutputUrl)                 }                  fileManager.createFile(atPath: videoOutputUrl.path, contents: nil, attributes: nil)             } catch {                 print(error)             }             return videoOutputUrl         } 

But this works good and result is:

videoWriter.startWriting() = true

videoWriter.status = writing

func videoFileLocation() -> URL {     let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString     let videoOutputUrl = URL(fileURLWithPath: documentsPath.appendingPathComponent("videoFile")).appendingPathExtension("mov")     do {         if fileManager.fileExists(atPath: videoOutputUrl.path) {             try fileManager.removeItem(at: videoOutputUrl)         }     } catch {         print(error)     }     return videoOutputUrl } 

Code of setUpWriter() function:

func setUpWriter() {         do {             self.outputFileLocation = videoFileLocation()             videoWriter = try AVAssetWriter(outputURL: outputFileLocation, fileType: .mov)                          var status = videoWriter.status                          // add video input             videoWriterInput = AVAssetWriterInput(mediaType: AVMediaType.video, outputSettings: [                 AVVideoCodecKey : AVVideoCodecType.h264,                 AVVideoWidthKey : 720,                 AVVideoHeightKey : 1280,                 AVVideoCompressionPropertiesKey : [                     AVVideoAverageBitRateKey : 2300000,                 ],             ])                          videoWriterInput.expectsMediaDataInRealTime = true                          if videoWriter.canAdd(videoWriterInput) {                 videoWriter.add(videoWriterInput)                 print("video input added")             } else {                 print("no input added")             }                          // add audio input             audioWriterInput = AVAssetWriterInput(mediaType: AVMediaType.audio, outputSettings: nil)                          audioWriterInput.expectsMediaDataInRealTime = true                          if videoWriter.canAdd(audioWriterInput!) {                 videoWriter.add(audioWriterInput!)                 print("audio input added")             }                          let wasStarted = videoWriter.startWriting() //false true                          status = videoWriter.status //failed writing         } catch let error {             debugPrint(error.localizedDescription)         }     } 
Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.