Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-23935] iOS: Ti.Media.switchCamera() fails to switch camera.

GitHub Issuen/a
TypeBug
PriorityHigh
StatusClosed
ResolutionFixed
Resolution Date2016-09-29T06:50:28.000+0000
Affected Version/sRelease 5.4.0, Release 5.5.0
Fix Version/sRelease 6.0.0
ComponentsiOS
Labelscamera, iOS, qe-6.0.0, switchCamera
ReporterMotiur Rahman
AssigneeHans Knöchel
Created2016-09-21T18:22:56.000+0000
Updated2016-09-29T17:44:46.000+0000

Description

Titanium.Media.switchCamera() fails to load the front-facing camera. *Steps to Reproduce:* 1.Run Code
var win = Ti.UI.createWindow({
    layout: "vertical"
});
win.open();

var button = Ti.UI.createButton({
    title: "Camera",
    top: 20,
    width: 200,
    height: 200
});

button.addEventListener("click", takePics);

win.add(button);

var view = Ti.UI.createView();

var take = Ti.UI.createView({

    bottom: 20,
    backgroundColor: "white",
    width: 70,
    height: 70,
    borderRadius: 35

});

var switchCamera = Ti.UI.createButton({

    top: 30,
    title: "Switch",
    right: 30,
    backgroundColor: "red",
    width: 50,
    height: 50,
    borderRadius: 25

});

// Add to the parent view.
view.add(take);
view.add(switchCamera);

function takePics() {
    Ti.Media.showCamera({
        success: function(e) {
            Ti.API.info(e.media);

        },
        error: function(e) {
            alert(e);
        },
        autohide: false,
        overlay: view,
        saveToPhotoGallery: true,
        mediaTypes: [Ti.Media.MEDIA_TYPE_PHOTO],
        showControls: false
    });

}

take.addEventListener('click', function(e) {
    Ti.Media.takePicture();
});

//var value = true;
switchCamera.addEventListener('click', function() {
	Ti.API.info("Switch camera!");
    if (Ti.Media.camera == Ti.Media.CAMERA_FRONT) {
        Ti.Media.switchCamera(Ti.Media.CAMERA_REAR);
    } else {
        Ti.Media.switchCamera(Ti.Media.CAMERA_FRONT);
    }
});
2. Click 'Switch' button for test. *Expected Result:* The front (facing the user, showing their face on the screen) camera should load. *Actual Result:* The back camera loads only. *Note: It works on 5.3.1.GA version.* Thanks.

