AVCaptureSession

AVCaptureSession

一个管理捕获(capture)活动的对象,通过协调输入(input)设备的数据流动,来捕获输出(output)

iPhone里的输入设备都有啥?

各种相机,相机和其他传感器的组合,麦克风,未知类型

输入设备负责干啥?

负责把设备采集到信息转换成数据

一句话总结

把所有输入设备得到的数据比喻成🍎,那么AVCaptureSession就是果农掰掰,AVCaptureDevice就是果园。

可用范围:

  • iOS 4.0+

  • macOS 10.7+

  • Mac Catalyst 14.0+

Framework

  • AVFoundation

声明

class AVCaptureSession : NSObject

举个栗子

下面的代码片段演示了如何配置捕获设备来录制音频。

摘果子辣,先实例化一个AVCaptureSession对象,然后添加对应类型的输入对象(input)和输出对象(output)。

// 先找个果农伯伯
let captureSession = AVCaptureSession()

// 再找个类型是audio的果园
// 找不到就撒油那拉
guard let audioDevice = AVCaptureDevice.default(for: .audio) else { return }

do {
		// 再找个筐装果子
		let audioInput = try AVCaptureDeviceInput(device: audioDevice)    
		// 问问果农伯伯,这个筐他行不行(黑布林大李子甜不甜)
		if captureSession.canAddInput(audioInput) {
      	// 果农伯伯:这筐能用(😖 👍甜)
				captureSession.addInput(audioInput)    
		}
} catch {    
		// 配置失败并处理错误
}

一切准备完成后,调用 startRunning() 就开始拼命的摘果子,调用 stopRunning() 就停止摘果子

划重点

startRunning() 方法是一个阻塞调用,可能需要一些时间,建议开一个串行队列去执行,以便主队列不会被阻塞(保持 UI 响应)有关实现示例,请参阅 AVCam:构建相机应用程序

您可以使用 sessionPreset 属性来自定义输出的质量级别、比特率或其他设置。 最常见的捕获配置可通过会话预设获得; 但是,某些特殊选项(例如高帧率)需要直接在 AVCaptureDevice 实例上设置捕获格式。

话题

管理输入和输出

var inputs: [AVCaptureInput]

捕获session的输入

(当前的input筐)

func canAddInput(AVCaptureInput) -> Bool

返回一个Bool值,用来判断一个input能不能用在session中

(果农能不能用这个筐)

func addInput(AVCaptureInput)

把input加入到session中

(把筐给果农)

func removeInput(AVCaptureInput)

Removes a given input.

var outputs: [AVCaptureOutput\]

The capture session’s outputs.

func canAddOutput(AVCaptureOutput) -> Bool

Returns a Boolean value that indicates whether a given output can be added to the session.

func addOutput(AVCaptureOutput)

Adds a given output to the session.

func removeOutput(AVCaptureOutput)

Removes a given output.

Managing Running State

func startRunning()

Tells the receiver to start running.

func stopRunning()

Tells the receiver to stop running.

var isRunning: Bool

Indicates whether the receiver is running.

var isInterrupted: Bool

Indicates whether the receiver has been interrupted.

static let AVCaptureSessionRuntimeError: NSNotification.Name

Posted if an error occurred during a capture session.

static let AVCaptureSessionDidStartRunning: NSNotification.Name

Posted when a capture session starts.

static let AVCaptureSessionDidStopRunning: NSNotification.Name

Posted when a capture session stops.

static let AVCaptureSessionWasInterrupted: NSNotification.Name

Posted if a capture session is interrupted.

static let AVCaptureSessionInterruptionEnded: NSNotification.Name

Posted if an interruption to a capture session finishes.

let AVCaptureSessionErrorKey: String

Key to retrieve the error object from a AVCaptureSessionRuntimeError user info dictionary.

let AVCaptureSessionInterruptionReasonKey: String

Key to retrieve information about a capture interruption from a AVCaptureSessionWasInterrupted user info dictionary.

let AVCaptureSessionInterruptionSystemPressureStateKey: String

The key for retrieving information about system pressure factors that caused a capture session interruption.

enum AVCaptureSession.InterruptionReason

Constants identifying the reason a capture session was interrupted, found in an AVCaptureSessionWasInterrupted user info dictionary.

Configuration Change

func beginConfiguration()

Indicates the start of a set of configuration changes to be made atomically.

func commitConfiguration()

Commits a set of configuration changes.

Managing Session Presets

struct AVCaptureSession.Preset

Constants to define capture setting presets using the sessionPreset property.

var sessionPreset: AVCaptureSession.Preset

A constant value indicating the quality level or bit rate of the output.

func canSetSessionPreset(AVCaptureSession.Preset) -> Bool

Returns a Boolean value that indicates whether the session can use the given preset.

Managing Connections

var connections: [AVCaptureConnection\]

An array of connections managed by the capture session.

func addConnection(AVCaptureConnection)

Adds a given capture connection to the session.

func canAddConnection(AVCaptureConnection) -> Bool

Returns a Boolean value that indicates whether a given connection can be added to the receiver.

func addInputWithNoConnections(AVCaptureInput)

Adds a capture input to the session without forming any connections.

func addOutputWithNoConnections(AVCaptureOutput)

Adds a capture output to the session without forming any connections.

func removeConnection(AVCaptureConnection)

Removes a capture connection from the session.

Sharing the Application’s Audio Session

var usesApplicationAudioSession: Bool

Indicates whether the capture session will make use of the app’s shared audio session.

var automaticallyConfiguresApplicationAudioSession: Bool

A Boolean value that indicates whether the capture session automatically changes settings in the app’s shared audio session.

Synchronizing Multiple Inputs and Outputs

var masterClock: CMClock?

A clock object used for output synchronization.

Managing Color Spaces

var automaticallyConfiguresCaptureDeviceForWideColor: Bool

A Boolean value that specifies whether the session should automatically use wide-gamut color where available.

Relationships

Inherits From

See Also

Capture Sessions

Setting Up a Capture Session

Configure input devices, output media, preview views, and basic settings before capturing photos or video.

Accessing the Camera While Multitasking

Operate the camera in Split View, Slide Over, or Picture in Picture mode.

AVCam: Building a Camera App

Capture photos with depth data and record video using the front and rear iPhone and iPad cameras.

AVMultiCamPiP: Capturing from Multiple Cameras

Simultaneously record the output from the front and back cameras into a single movie file by using a multi-camera capture session.

AVCamBarcode: Detecting Barcodes and Faces

Identify machine readable codes or faces by using the camera.

class AVCaptureMultiCamSession

A capture session that supports simultaneous capture from multiple inputs of the same media type.

posted @ 2021-06-18 18:36  82年快乐肥宅  阅读(458)  评论(0)    收藏  举报