how switch camera in ios?

how switch camera in ios?


            how switch camera in ios? 

          I use :

               self.myCamera = AVCaptureDevice.defaultDevice(withDeviceType: .builtInWideAngleCamera, mediaType: AVMediaTypeVideo, position: .front)

              let cameraid=self.myCamera?.uniqueID

              self.myCameraPar=RTCLocalCameraStreamParameters(videoEnabled: true, audioEnabled: true)
            // [parameters setResolutionWidth:352 height:288];
                self.myCameraPar?.setResolutionWidth(352, height: 288)
                self.localCanmerStream=RTCLocalCameraStream(parameters: self.myCameraPar!)

          I switch camera,but no success! 


 let session =  AVCaptureSession()
            do {
                let    Input1 = try AVCaptureDeviceInput(device: self.myCamera)
                let    Input2 = try AVCaptureDeviceInput(device: self.myCamera2)
                let b =  session.canAddInput(Input2)


 I look at API  "RTCAVFoundationVideoSource" ,but I can not use.

        let t = RTCAVFoundationVideoSource.init(constraints: <#T##RTCMediaConstraints!#>)
            t?.useBackCamera = true 



4 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.


I 've set source and localstream like below 

source = RTCAVFoundationVideoSource(constraints: mediaConstraints)

source?.useBackCamera = true

localStream = RTCLocalCameraStream(audioEnabled: true, videoSource: source!, error: nil)

and after this just use useBackCamera (true|false)  inside my changeCamera() function. I didn't use any cameraParameter.











p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo; color: #4f8187; background-color: #ffffff}
span.s1 {color: #000000}

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo; color: #4f8187; background-color: #ffffff}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; background-color: #ffffff; min-height: 14.0px}
p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo; color: #000000; background-color: #ffffff}
span.s1 {color: #000000}
span.s2 {color: #4f8187}
span.s3 {color: #ba2da2}

For iOS SDK , switch camera can be done through set useBackCamera as Yes /NO

Following is example code , you can include it to your SwitchCamera() function and it will switch camera over successive calls when i is even or odd

    static int i = 0;
    NSDictionary *mandatoryConstraints = @{@"minWidth" : @"640", @"minHeight" : @"480",@"maxWidth" : @"640", @"maxHeight" : @"480", @"maxFrameRate":@"24", @"minFrameRate":@"24"};
    RTCMediaConstraints* constraints = [[RTCMediaConstraints alloc] initWithMandatoryConstraints:mandatoryConstraints optionalConstraints:nil];
    RTCAVFoundationVideoSource* source=[[RTCAVFoundationVideoSource alloc] initWithConstraints:constraints];
    if((i++)%2 == 1){
      source.useBackCamera = YES;
      source.useBackCamera = NO;
    NSError *err=[[NSError alloc]init];
    _localStream=[[RTCLocalCameraStream alloc]initWithAudioEnabled:YES VideoSource:source error: &err];




Hi , Thank you all。   My problem has been solved


 let mandatoryConstraints = ["minWidth":"640","minHeight":"480","maxWidth":"640","maxHeight":"480","maxFrameRate":"16","minFrameRate":"16"]
                let constraints = RTCMediaConstraints(mandatoryConstraints: mandatoryConstraints, optionalConstraints: nil)
                self.mVideoSource = RTCAVFoundationVideoSource(constraints: constraints)
                self.mVideoSource?.useBackCamera = false
                self.mIsBackCamera = false
                self.localCanmerStream=RTCLocalCameraStream(audioEnabled: true, videoSource: self.mVideoSource!, error: nil)


func switchCamera()
            if mIsBackCamera
                self.mVideoSource?.useBackCamera = false
                mIsBackCamera = false
                self.mVideoSource?.useBackCamera = true
                mIsBackCamera = true






How to switch camera in v4.0 of iOS client using ICSLocalStream?

Leave a Comment

Please sign in to add a comment. Not a member? Join today