/*
 * Copyright (C) 2013 Steelkiwi Development, Julia Zudikova, Viacheslav Tiagotenkov
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * 		http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.skd.androidrecording.video;

import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.util.Log;

import java.io.IOException;

/*
 * Manages media recorder recording 
 */

public class MediaRecorderManager {
	private static final String TAG = "MediaRecorderManager ";
	private MediaRecorder recorder;
	private boolean isRecording;

	public MediaRecorderManager() {
		recorder = new MediaRecorder();
	}

	public boolean startRecording(Camera camera, CamcorderProfile profile, String fileName, int cameraRotationDegree) {
		Log.i(TAG, String.format("start recording %dx%d -> %s",
				profile.videoFrameWidth, profile.videoFrameHeight, fileName));
		try {
			camera.unlock();
			recorder.setCamera(camera);
			recorder.setOrientationHint(cameraRotationDegree);
			recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
			recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
			recorder.setProfile(profile);
			recorder.setOutputFile(fileName);
			recorder.prepare();
			recorder.start();
			isRecording = true;
		} catch (IllegalStateException e) {
			e.printStackTrace();
			releaseRecorder();
		} catch (IOException e) {
			e.printStackTrace();
			releaseRecorder();
		} catch (RuntimeException e) {
			e.printStackTrace();
			releaseRecorder();
		}

		return isRecording;
	}

	public boolean stopRecording() {
		if (isRecording) {
			isRecording = false;
			recorder.stop();
			recorder.reset();
			return true;
		}
		return false;
	}

	public void releaseRecorder() {
		recorder.release();
		recorder = null;
	}

	public boolean isRecording() {
		return isRecording;
	}
}

@@ -1802,38 +1802,16 @@
 race();%0A
-%09%09%09releaseRecorder();%0A
 %09%09%7D catc
@@ -1822,32 +1822,32 @@
 OException e) %7B%0A
+
 %09%09%09e.printStackT
@@ -1854,38 +1854,16 @@
 race();%0A
-%09%09%09releaseRecorder();%0A
 %09%09%7D catc
@@ -1879,32 +1879,32 @@
 eException e) %7B%0A
+
 %09%09%09e.printStackT
@@ -1915,30 +1915,8 @@
 ();%0A
-%09%09%09releaseRecorder();%0A
 %09%09%7D%0A

fix Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaRecorder.release()' on a null object reference at com.skd.androidrecording.video.MediaRecorderManager.releaseRecorder(MediaRecorderManager.java:78) at com.skd.androidrecording.video.VideoRecordingManager.dispose(VideoRecordingManager.java:134) at com.skd.androidrecording.ui.VideoRecordingActivity.onDestroy(VideoRecordingActivity.java:209)