AVCaptureSession Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Coordinates a recording session.
[Foundation.Register("AVCaptureSession", true)]
[ObjCRuntime.Unavailable(ObjCRuntime.PlatformName.WatchOS, ObjCRuntime.PlatformArchitecture.All, null)]
[ObjCRuntime.Unavailable(ObjCRuntime.PlatformName.TvOS, ObjCRuntime.PlatformArchitecture.All, null)]
public class AVCaptureSession : Foundation.NSObject
type AVCaptureSession = class
inherit NSObject
- Inheritance
- Attributes
Remarks
The AVCaptureSession object coordinates the recording of video or audio input and passing the recorded information to one or more output objects. As the iOS line has advanced, different devices have gained multiple capture devices (in particular, gained multiple cameras). Application developers can use DefaultDeviceWithMediaType(String) or DevicesWithMediaType(String), passing in the constants defined in AVMediaType.
Configuring capture consists of setting the Inputs and Outputs properties of the AVCaptureSession. Notice that multiple AVCaptureInputs and AVCaptureOutputs are possible. For instance, to capture both audio and video, one would use two capture inputs:
var session = new AVCaptureSession();
var camera = AVCaptureDevice.DefaultDeviceWithMediaType(AVMediaType.Video);
var mic = AVCaptureDevice.DefaultDeviceWithMediaType(AVMediaType.Audio);
if(camera == null || mic == null){
throw new Exception("Can't find devices");
}
var cameraInput = AVCaptureDeviceInput.FromDevice (camera);
//info.plist _must_ contain NSMicrophoneUsageDescription key
var micInput = AVCaptureDeviceInput.FromDevice (mic);
if(session.CanAddInput(cameraInput)){
session.AddInput(cameraInput);
}
if(session.CanAddInput(micInput)){
session.AddInput(micInput);
}
Note that permission to access the microphone (and in some regions, the camera) must be given by the user, requiring the developer to add the NSMicrophoneUsageDescription
to the application's info.plist file.
Video can be captured directly to file with AVCaptureMovieFileOutput. However, this class has no display-able data and cannot be used simultaneously with AVCaptureVideoDataOutput. Instead, application developers can use it in combination with a AVCaptureVideoPreviewLayer, as shown in the following example:
var layer = new AVCaptureVideoPreviewLayer (session);
layer.VideoGravity = AVLayerVideoGravity.ResizeAspectFill;
var cameraView = new UIView ();
cameraView.Layer.AddSublayer (layer);
var filePath = Path.Combine (Path.GetTempPath (), "temporary.mov");
var fileUrl = NSUrl.FromFilename (filePath);
var movieFileOutput = new AVCaptureMovieFileOutput ();
var recordingDelegate = new MyRecordingDelegate ();
session.AddOutput (movieFileOutput);
movieFileOutput.StartRecordingToOutputFile (fileUrl, recordingDelegate);
Application developers should note that the function StopRecording() is asynchronous; developers should wait until the FinishedRecording(AVCaptureFileOutput, NSUrl, NSObject[], NSError) delegate method before manipulating the file (for instance, before saving it to the Photos album with SaveToPhotosAlbum(String, UIVideo+SaveStatus) or WriteVideoToSavedPhotosAlbumAsync(NSUrl)).
public class MyRecordingDelegate : AVCaptureFileOutputRecordingDelegate
{
public override void FinishedRecording (AVCaptureFileOutput captureOutput, NSUrl outputFileUrl, NSObject [] connections, NSError error)
{
if (UIVideo.IsCompatibleWithSavedPhotosAlbum (outputFileUrl.Path))
{
var library = new ALAssetsLibrary ();
library.WriteVideoToSavedPhotosAlbum (outputFileUrl, (path, e2) =>
{
if (e2 != null)
{
new UIAlertView ("Error", e2.ToString (), null, "OK", null).Show ();
}
else
{
new UIAlertView ("Saved", "Saved to Photos", null, "OK", null).Show ();
File.Delete (outputFileUrl.Path);
}
});
}
else
{
new UIAlertView ("Incompatible", "Incompatible", null, "OK", null).Show ();
}
}
}
Application developers can configure one or more output ports for the captured data, and these can be still frames, video frames with timing information, audio samples, quicktime movie files, or can be rendered directly to a CoreAnimation layer.
Once the input and output components of the session are set, the actual processing is begun by calling the StartRunning() method.
void SetupCapture ()
/ configure the capture session for low resolution, change this if your code
// can cope with more data or volume
session = new AVCaptureSession () {
SessionPreset = AVCaptureSession.PresetMedium
};
// create a device input and attach it to the session
var captureDevice = AVCaptureDevice.DefaultDeviceWithMediaType (AVMediaType.Video);
var input = AVCaptureDeviceInput.FromDevice (captureDevice);
if (input == null){
Console.WriteLine ("No video input device");
return false;
}
session.AddInput (input);
// create a VideoDataOutput and add it to the sesion
var output = new AVCaptureVideoDataOutput () {
VideoSettings = new AVVideoSettings (CVPixelFormatType.CV32BGRA),
// If you want to cap the frame rate at a given speed, in this sample: 15 frames per second
MinFrameDuration = new CMTime (1, 15)
};
// configure the output
queue = new MonoTouch.CoreFoundation.DispatchQueue ("myQueue");
outputRecorder = new OutputRecorder ();
output.SetSampleBufferDelegateAndQueue (outputRecorder, queue);
session.AddOutput (output);
session.StartRunning ();
}
public class OutputRecorder : AVCaptureVideoDataOutputSampleBufferDelegate {
public override void DidOutputSampleBuffer (AVCaptureOutput captureOutput, CMSampleBuffer sampleBuffer, AVCaptureConnection connection)
{
try {
var image = ImageFromSampleBuffer (sampleBuffer);
// Do something with the image, we just stuff it in our main view.
AppDelegate.ImageView.BeginInvokeOnMainThread (delegate {
AppDelegate.ImageView.Image = image;
});
//
// Although this looks innocent "Oh, he is just optimizing this case away"
// this is incredibly important to call on this callback, because the AVFoundation
// has a fixed number of buffers and if it runs out of free buffers, it will stop
// delivering frames.
//
sampleBuffer.Dispose ();
} catch (Exception e){
Console.WriteLine (e);
}
}
UIImage ImageFromSampleBuffer (CMSampleBuffer sampleBuffer)
{
// Get the CoreVideo image
using (var pixelBuffer = sampleBuffer.GetImageBuffer () as CVPixelBuffer){
// Lock the base address
pixelBuffer.Lock (0);
// Get the number of bytes per row for the pixel buffer
var baseAddress = pixelBuffer.BaseAddress;
int bytesPerRow = pixelBuffer.BytesPerRow;
int width = pixelBuffer.Width;
int height = pixelBuffer.Height;
var flags = CGBitmapFlags.PremultipliedFirst | CGBitmapFlags.ByteOrder32Little;
// Create a CGImage on the RGB colorspace from the configured parameter above
using (var cs = CGColorSpace.CreateDeviceRGB ())
using (var context = new CGBitmapContext (baseAddress,width, height, 8, bytesPerRow, cs, (CGImageAlphaInfo) flags))
using (var cgImage = context.ToImage ()){
pixelBuffer.Unlock (0);
return UIImage.FromImage (cgImage);
}
}
}
}
Constructors
AVCaptureSession() |
Default constructor that initializes a new instance of this class with no parameters. |
AVCaptureSession(IntPtr) |
A constructor used when creating managed representations of unmanaged objects; Called by the runtime. |
AVCaptureSession(NSObjectFlag) |
Constructor to call on derived classes to skip initialization and merely allocate the object. |
Properties
AutomaticallyConfiguresApplicationAudioSession |
Whether the AVCaptureSession uses the app's shared audio session. |
AutomaticallyConfiguresCaptureDeviceForWideColor |
Gets or sets a Boolean value that controls whether or not the session should automatically use wide color if it is available. |
Class | (Inherited from NSObject) |
ClassHandle |
The handle for this class. |
DebugDescription |
A developer-meaningful description of this object. (Inherited from NSObject) |
Description |
Description of the object, the Objective-C version of ToString. (Inherited from NSObject) |
DidStartRunningNotification |
Notification constant for DidStartRunning |
DidStopRunningNotification |
Notification constant for DidStopRunning |
ErrorKey |
Represents the value associated with the constant AVCaptureSessionErrorKey |
Handle |
Handle (pointer) to the unmanaged object representation. (Inherited from NSObject) |
Inputs |
Inputs to the capture session. |
Interrupted |
Whether the session has been interrupted. |
InterruptionEndedNotification |
Notification constant for InterruptionEnded |
InterruptionReasonKey |
Gets a key that accesses the reason that a capture session was interrupted. |
InterruptionSystemPressureStateKey | |
IsDirectBinding | (Inherited from NSObject) |
IsProxy | (Inherited from NSObject) |
MasterClock |
A read-only clock that provides a timebase for synchronizing timestamps from multiple input devices. |
Outputs |
Outputs for the capture session. |
Preset1280x720 |
Represents the value associated with the constant AVCaptureSessionPreset1280x720 |
Preset1920x1080 |
Represents the value associated with the constant AVCaptureSessionPreset1920x1080 |
Preset320x240 | |
Preset352x288 |
Represents the value associated with the constant AVCaptureSessionPreset352x288 |
Preset3840x2160 |
Represents the value associated with the constant AVCaptureSessionPreset3840x2160. |
Preset640x480 |
Represents the value associated with the constant AVCaptureSessionPreset640x480 |
Preset960x540 | |
PresetHigh |
Represents the value associated with the constant AVCaptureSessionPresetHigh |
PresetiFrame1280x720 |
Represents the value associated with the constant AVCaptureSessionPresetiFrame1280x720 |
PresetiFrame960x540 |
Represents the value associated with the constant AVCaptureSessionPresetiFrame960x540 |
PresetInputPriority |
Represents the value associated with the constant AVCaptureSessionPresetInputPriority |
PresetLow |
Represents the value associated with the constant AVCaptureSessionPresetLow |
PresetMedium |
Represents the value associated with the constant AVCaptureSessionPresetMedium |
PresetPhoto |
Represents the value associated with the constant AVCaptureSessionPresetPhoto |
RetainCount |
Returns the current Objective-C retain count for the object. (Inherited from NSObject) |
Running |
Whether the capture session is currently running. |
RuntimeErrorNotification |
Notification constant for RuntimeError |
Self | (Inherited from NSObject) |
SessionPreset |
The session preset. |
Superclass | (Inherited from NSObject) |
SuperHandle |
Handle used to represent the methods in the base class for this NSObject. (Inherited from NSObject) |
UsesApplicationAudioSession |
Whether the session should use the application's shared audio session. |
WasInterruptedNotification |
Notification constant for WasInterrupted |
Zone | (Inherited from NSObject) |
Methods
AddConnection(AVCaptureConnection) |
Adds the specified capture |
AddInput(AVCaptureInput) |
Adds an input to the Capture Session |
AddInputWithNoConnections(AVCaptureInput) |
Adds the specified |
AddObserver(NSObject, NSString, NSKeyValueObservingOptions, IntPtr) |
Registers an object for being observed externally (using NSString keyPath). Observed changes are dispatched to the observer’s object ObserveValue(NSString, NSObject, NSDictionary, IntPtr) method. (Inherited from NSObject) |
AddObserver(NSObject, String, NSKeyValueObservingOptions, IntPtr) |
Registers an object for being observed externally (using string keyPath). Observed changes are dispatched to the observer’s object ObserveValue(NSString, NSObject, NSDictionary, IntPtr) method. (Inherited from NSObject) |
AddObserver(NSString, NSKeyValueObservingOptions, Action<NSObservedChange>) |
Registers an object for being observed externally using an arbitrary method. (Inherited from NSObject) |
AddObserver(String, NSKeyValueObservingOptions, Action<NSObservedChange>) |
Registers an object for being observed externally using an arbitrary method. (Inherited from NSObject) |
AddOutput(AVCaptureOutput) |
Adds an output to the capture session. |
AddOutputWithNoConnections(AVCaptureOutput) |
Adds the specified |
AwakeFromNib() |
Called after the object has been loaded from the nib file. Overriders must call base.AwakeFromNib(). (Inherited from NSObject) |
BeginConfiguration() |
Initiates a transaction to change the configuration of the Capture Session. |
BeginInvokeOnMainThread(Action) | (Inherited from NSObject) |
BeginInvokeOnMainThread(Selector, NSObject) |
Invokes asynchrously the specified code on the main UI thread. (Inherited from NSObject) |
Bind(NSString, NSObject, String, NSDictionary) | (Inherited from NSObject) |
Bind(String, NSObject, String, NSDictionary) |
Obsolete.
(Inherited from NSObject)
|
BindingInfo(String) |
Obsolete.
(Inherited from NSObject)
|
BindingOptionDescriptions(String) |
Obsolete.
(Inherited from NSObject)
|
BindingValueClass(String) |
Obsolete.
(Inherited from NSObject)
|
CanAddConnection(AVCaptureConnection) |
Returns |
CanAddInput(AVCaptureInput) |
Returns whether the given input can be added to the session. |
CanAddOutput(AVCaptureOutput) |
Returns whether the given input can be added to the session. |
CanSetSessionPreset(NSString) |
Whether the session supports a particular set of predefined capture settings. |
CommitConfiguration() |
Applies atomically all the configuration changes that were made to the Capture Session since BeginConfiguration(). |
CommitEditing() | (Inherited from NSObject) |
CommitEditing(NSObject, Selector, IntPtr) | (Inherited from NSObject) |
ConformsToProtocol(IntPtr) |
Invoked to determine if this object implements the specified protocol. (Inherited from NSObject) |
Copy() |
Performs a copy of the underlying Objective-C object. (Inherited from NSObject) |
DangerousAutorelease() | (Inherited from NSObject) |
DangerousRelease() | (Inherited from NSObject) |
DangerousRetain() | (Inherited from NSObject) |
DidChange(NSKeyValueChange, NSIndexSet, NSString) |
Indicates a change occurred to the indexes for a to-many relationship. (Inherited from NSObject) |
DidChange(NSString, NSKeyValueSetMutationKind, NSSet) | (Inherited from NSObject) |
DidChangeValue(String) |
Indicates that a change occurred on the specified key. (Inherited from NSObject) |
Dispose() |
Releases the resources used by the NSObject object. (Inherited from NSObject) |
Dispose(Boolean) |
Releases the resources used by the NSObject object. (Inherited from NSObject) |
DoesNotRecognizeSelector(Selector) |
Indicates that this object does not recognize the specified selector. (Inherited from NSObject) |
Equals(NSObject) | (Inherited from NSObject) |
Equals(Object) | (Inherited from NSObject) |
ExposedBindings() | (Inherited from NSObject) |
GetBindingInfo(NSString) | (Inherited from NSObject) |
GetBindingOptionDescriptions(NSString) | (Inherited from NSObject) |
GetBindingValueClass(NSString) | (Inherited from NSObject) |
GetDictionaryOfValuesFromKeys(NSString[]) |
Retrieves the values of the specified keys. (Inherited from NSObject) |
GetHashCode() |
Generates a hash code for the current instance. (Inherited from NSObject) |
GetMethodForSelector(Selector) | (Inherited from NSObject) |
GetNativeField(String) |
Obsolete.
(Inherited from NSObject)
|
GetNativeHash() | (Inherited from NSObject) |
Init() | (Inherited from NSObject) |
InitializeHandle(IntPtr, String) | (Inherited from NSObject) |
InitializeHandle(IntPtr) | (Inherited from NSObject) |
Invoke(Action, Double) | (Inherited from NSObject) |
Invoke(Action, TimeSpan) | (Inherited from NSObject) |
InvokeOnMainThread(Action) | (Inherited from NSObject) |
InvokeOnMainThread(Selector, NSObject) |
Invokes synchrously the specified code on the main UI thread. (Inherited from NSObject) |
IsEqual(NSObject) | (Inherited from NSObject) |
IsKindOfClass(Class) | (Inherited from NSObject) |
IsMemberOfClass(Class) | (Inherited from NSObject) |
MarkDirty() |
Promotes a regular peer object (IsDirectBinding is true) into a toggleref object. (Inherited from NSObject) |
MutableCopy() |
Creates a mutable copy of the specified NSObject. (Inherited from NSObject) |
ObjectDidEndEditing(NSObject) | (Inherited from NSObject) |
ObserveValue(NSString, NSObject, NSDictionary, IntPtr) |
Indicates that the value at the specified keyPath relative to this object has changed. (Inherited from NSObject) |
PerformSelector(Selector, NSObject, Double, NSString[]) | (Inherited from NSObject) |
PerformSelector(Selector, NSObject, Double) |
Invokes the selector on the current instance and if the |
PerformSelector(Selector, NSObject, NSObject) | (Inherited from NSObject) |
PerformSelector(Selector, NSObject) | (Inherited from NSObject) |
PerformSelector(Selector, NSThread, NSObject, Boolean, NSString[]) | (Inherited from NSObject) |
PerformSelector(Selector, NSThread, NSObject, Boolean) | (Inherited from NSObject) |
PerformSelector(Selector) | (Inherited from NSObject) |
PrepareForInterfaceBuilder() | (Inherited from NSObject) |
RemoveConnection(AVCaptureConnection) |
Removes the specified |
RemoveInput(AVCaptureInput) |
Removes the specified input source. |
RemoveObserver(NSObject, NSString, IntPtr) |
Stops the specified observer from receiving further notifications of changed values for the specified keyPath and context. (Inherited from NSObject) |
RemoveObserver(NSObject, NSString) |
Stops the specified observer from receiving further notifications of changed values for the specified keyPath. (Inherited from NSObject) |
RemoveObserver(NSObject, String, IntPtr) |
Stops the specified observer from receiving further notifications of changed values for the specified keyPath and context. (Inherited from NSObject) |
RemoveObserver(NSObject, String) |
Stops the specified observer from receiving further notifications of changed values for the specified keyPath. (Inherited from NSObject) |
RemoveOutput(AVCaptureOutput) |
Removes the specified output. |
RespondsToSelector(Selector) |
Whether this object recognizes the specified selector. (Inherited from NSObject) |
SetNativeField(String, NSObject) |
Obsolete.
(Inherited from NSObject)
|
SetNilValueForKey(NSString) |
Sets the value of the specified key to null. (Inherited from NSObject) |
SetValueForKey(NSObject, NSString) |
Sets the value of the property specified by the key to the specified value. (Inherited from NSObject) |
SetValueForKeyPath(IntPtr, NSString) |
A constructor used when creating managed representations of unmanaged objects; Called by the runtime. (Inherited from NSObject) |
SetValueForKeyPath(NSObject, NSString) |
Sets the value of a property that can be reached using a keypath. (Inherited from NSObject) |
SetValueForUndefinedKey(NSObject, NSString) |
Indicates an attempt to write a value to an undefined key. If not overridden, raises an NSUndefinedKeyException. (Inherited from NSObject) |
SetValuesForKeysWithDictionary(NSDictionary) |
Sets the values of this NSObject to those in the specified dictionary. (Inherited from NSObject) |
StartRunning() |
Starts the flow of inputs and outputs. |
StopRunning() |
Asynchronously stops the flow of inputs and outputs. |
ToString() |
Returns a string representation of the value of the current instance. (Inherited from NSObject) |
Unbind(NSString) | (Inherited from NSObject) |
Unbind(String) |
Obsolete.
(Inherited from NSObject)
|
ValueForKey(NSString) |
Returns the value of the property associated with the specified key. (Inherited from NSObject) |
ValueForKeyPath(NSString) |
Returns the value of a property that can be reached using a keypath. (Inherited from NSObject) |
ValueForUndefinedKey(NSString) |
Indicates an attempt to read a value of an undefined key. If not overridden, raises an NSUndefinedKeyException. (Inherited from NSObject) |
WillChange(NSKeyValueChange, NSIndexSet, NSString) |
Indicates that the values of the specified indices in the specified key are about to change. (Inherited from NSObject) |
WillChange(NSString, NSKeyValueSetMutationKind, NSSet) | (Inherited from NSObject) |
WillChangeValue(String) |
Indicates that the value of the specified key is about to change. (Inherited from NSObject) |