Avinash Shinde Avinash Shinde - 22 days ago 22
Android Question

Android MediaRecorder Stop() function gives error E/MediaRecorder: stop failed: -1007

This piece of code works great in debug mode, but when not is debug mode it always throws Runtime exception.

mMediaRecorder.stop();


According to Java doc :

Stops recording. Call this after start(). Once recording is stopped,
you will have to configure it again as if it has just been constructed.
Note that a RuntimeException is intentionally thrown to the
application, if no valid audio/video data has been received when stop()
is called. This happens if stop() is called immediately after
start(). The failure lets the application take action accordingly to
clean up the output file (delete the output file, for instance), since
the output file is not properly constructed when this happens.

@throws IllegalStateException if it is called before start()


does this means media recorder is not getting valid data?

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setTitle("Snapshot record");

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
mScreenDensity = metrics.densityDpi;

mActivity = this;

initializeCamera();
}

private void initializeCamera() {
if (checkCameraHardware(mActivity)) {
mCamera = openFrontFacingCamera();
} else {
Toast.makeText(mActivity, "Camera not available.", Toast.LENGTH_LONG).show();
}

Camera.Parameters parameters = mCamera.getParameters();
List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();
Camera.Size cs = sizes.get(sizes.size() - 1);
parameters.setPreviewSize(cs.width, cs.height);
mCamera.setParameters(parameters);

mCameraPreview = new CameraPreview(this, mCamera);

FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mCameraPreview);
}

private boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
return true;
} else {
return false;
}
}

private Camera openFrontFacingCamera() {
int cameraCount = 0;
Camera cam = null;
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
cameraCount = Camera.getNumberOfCameras();
for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
Camera.getCameraInfo(camIdx, cameraInfo);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
try {
cam = Camera.open(camIdx);
} catch (RuntimeException e) {
Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());
}
}
}

return cam;
}


public void onSurfaceCreated() {
startCameraRecording();
}

private void startCameraRecording() {
if (prepareVideoRecorder()) {
mMediaRecorder.start();
} else {
releaseMediaRecorder();
}
}

private boolean prepareVideoRecorder() {
mCamera.unlock();

mMediaRecorder = new MediaRecorder();
mMediaRecorder.setCamera(mCamera);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());
mMediaRecorder.setPreviewDisplay(mCameraPreview.getHolder().getSurface());

try {
mMediaRecorder.prepare();
} catch (IllegalStateException e) {
Log.d(TAG, "IllegalStateException preparing MediaRecorder: " + e.getMessage());
releaseMediaRecorder();
return false;
} catch (IOException e) {
Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage());
releaseMediaRecorder();
return false;
}
return true;
}

protected 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

mCamera.stopPreview();
}
}


CameraPreview Class

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = "CameraPreview";
private SurfaceHolder mHolder;
private Camera mCamera;
Activity activity;

public CameraPreview(Activity context, Camera camera) {
super(context);
mCamera = camera;
activity = context;
mHolder = getHolder();
mHolder.addCallback(this);

mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();

if (activity instanceof SnapShotActivity)
((SnapShotActivity) activity).onSurfaceCreated();

} catch (IOException e) {
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
}
}

public void surfaceDestroyed(SurfaceHolder holder) {
}

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

}


On method

protected void stopRecording() {
try {
if (mMediaRecorder == null)
return;
mMediaRecorder.stop(); // stop the recording
} catch (RuntimeException e) {
e.printStackTrace();
}
releaseMediaRecorder(); // release the MediaRecorder object
}


Stack trace :

W/System.err: java.lang.RuntimeException: stop failed.
W/System.err: at android.media.MediaRecorder.stop(Native Method)

Answer

Actually for me size set for Camcorder profile and size set to Media Recorder were not matching.

Camera.Parameters parameters = mCamera.getParameters();
CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
profile.videoFrameWidth = optimalSize.width;
profile.videoFrameHeight = optimalSize.height;

parameters.setPreviewSize(profile.videoFrameWidth, profile.videoFrameHeight);

Thank you.