moslienko/avrecorderwrapper
A wrapper for recording an audio file.
Table of Contents
Init record Record Control Available properties Observers Error handling Recording states * Check permission
Installation
The library requires a dependency AppViewUtilits.
Swift Package Manager
To integrate using Apple's Swift Package Manager, add the following as a dependency to your Package.swift:
dependencies: [
.package(url: "https://github.com/moslienko/AVRecorderWrapper.git", from: "1.0.0")
]Alternatively navigate to your Xcode project, select Swift Packages and click the + icon to search for AVRecorderWrapper.
Manually
If you prefer not to use any of the aforementioned dependency managers, you can integrate AVRecorderWrapper into your project manually. Simply drag the Sources Folder into your Xcode project.
Example
The example application is the best way to see AVRecorderWrapper in action. Simply open the AVRecorderWrapperExample.xcodeproj and run the AVRecorderWrapperExample scheme.
Usage
Init record
Before you begin recording, you need to initialize it via the start method. Specify the directory to which the audio file will be saved and (if necessary) your own settings for AVAudioRecorder.
func start(path: AudioAssetsPath, recorderSettings: [String: Any] = [:])For example:
AVRecorderWrapper.shared.start(path: .temp(fileName: "\(Date().timeIntervalSince1970).m4a"))Record Control
Attempts to start recording.
func tryStartRecord()Pauses the recording process.
func pauseRecord()Attempts to continue recording.
func tryContinueRecord()Pauses or continues the recording depending on the current state.
func pauseOrContinueRecord()Stops the recording process.
func stopRecording()Stops the recording process.
func stopRecording()Available properties
A boolean indicating whether the recorder is currently recording.
var isRecording: BoolThe file path where the recorded audio will be saved.
var path: AudioAssetsPath?Duration of the current recording.
var recordDuration: TimeIntervalObservers
To get callbacks from the library, you can use the setObservers method or the AVRecorderWrapperDelegate delegate setting.
AVRecorderWrapper.shared.delegate = self AVRecorderWrapper.shared.setObservers(
didStartRecording: {
/// A callback that is triggered when recording starts.
},
didFinishRecording: {
/// A callback that is triggered when recording finishes.
},
didUpdateState: { state in
/// A callback that is triggered to update the recorder state.
},
didUpdateTime: { time in
/// A callback that is triggered to update the elapsed recording time.
},
didHandleRecorderError: { error in
// A callback that is triggered when an error is received during the audio recording process.
}
)Error handling
enum RecorderError: Error {
/// The path to the file to be recorded is missing
case missingFilePath
/// Standard error model
case systemError(_ error: Error)
/// Cannot create an audio session
case failedStartAudioSession
/// Unknown error
case unknownError
}Recording states
enum RecorderState {
/// The recorder is currently recording audio.
case recording
/// The recorder has been stopped and is not recording.
case stopped
/// The user has denied permission to record audio.
case denied
/// The recording is temporarily paused.
case paused
/// The recording has been resumed after being paused.
case continued
}Check permission
Checking and calling the microphone access authorization:
AVRecorderWrapper.shared.checkPermission { isGranted in
...
}You may not call checkPermission before starting or continuing a record, the library will do the check itself and return .denied in updateState (delegate) or didUpdateState (callback) if permission has been rejected.
Don't forget to add the field to your info.plist:
<key>NSMicrophoneUsageDescription</key>
<string>...</string>License
AVRecorderWrapper
Copyright (c) 2024 Pavel Moslienko 8676976+moslienko@users.noreply.github.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.Package Metadata
Repository: moslienko/avrecorderwrapper
Default branch: master
README: README.md