why lock camera when Mediarecorder is released - android

I don't know why camera object should be locked, since MediaRecorder object is released.
Here's the code:
private void releaseMediaRecorder(){
if (mMediaRecorder != null) {
mMediaRecorder.reset(); // clear recorder configuration
mMediaRecorder.release(); // release the recorder object
mMediaRecorder = null;
mCamera.lock(); // lock camera for later use
//
}
}
The result of the function are
1.camera is locked, so it can not be used to take photo or by other application?
2.if this application is paused, stopped or destroyed, camera is unlocked automatically?
This is the standard code from http://developer.android.com/guide/topics/media/camera.html#release-camera

If we see mCamera.lock(); is called in releaseMediaRecorder() method. The releaseMediaRecorder(); is called in onPause() method along with releaseCamera();.so, by seeing this you can understand that in releaseMediaRecorder() method media elements are enabled but the instance of the camera is created in releaseCamera() method till the instance is created our application should not use (or) cannot use the camera. That is why it is kept locked there and in releaseCamera() method if instance is not equal to null they release it.

Related

GLSurfaceView switching issue

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;
}
}

Android pause media player when user opens the camera and start recording video

I am developing music play app. When user push my app to background song is playing fine. Now if user opens the camera and starts recording video from camera, I need to pause the song playing from my app .How to do this?
I expect that the app responsible for video recording will request audio focus to notify other apps that they should cease playback. If this is the case,
you can implement AudioManager.OnAudioFocusChangeListener like this:
#Override
public void onAudioFocusChange(int focusChange)
{
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
{
// pause playback
}
else if (focusChange == AudioManager.AUDIOFOCUS_LOSS)
{
((AudioManager)getSystemService(Context.AUDIO_SERVICE)).abandonAudioFocus(this);
doStopPlayback();
}
// else if... listen to other types of audio focus loss/ gain
}
where doStopPlayback() is your method for releasing the MediaPlayer etc.
See also this guide for media playback.
You can check it using method Camera.open(cameraId).
Creates a new Camera object to access a particular hardware camera. If the same camera is opened by other applications, this will throw a RuntimeException.
Throws RuntimeException
If opening the camera fails (For Example, if the camera is in use by another process or device policy manager has disabled the camera).
Update:
Example:
public boolean isCameraUsebyApp() {
Camera camera = null;
try {
camera = Camera.open();
} catch (RuntimeException e) {
return true;
} finally {
if (camera != null) camera.release();
}
return false;
}
You can use this method to use as but keep this thing in mind that this method first acquire the camera.
If its acquire successfully then its mean that no other application is using this camera and don't forgot to release it again otherwise you will not able to acquire it again.
Its its throws RuntimeException it means that camera is in use by another process or device policy manager has disabled the camera.

How to reinitialize a camera object after is has been released outside of onCreate

I am using the camera flash for a Morse code application. I create a new camera object when the class is created. The user has a button which is used to reset and also release the camera when required (If they want to stop the light Morse sequence prematurely).
The problem is that when they hit the reset button because the activity is not created or loaded again the camera never get reinitialized - this is a problem because the method of the class that it is calling is used by another class and releases the camera when it is has completed it's function. I am not sure how to structure the code in a way that allows me to do this.
I am wondering if anyone has any advice/suggestions in how to achieve this?
//Camera object being declared
Light light;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity__morse);
//Camera object being initialized
light = new Light
}
//Reset user pressed a button
public void reset(View view)
{
light.release();
}
//Releasing the camera
public void release() {
if(camera != null)
{
camera.stopPreview();
camera.release();
camera = null;
}
}
Don't initialize the camera in onCreate. Do it in onStart, and release it in onStop.

Google Android Building A Camera App Tutorial

I'm currently going through the 'Building A Camera App' tutorial -http://developer.android.com/guide/topics/media/camera.html#custom-camera
As someone relatively new to android, I find it a bit confusing / unclear at times.
I'm trying to understand where this code is supposed to go:
private boolean isRecording = false;
// Add a listener to the Capture button
Button captureButton = (Button) findViewById(id.button_capture);
captureButton.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isRecording) {
// stop recording and release camera
mMediaRecorder.stop(); // stop the recording
releaseMediaRecorder(); // release the MediaRecorder object
mCamera.lock(); // take camera access back from MediaRecorder
// inform the user that recording has stopped
setCaptureButtonText("Capture");
isRecording = false;
} else {
// initialize video camera
if (prepareVideoRecorder()) {
// Camera is available and unlocked, MediaRecorder is prepared,
// now you can start recording
mMediaRecorder.start();
// inform the user that recording has started
setCaptureButtonText("Stop");
isRecording = true;
} else {
// prepare didn't work, release the camera
releaseMediaRecorder();
// inform user
}
}
}
}
);
Can this be anywhere within the top level class, or is this supposed to be inside one of the provided methods or inner classes?
No matter where I put this code it's causing errors telling me to add or remove '}', but I'm sure I must just have it in the wrong place, since I'm sure google's code is fine.
Any help much appreciated!
This code belongs in the activity that loads the layout containing the "button_capture" Button. One you find that activity, out this inside of the onCreate() method.
No matter where I put this code it's causing errors telling me to add or remove '}'
This is simply a matter of getting your braces paired up properly.

MediaRecorder.stop() hangs on Samsung Galaxy Camera

Calling stop() on my MediaRecorder hangs indefinitely on the Samsung Galaxy Camera. Placing this call in a separate thread does not help the problem either.
Logcat does not show any error messages. However, running this same app does not incur any problems on the Samsung Galaxy Nexus.
This is the code surrounding my call to stop:
View.OnClickListener captureListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isRecording) {
// stop recording and release camera
mMediaRecorder.stop();
releaseMediaRecorder(); // release the MediaRecorder object
mCamera.lock(); // take camera access back from MediaRecorder
// inform the user that recording has stopped
captureButton.setText("Capture");
isRecording = false;
} else {
// initialize video camera
if (prepareVideoRecorder()) {
// Camera is available and unlocked, MediaRecorder is prepared,
// now you can start recording
mMediaRecorder.start();
// inform the user that recording has started
captureButton.setText("Stop");
isRecording = true;
} else {
// prepare didn't work, release the camera
releaseMediaRecorder();
// inform user
}
}
}
};
One thing I saw is that for some devices MediaRecorder.stop() hangs if there is no preview attached (i.e. you called Camera.stopPreview() before or maybe you never called startPreview()).

Categories

Resources