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

AVAudioSession setting not working properly for first recording #4

Open
NatashaTheRobot opened this issue Apr 17, 2024 · 0 comments
Open

Comments

@NatashaTheRobot
Copy link
Collaborator

NatashaTheRobot commented Apr 17, 2024

Just calling the recorder.prepare() function doesn't work on the first try to record, but works on the second attempt:

    func startRecording() async {
        do {
            try await recorder.prepare()
            try await recorder.record()
        } catch {
            print(error)
        }
    }

The following code does work:

    func startRecording() async {
        do {
           // this needs to be added
            let audioSession = AVAudioSession.sharedInstance()
            try audioSession.setCategory(.playAndRecord, mode: .default)
            try audioSession.setActive(true)
            try await recorder.prepare()
            try await recorder.record()
        } catch {
            print(error)
        }
    }

So this needs to be set better in the AudioRecorder's record function

    @MainActor
    public func record() async throws {
        // this seems to be set too late
        try _AVAudioSession.shared.setCategory(.record, mode: .default)
        try _AVAudioSession.shared.setActive(true)
        
        guard try base.record() else {
            // this error gets thrown on the first attempt to record
            throw _PlaceholderError()
        }
        
        self.state = .recording
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant