This question applies to Android phones, but I may have the same question for iOS and UWP in the future. So a solution to my problem regarding the other platforms would be appreciated, but not required.
I am using the camera2 package to try to develop a camera application for Android phones and using Kotlin for it. One of the things I'm trying to do is to have the app find a camera device that is available for use at the current moment so it can initialize it and start doing camera stuff.
I see that the function getCameraIdList will return an array of camera devices currently connected to the device. However, the array will include those that are currently in use by other applications or API clients. Those camera devices in use can't be used by a different application until it's released by the original client. I was thinking that maybe I can use a loop to iterate over the string array returned by that function (starting with the camera device on the back of the phone) until the application finds one that is available. I can't find a way however.
I see that there is a class called AvailabilityCallback that could notify if a certain camera device is available. Although it seems that this callback would be used if the camera device was unavailable before. Also I wouldn't know if this is the right approach.
With StateCallback, which is a parameter for openCamera, contains a method for what the computer should do if an error occurs; and it would be ERROR_CAMERA_IN_USE in this case. The code below would be something like this:
callback: StateCallback() {
onClosed(camera: CameraDevice){
//do stuff
}
onDisconnected(camera: CameraDevice){
//do stuff
}
onError(camera: CameraDevice){
//Try a different camera device when ERROR_CAMERA_IN_USE occurs
}
onOpened(camera: CameraDevice){
//do stuff
}
}
fun openCamera(cameraId: String, callback, handler: Handler?){
}
I don't know if this is the right approach either.
I also thought about having the application use openCamera more than once if the first camera wasn't available but I imagine it would be expensive for it to do.
Knowing how to find and use an available camera device using camera2 is what I'm trying to do.