AVAudioRecorder not working
In Swift you don't provide the error
parameter since it throws
.
Change this:
soundRecorder = try AVAudioRecorder(url: getFileURL() as URL, settings: recordSettings, error: &error)
to:
soundRecorder = try AVAudioRecorder(url: getFileURL() as URL, settings: recordSettings)
CMSampleBufferGetImageBuffer returning null
After hours of debugging, it turns out that the sample might be a video or audio sample. So trying to get CVPixelBufferRef from an audio buffer returns null.
I solved it by checking the sample type before proceeding. Since I am not interested in the audio samples I simply return when its an audio sample.
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CMFormatDescriptionRef formatDesc = CMSampleBufferGetFormatDescription(sampleBuffer);
CMMediaType mediaType = CMFormatDescriptionGetMediaType(formatDesc);
//Checking sample type before proceeding
if (mediaType == kCMMediaType_Audio)
{return;}
//Processing the sample...
}
Related Topics
How to Create a Packed Data Structure in Swift
Property Cannot Be Declared Public Because Its Type Uses an Internal Type
Subclass of Skspritenode in .Sks File
How to Implement a Spritekit Timer
Facebookshare Causing Compiler Error After Update
Xcode Autocomplete Does Not Work in Sources Folder of Swift Playgrounds
How to Save Cgimage to Data in Swift
Iterate an Array W/ Explicit Object Type in Swift
What Does the Swift 'Mutating' Keyword Mean
How to Rearrange Views in Swiftui Zstack by Dragging
How to Alloc/Dealloc Unsafe Pointers in Swift
Convert String to Cgfloat in Swift
How to Fix Error: This Class Is Not Key Value Coding-Compliant for the Key Tableview.'
Get Rawvalue from Enum in a Generic Function
Swift Calling Static Methods: Type(Of: Self) VS Explicit Class Name