Comments

  1. Hans Knöchel 2016-09-24

    Note: Similar to TIMOB-17470, the camera can only be switched after the UI is displayed!
  2. eric harms 2016-09-26

    Hans -- Ive had something similar in our app prior to 5.5.0.GA and XCode8 -- and it worked. I added a _.delay() to wait for the UI to load, and the camera would switch. Now using IOS10 on the device and XCode 8 with Ti 5.5.0.GA, if I use the example above (and same with my app's code) and add the same kind of delay to wait for the UI to be loaded, Ti.Media.switchCamera(Ti.Media.CAMERA_FRONT) does not fire anymore on a device with IOS10. i can put other things in the event listener for the switchCamera button click event, and they do fire, so I know the click event is being handled. I can alert the current Ti.Media.camera, which comes up as 0 when the camera loads and the UI is ready. I updated the sample code of this ticket as follows, the _.delay() of applying the eventListener (to simulate that the UI has completed loading) is at the end. I can't get the camera to switch checking the current camera, or by setting it explicitly. Thanks, Eric TI 5.5.0.GA XCode 8 OSX Sierra 10.12 TI CLI 5.0.9 IPhone 6+ / IOS 10.0.1
       var win = Ti.UI.createWindow({
           layout: "vertical"
       });
       win.open();
        
       var button = Ti.UI.createButton({
           title: "Camera",
           top: 20,
           width: 200,
           height: 200
       });
        
       button.addEventListener("click", takePics);
        
       win.add(button);
        
       var view = Ti.UI.createView();
        
       var take = Ti.UI.createView({
        
           bottom: 20,
           backgroundColor: "white",
           width: 70,
           height: 70,
           borderRadius: 35
        
       });
        
       var switchCamera = Ti.UI.createButton({
        
           top: 30,
           title: "Switch",
           right: 30,
           backgroundColor: "red",
           width: 50,
           height: 50,
           borderRadius: 25
        
       });
        
       // Add to the parent view.
       view.add(take);
       view.add(switchCamera);
        
       function takePics() {
           Ti.Media.showCamera({
               success: function(e) {
                   Ti.API.info(e.media);
        
               },
               error: function(e) {
                   alert(e);
               },
               autohide: false,
               overlay: view,
               saveToPhotoGallery: true,
               mediaTypes: [Ti.Media.MEDIA_TYPE_PHOTO],
               showControls: false,
               allowEditing: false
           });
        
       }
        
       take.addEventListener('click', function(e) {
           Ti.Media.takePicture();
       });
       
       _.delay(function() {
       
           switchCamera.addEventListener('click', function() {
               alert(Ti.Media.camera);
               if (Ti.Media.camera == Ti.Media.CAMERA_FRONT) {
                   Ti.Media.switchCamera(Ti.Media.CAMERA_REAR);
                   alert('switch to rear camera');
                   Ti.Media.vibrate();
               } else {
                   Ti.Media.switchCamera(Ti.Media.CAMERA_FRONT);
                   alert('switch to front camera');
                   Ti.Media.vibrate();
               }
               alert('should switch')
       
               Ti.Media.switchCamera(Ti.Media.CAMERA_FRONT);
               Ti.Media.vibrate();
           });
       });
       
  3. eric harms 2016-09-27

    Hans, Nevermind my reply here -- I think there might be something else going on. Thanks
  4. eric harms 2016-09-27

    Hans, Follow up here -- the ability to be able to switch the camera seems to rely on having this property in tiapp.xml true, value must be true I need to ti clean when removing or adding it to see the effect, or the tiapp.xml does not get reset. without that property in tiapp.xml, Ti.Media.switchCamera(camera) does not seem to work. I tried this in my app, which was built before 5.1.0 so it did not have this property (looked like the property was new in 5.1.0.GA -- https://docs.appcelerator.com/platform/release-notes/?version=5.1.0.GA#js_thread). I also tried it with a clean test app using 5.5.0.GA and the code I added above in this thread, removing then adding the property to tiapp.xml, and can confirm it breaks then works as expected. thanks Eric
  5. Hans Knöchel 2016-09-28

    Hey Eric, good catch! I guess I know in which direction we can investigate now. Thanks!
  6. Hans Knöchel 2016-09-28

    [~capsizeno4] Fixed! The issue was that we did not ensure that the code is being executed from the correct thread (UI-thread). I also scanned the rest of the camera-API to ensure it includes the macro as well - it does. PR (master): https://github.com/appcelerator/titanium_mobile/pull/8443 [~cng] Please let me know if we can consider this fix for 6.0.0 as well. Demo:
       var win = Ti.UI.createWindow({
           layout: "vertical"
       });
       win.open();
       
       var button = Ti.UI.createButton({
           title: "Camera",
           top: 20,
           width: 200,
           height: 200
       });
       
       button.addEventListener("click", takePics);
       
       win.add(button);
       
       var view = Ti.UI.createView();
       
       var take = Ti.UI.createView({
           bottom: 20,
           backgroundColor: "white",
           width: 70,
           height: 70,
           borderRadius: 35
       });
       
       var switchCamera = Ti.UI.createButton({
           top: 30,
           title: "Switch",
           right: 30,
           backgroundColor: "red",
           tintColor: "white",
           width: 60,
           height: 60,
           borderRadius: 30
       });
       
       // Add to the parent view.
       view.add(take);
       view.add(switchCamera);
       
       function takePics() {
           Ti.Media.showCamera({
               success: function(e) {
                   Ti.API.info(e.media);
       
               },
               error: function(e) {
                   alert(e);
               },
               autohide: false,
               overlay: view,
               mediaTypes: [Ti.Media.MEDIA_TYPE_PHOTO],
               showControls: false
           });
       
       }
       
       take.addEventListener('click', function(e) {
           Ti.Media.takePicture();
       });
       
       //var value = true;
       switchCamera.addEventListener('click', function() {
           Ti.API.info("Switch camera!");
           if (Ti.Media.camera == Ti.Media.CAMERA_FRONT) {
               Ti.Media.switchCamera(Ti.Media.CAMERA_REAR);
           } else {
               Ti.Media.switchCamera(Ti.Media.CAMERA_FRONT);
           }
       });
       
  7. eric harms 2016-09-28

    excellent Hans nice one Eric
  8. Chee Kiat Ng 2016-09-29

    This is like a regression so yes we need it for 6.0.0.
  9. Hans Knöchel 2016-09-29

    PR (6_0_X): https://github.com/appcelerator/titanium_mobile/pull/8447
  10. Chee Kiat Ng 2016-09-29

    CR and FT passed. PRs merged.
  11. Josh Longton 2016-09-29

    Verified as fixed. Tested On: {noformat} iPhone 5 9.3.5 iPhone 6 10.0 Mac OSX El Capitan 10.12 Ti SDK: 6.0.0.v20160929031439 Appc Studio: 4.8.0.201609232005 Appc NPM: 4.2.8-7 App CLI: 6.0.0-54 Xcode 8.0 Node v4.4.7 {noformat} *Closing ticket.*

JSON Source