opentok / opentok-android-sdk-samples

Sample applications illustrating best practices using OpenTok Android SDK.
https://tokbox.com/developer/sdks/android/
MIT License
211 stars 169 forks source link

Publisher.cycleCamera() crashes #134

Closed lby1992 closed 7 years ago

lby1992 commented 7 years ago

Hi, I have a problem of camera toggle. I provide a button to toggle camera. When user was publishing and clicked the toggle camera button, app crashes.

below is my code:

toggleButton.setOnClickListener(v -> mPublisher.cycleCamera());

and the crash logs:

Fatal Exception: java.lang.RuntimeException: Fail to connect to camera service
       at android.hardware.Camera.native_setup(Camera.java)
       at android.hardware.Camera.<init>(Camera.java:374)
       at android.hardware.Camera.open(Camera.java:329)
       at com.opentok.android.DefaultVideoCapturer.swapCamera(DefaultVideoCapturer.java:298)
       at com.opentok.android.DefaultVideoCapturer.cycleCamera(DefaultVideoCapturer.java:278)
       at com.opentok.android.Publisher.cycleCamera(Publisher.java:518)
       at com.fancred.android.live.LivePublisherActivity.toggleCamera(LivePublisherActivity.java:299)
       at com.fancred.android.live.LivePublisherActivity.onClick(LivePublisherActivity.java:307)
       at android.view.View.performClick(View.java:4438)
       at android.view.View$PerformClick.run(View.java:18441)
       at android.os.Handler.handleCallback(Handler.java:733)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:136)
       at android.app.ActivityThread.main(ActivityThread.java:5111)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:515)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
       at dalvik.system.NativeStart.main(NativeStart.java)

sdk version is 2.11.0, my phone has two cameras

hope someone help me, thanks

marinaserranomontes commented 7 years ago

Hi @lby1992,

What device model are you using?

lby1992 commented 7 years ago

hi, @marinaserranomontes, this occur on each my device, I think it's caused by the incorrect code. I have adjusted my code, now it seems working fine:

 BaseVideoCapturer capturer = livePublisher.getCapturer();
        if (capturer.isCaptureStarted()) {
            capturer.stopCapture();
        }
        capturer.destroy();
        livePublisher.cycleCamera();
        capturer.init();
        capturer.startCapture();

If it's the correct way to toggle camera

marinaserranomontes commented 7 years ago

hi @lby1992 ,

You don't need to manage the capturer if you want to cycle the camera. If the publisher exists, you should only call the publisher.cycleCamera() and it should work. You can find an example here: https://github.com/opentok/opentok-android-sdk-samples/blob/bdec9e5d14549134c75b28d89f500d921f08d53d/Simple-Multiparty/app/src/main/java/com/tokbox/android/tutorials/simple_multiparty/MainActivity.java#L60

lby1992 commented 7 years ago

@marinaserranomontes I just called the same as sample code before, but it crashed. I search on Google with the exception message, and then manage the capturer by myself.

marinaserranomontes commented 7 years ago

@lby1992 just to reproduce and debug it, please, could you detail the Android versions and device models you are using, please? Thanks!!

lby1992 commented 7 years ago

hi @marinaserranomontes , below models I am using:

1.ZTE N958St, api 19 2.Emulator Nexus_5x_API_24

lby1992 commented 7 years ago

I am looking for more device to try this

marinaserranomontes commented 7 years ago

Hi @lby1992 ,

I am not able to reproduce it with the emulator and API 24. We are working on the possible camera swap exceptions for the next release.

Thanks for your feedback.

lby1992 commented 7 years ago

@marinaserranomontes Ok, thanks for your help.