Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EC515] Swift port #310

Closed
zippy1978 opened this issue May 29, 2024 · 0 comments · Fixed by #312
Closed

[EC515] Swift port #310

zippy1978 opened this issue May 29, 2024 · 0 comments · Fixed by #312
Assignees
Labels
🗃️ rule rule improvment or rule development or bug

Comments

@zippy1978
Copy link
Contributor

Creation of an AVAudioRecorder or AVCaptureSession object is used to record audio and video. These classes have methods to stop recording and release resources.

In addition to unnecessary resources (such as memory and instances of codecs) being held, failure to properly stop and release these objects if they are no longer needed may also lead to continuous battery consumption for mobile devices.

Noncompliant Code Examples

import AVFoundation

var audioRecorder: AVAudioRecorder?

func startRecording() {
    let settings = [
        AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
        AVSampleRateKey: 12000,
        AVNumberOfChannelsKey: 1,
        AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
    ]

    do {
        audioRecorder = try AVAudioRecorder(url: getDocumentsDirectory().appendingPathComponent("recording.m4a"), settings: settings)
        audioRecorder?.record()
    } catch {
        // Handle error
    }
}
import AVFoundation

var captureSession: AVCaptureSession?

func startVideoRecording() {
    captureSession = AVCaptureSession()
    captureSession?.beginConfiguration()

    let videoDevice = AVCaptureDevice.default(for: .video)
    let audioDevice = AVCaptureDevice.default(for: .audio)

    do {
        let videoInput = try AVCaptureDeviceInput(device: videoDevice!)
        let audioInput = try AVCaptureDeviceInput(device: audioDevice!)

        if (captureSession?.canAddInput(videoInput) ?? false) {
            captureSession?.addInput(videoInput)
        }

        if (captureSession?.canAddInput(audioInput) ?? false) {
            captureSession?.addInput(audioInput)
        }

        captureSession?.commitConfiguration()
        captureSession?.startRunning()
    } catch {
        // Handle error
    }
}

Compliant Solutions

import AVFoundation

var audioRecorder: AVAudioRecorder?

func startRecording() {
    let settings = [
        AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
        AVSampleRateKey: 12000,
        AVNumberOfChannelsKey: 1,
        AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
    ]

    do {
        audioRecorder = try AVAudioRecorder(url: getDocumentsDirectory().appendingPathComponent("recording.m4a"), settings: settings)
        audioRecorder?.record()
    } catch {
        // Handle error
    }
}

func stopRecording() {
    if let recorder = audioRecorder, recorder.isRecording {
        recorder.stop()
        audioRecorder = nil
    }
}
import AVFoundation

var captureSession: AVCaptureSession?

func startVideoRecording() {
    captureSession = AVCaptureSession()
    captureSession?.beginConfiguration()

    let videoDevice = AVCaptureDevice.default(for: .video)
    let audioDevice = AVCaptureDevice.default(for: .audio)

    do {
        let videoInput = try AVCaptureDeviceInput(device: videoDevice!)
        let audioInput = try AVCaptureDeviceInput(device: audioDevice!)

        if (captureSession?.canAddInput(videoInput) ?? false) {
            captureSession?.addInput(videoInput)
        }

        if (captureSession?.canAddInput(audioInput) ?? false) {
            captureSession?.addInput(audioInput)
        }

        captureSession?.commitConfiguration()
        captureSession?.startRunning()
    } catch {
        // Handle error
    }
}

func stopVideoRecording() {
    if let session = captureSession, session.isRunning {
        session.stopRunning()
        captureSession = nil
    }
}
@zippy1978 zippy1978 added the 🗃️ rule rule improvment or rule development or bug label May 29, 2024
@zippy1978 zippy1978 self-assigned this May 29, 2024
@dedece35 dedece35 linked a pull request May 30, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🗃️ rule rule improvment or rule development or bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant