HKStreamRecorder
public actor HKStreamRecorder
extension HKStreamRecorder: HKStreamOutput
extension HKStreamRecorder: MediaMixerOutput
An actor represents video and audio recorder.
This actor is compatible with both HKStreamOutput and MediaMixerOutput. This means it can record the output from MediaMixer in addition to HKStream.
// An example of recording MediaMixer.
let recorder = HKStreamRecorder()
let mixer = MediaMixer()
mixer.addOutput(recorder)
// An example of recording streaming.
let recorder = HKStreamRecorder()
let mixer = MediaMixer()
let stream = RTMPStream()
mixer.addOutput(stream)
stream.addOutput(recorder)
-
The error domain codes.
See moreDeclaration
Swift
public enum Error : Swift.Error
-
The default recording settings.
Declaration
Swift
public static let defaultSettings: [AVMediaType : [String : any Sendable]]
-
The recorder settings.
Declaration
Swift
public private(set) var settings: [AVMediaType : [String : any Sendable]] { get }
-
The recording output url.
Declaration
Swift
public var outputURL: URL? { get }
-
The recording or not.
Declaration
Swift
public private(set) var isRecording: Bool { get }
-
The the movie fragment interval in sec.
Declaration
Swift
public private(set) var movieFragmentInterval: Double? { get }
-
Declaration
Swift
public private(set) var videoTrackId: UInt8? { get }
-
Declaration
Swift
public private(set) var audioTrackId: UInt8? { get }
-
The default file save location.
Declaration
Swift
public private(set) var moviesDirectory: URL { get }
-
The default file save location.
-
Creates a new recorder.
Declaration
Swift
public init()
-
Sets the movie fragment interval in sec.
This value allows the file to be written continuously, so the file will remain even if the app crashes or is forcefully terminated. A value of 10 seconds or more is recommended.
Declaration
Swift
public func setMovieFragmentInterval(movieFragmentInterval: Double?)
-
startRecording(_:
Asynchronoussettings: ) Starts recording.
For iOS, if the URL is unspecified, the file will be saved in .documentDirectory. You can specify a folder of your choice, but please use an absolute path.
try? await recorder.startRecording(nil) // -> $documentDirectory/B644F60F-0959-4F54-9D14-7F9949E02AD8.mp4 try? await recorder.startRecording(URL(string: "dir/sample.mp4")) // -> $documentDirectory/dir/sample.mp4 try? await recorder.startRecording(await recorder.moviesDirectory.appendingPathComponent("sample.mp4")) // -> $documentDirectory/sample.mp4 try? await recorder.startRecording(URL(string: "dir")) // -> $documentDirectory/dir/33FA7D32-E0A8-4E2C-9980-B54B60654044.mp4
Note
Folders are not created automatically, so it’s expected that the target directory is created in advance.Throws
Error.fileAlreadyExists
when case file already exists.Throws
Error.notSupportedFileType
when case species not supported format.Declaration
Swift
public func startRecording(_ url: URL? = nil, settings: [AVMediaType : [String : any Sendable]] = HKStreamRecorder.defaultSettings) async throws
Parameters
url
The file path for recording. If nil is specified, a unique file path will be returned automatically.
settings
Settings for recording.
-
stopRecording()
AsynchronousStops recording.
Example of saving to the Photos app.
do { let outputURL = try await recorder.stopRecording() PHPhotoLibrary.shared().performChanges({() -> Void in PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: outputURL) }, completionHandler: { _, error -> Void in try? FileManager.default.removeItem(at: outputURL) } } catch { print(error) }
Declaration
Swift
public func stopRecording() async throws -> URL
-
Declaration
Swift
public func selectTrack(_ id: UInt8?, mediaType: CMFormatDescription.MediaType)
-
Declaration
Swift
nonisolated public func mixer(_ mixer: MediaMixer, didOutput sampleBuffer: CMSampleBuffer)
-
Declaration
Swift
nonisolated public func mixer(_ mixer: MediaMixer, didOutput buffer: AVAudioPCMBuffer, when: AVAudioTime)