0

Using AVAssetWriter to save captured audio and video to a file. It works correctly on my own machines all the time, but Apple's App Review team reports a crash when finishing recording.

In the report the thread crashes upon finishWriting(completionHandler:).

My code to stop writing:

    var videoWriterInput: AVAssetWriterInput!
    var audioWriterInput: AVAssetWriterInput!
    var videoWriter: AVAssetWriter!
    var isRecording = false

    func stopVideoWriter() async {
        guard isRecording else { return }
        isRecording = false

        videoWriterInput.markAsFinished()
        audioWriterInput.markAsFinished()

        videoWriter.finishWriting { [self] in
            videoWriter = nil
            videoWriterInput = nil
            audioWriterInput = nil
        }
    }

Some SO posts mention to check videoWriter.status before stopping, but this does not seem to help at all. Also there is no documentation that shows it is necessary to check the status before finishing.

Apple's documentation for finishWriting(completionHandler:) mentions the following:

To ensure the asset writer finishes writing all samples, call this method only after all calls to append(:) or append(:withPresentationTime:) return.

The app works fine on my own testing machines, so I cannot confirm that this is the actual part where the crash happens.

Any ideas?

hotdogsoup.nl
  • 1,078
  • 1
  • 9
  • 22
  • 1
    The crash is almost certainly related to using implicitly unwrapped optionals (i.e. ending in `!`). You should should almost certainly get rid of those and replace them with normal types (without `!`). You don't show code for how you ensure that those are always non-nil at this point. If there is any possibility at all that they are nil, then this code will (correctly) crash. – Rob Napier Feb 13 '23 at 00:47
  • @hotdogsoup.nl Did you solve this? I face same problem – vietstone Jul 05 '23 at 18:57

0 Answers0