I am having an issue with while switching GLSurfaceView. I have a GLSurfaceView (connected with Camera) and I want to move it to another view , but when I am moving it it's losing its frames and showing blackScreen after switching. and when I am reconnecting it to Camera, it starts show my Camera but with delay.
Could any one please tell me solution to move it to another parent without losing its frames (because when I am reconnecting it its showing frames with 2-3 seconds delay).
Please find code written below :
(Here VideoPanel (extends GLSurfaceView) is Frame provided by oovooSDK to show video)
final VideoPanel movingVideoView = parent1.getChildAt(0);
movingView.onPause();
parent1.removeView(movingView);
parent2.addView(movingView);
movingView.onResume();
movingView.requestRender();
for reconnecting :
application.unbindVideoPanel(videoView.userId, videoView.videoRender);
application.unbindVideoPanel(videoView.userId, videoView.videoRender);
// this methods shows delay of 2-3 seconds for binding view again
I tried following methods also
((VideoPanel) videoView.videoRender).setPreserveEGLContextOnPause(true);
// to save context when paused
((VideoPanel)videoView.videoRender).setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); // to render frame again when requestRender is called
Camera is not proper release when your activity going to end.
If you are using a Surface view than release your camera in onSurfaceDestroy
public void surfaceDestroyed(SurfaceHolder holder) {
if(camera!=null){
camera.stopPreview();
camera.setPreviewCallback(null);
camera.release();
camera = null;
}
}
Also recommend a release your camera if it's not ever going to use.
protected void onDestroy(){
if(camera!=null){
camera.stopPreview();
camera.setPreviewCallback(null);
camera.release();
camera = null;
}
}
Related
i have develped a camera app which uses camera API from this link
its working perfectly it shows preview takes picture but when i finish the app it never releases camera ...i can't use camera solely after installing this app ...even the app get crashed when i restart it.
here's how i am releasing the camera-->
#Override
protected void onPause() {
super.onPause();
if (camera != null) {
// preview.setCamera(null);
camera.stopPreview();
camera.setPreviewCallback(null);
camera.release();
camera = null;
}
}
onResume i put this code also but didn't work..s
#Override
protected void onResume() {
super.onResume();
camera = Camera.open();
preview.setCamera(camera);
}
I have same problems on HTC One X (4.1.2) and Nexus 4(4.3), but on Galaxy Nexus (4.3) same release method works great.
I temporary solved this issue with using camera.unlock() instead of camera.release() and keeping static reference to Camera object.
I've searched all over on the web and I can't find out what that 1001 error is. A few seconds after that I get the camera 100 error but I can't find out what the first error is. Does anyone have any ideas?
I encountered this error as well on my S3. I believe I tracked it down to how the camera preview surface was used by the MediaRecorder. In my case the preview display was getting reset when I was attempting to start recording. I solved it by cleaning out my code and just used the calls to set, start and stop the preview display in the SurfaceView implementation below (from the Android Camera developer guide):
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview.
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
if (mHolder.getSurface() == null){
// preview surface does not exist
return;
}
// stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e){
// ignore: tried to stop a non-existent preview
}
// set preview size and make any resize, rotate or
// reformatting changes here
// start preview with new settings
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e){
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
}
}
Just thought I would add a post here for future reference. This issue bothered me for a long time.
It turns out that my problem was caused by an incorrect preview size, although the resolution set was obtained from the getSupportedPictureSize method.
So for example you can get the sizes as follows:
//first entry in list is 1392x1392 for front facing camera on an S3
List<Camera.Size> supportedPictureSizes = params.getSupportedPictureSizes();
Setting this resolution or neglecting to set a picture size alltogether will cause the dreaded error 1001.
If you encounter this on any other device I would recommend trying different picture sizes.
So there was another reason for why I got it on my Galaxy S3. I was using a TextureView to show my camera preview and got this dreaded error when pressing the home button after a successful preview and then entering the app again. In the onResume() function I started up the preview again and found that I had not released the SurfaceTexture instance variable in the onSurfaceTextureDestroyed() function.
I added the release line to this function and it now looks like this and works perfectly:
#Override public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
mSurfaceTexture = null; //This was the offending culprit.
releaseMediaPlayer();
releaseVideoRecorder();
releaseCamera();
return false;
}
In my case, in Samsung S3, the video-size parameter was not set and this led to the 1001 error. Setting the video size on the media recorder using preview size fixed the issue. However, this change may fail on other devices since the parameter may or may not be available/set in all devices. The following code addresses most of the devices:
if(params.get("video-size") != null && params.get("video-size").isEmpty()) {
int videoWidth = params.getPreviewSize().width;
int videoHeight = params.getPreviewSize().height;
mediaRecorder.setVideoSize(videoWidth, videoHeight);
} else {
mediaRecorder.setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight);
}
Using the following very simple camera preview activity (from a google example found here), the Nexus 4 camera is noticeably slower that the device's standard camera application:
public class LiveCameraActivity extends Activity implements TextureView.SurfaceTextureListener {
private Camera mCamera;
private TextureView mTextureView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mTextureView = new TextureView(this);
mTextureView.setSurfaceTextureListener(this);
setContentView(mTextureView);
}
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
mCamera = Camera.open();
try {
mCamera.setPreviewTexture(surface);
mCamera.startPreview();
} catch (IOException ioe) {
// Something bad happened
}
}
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
// Ignored, Camera does all the work for us
}
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
mCamera.stopPreview();
mCamera.release();
return true;
}
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
// Invoked every time there's a new Camera preview frame
}
}
I have been having issues with my application's camera speed on the Nexus 4, but I see this problem on no other devices. I was concerned that this was a Jelly Bean 4.2 difference, but Galaxy Nexus phones running JB4.2 work as normal with no lag. I realize this example code uses a TextureView, but other phones do not experience lag with this example.
Any help would be greatly appreciated.
I have found that the preview frame rate can be increased to a normal rate by setting the recording hint to true, but this severely decreases camera performance especially in medium to low light situations with regard to color saturation and contrast (I am shocked at the quality of the front facing camera given that this is Google's latest and greatest attempt to compete with the iPhone 5 and her quality cameras... can't wait for the X Phone).
This is not a solution to the problem, but it is something of a band-aid for developers looking to avoid shocking frame rates.
If you set the picture-size to 3264x2448, this will fix it. Even if you don't ever take a picture the picture-size is important because ZSL is enabled by default. The default picture size is 640x480 and this causes the slow preview frame rate. Disabling ZSL makes the preview even faster which is why setting the recording hit to true helps, enabling HDR does the same thing.
Is there any way to correctly stop the camera service? This does not seem to work:
public void surfaceDestroyed(SurfaceHolder holder){
camera.stopPreview();
camera = null;
}
After doing that the camera does not work, even if I use the program that is preinstalled in my handset.
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
camera=null;
}
This works fine for me. Make sure your surfaceDestroyed() method is getting invoked.
If you are using the the preview callback you might want to deregister it in your surfaceDestroyed
mCamera.setPreviewCallback(null);
mCamera is supposed to be your Camera object
I'm using the Android APIDemo sample code.
When I run the CameraPreview example, at first it was giving me an error.
I traced that one down and the sample was working for a while.
Now, it no longer works. It says
ERROR/AndroidRuntime(2949): java.lang.RuntimeException: Fail to connect to camera service
What can be causing that? It happens when camera.open() is called.
Thanks,
Tee
Be sure to properly release all the aquired camera resources:
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (mCam != null) {
mCam.stopPreview();
mCam.setPreviewCallback(null);
mCam.release();
mCam = null;
}
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
if (mCam == null) {
mCam = Camera.open();
try {
mCam.setPreviewDisplay(holder);
// TODO test how much setPreviewCallbackWithBuffer is faster
mCam.setPreviewCallback(this);
} catch (IOException e) {
mCam.release();
mCam = null;
}
}
}
Make sure your <uses-permission> elements are in the proper positions in your AndroidManifest.xml file.
It happens if your activity does not close the camera properly in surfaceDestroyed or onConfigurationChanged etc...
Don't forget to do this everytime you go out of your activity:
if (camera!=null){
camera.stopPreview();
camera.release();
camera=null;
}
Another reason of this error is when you try to open camera but some other application or even your application is already using camera.
I also get this type of issue on a HTC device. To solve add this code:
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if (camera!=null)
{
camera.stopPreview();
camera.release();
camera=null;
}
}
And yet you cannot start camera then restart device.
Also, if you are using the emulator, make sure you have selected to Emulate the Front Camera and/or the Back Camera.
Android Virtual Device Manager->Select Device->Edit->Front Camera->Emulated
As others mention, you have to call release() on your camera object when you're finished.
I wasn't doing this initially, so I changed my code but it still gave me the same error. I was deploying directly to a physical handset and had to restart the phone before it worked
I also received this error when I was testing and stopped execution before reaching the point in code when the:
if (camera!=null){
camera.stopPreview();
camera.release();
camera=null;
}
was called. This then blocked the camera because it hadn't een released properly. My solution was to turn the camera off and back on again. You can confirm this is the case by trying to use the inbuilt Camera app in your phone. It won't work either because it is still busy.
Second #matt-burns however you might want to check that you're only trying to get the camera once. I had forgotten to comment out a line and was trying to launch two activities that would both try to obtain the camera.