편집

다음을 통해 공유


AVCaptureSession.BeginConfiguration Method

Definition

Initiates a transaction to change the configuration of the Capture Session.

[Foundation.Export("beginConfiguration")]
public virtual void BeginConfiguration ();
abstract member BeginConfiguration : unit -> unit
override this.BeginConfiguration : unit -> unit
Attributes

Remarks

This method and CommitConfiguration() must be used if the application developer wishes to change the configuration of a running session. For instance, to swap between front and back cameras, code such as the following can be used:

AVCaptureMovieFileOutput movieFileOutput;

AVCaptureDevice CurrentCamera { get; set; }
AVCaptureDevice BackCamera { get; set; }
AVCaptureDevice FrontCamera { get; set; }
AVCaptureDevice Mic { get; set; }

bool HasBackCamera { get { return BackCamera != null; } }
bool HasFrontCamera { get { return FrontCamera != null; } }
bool HasMic { get { return Mic != null; } }

void SetDeviceProperties()
{
	//Set up the devices
	foreach(var device in AVCaptureDevice.DevicesWithMediaType(AVMediaType.Video))
	{
		if(device.Position == AVCaptureDevicePosition.Back)
		{
			BackCamera = device;
		}
		else
		{
			FrontCamera = device;
		}
	}
	Mic = AVCaptureDevice.DefaultDeviceWithMediaType(AVMediaType.Audio);
}

protected void StopRecording()
{
	if(movieFileOutput != null && movieFileOutput.Recording)
	{
		movieFileOutput.StopRecording();
	}
	RecordingFinished(this, new EventArgs());
}

public bool SwapCameras()
{
	StopRecording();
	if(HasBackCamera && HasFrontCamera)
	{
		var nextCamera = CurrentCamera == BackCamera ? FrontCamera : BackCamera;
		NSError error = null;
		var newInput = new AVCaptureDeviceInput(nextCamera, out error);
		if(error != null)
		{
			throw new Exception(error.ToString());
		}
		session.BeginConfiguration();
		//Remove current video input
		foreach(AVCaptureDeviceInput input in session.Inputs)
		{
			if(input.Device.HasMediaType(AVMediaType.Video))
			{
				session.RemoveInput(input);
			}
		}
		if(session.CanAddInput(newInput))
		{
			session.AddInput(newInput);
		}
		session.CommitConfiguration();
		CurrentCamera = nextCamera;
		CameraConfigured(this, new TArgs<AVCaptureDevice>(CurrentCamera));
	}
	return CurrentCamera == FrontCamera;
}

Applies to