MediaRecorder, start failed: -19 - android

Has anyone managed to successfully record a video using MediaRecorder on Glass?
This is the code i am using in order to prepare the Recorder. I keep getting error -19.
recorder = new MediaRecorder();
recorder.setOutputFile(videoFile);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setVideoFrameRate(15);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
Thank you.
Update:
It seems to be a GDK bug. I have reported it and it got accepted. If you are having the same issue just star the bug report to stay informed:
https://code.google.com/p/google-glass-api/issues/detail?id=360

To start video capture on Glass it seems that you have to completely stop the video preview. If you are using a previewing camera, prior of doing anything with a MediaRecorder, just run:
try {
mCamera.setPreviewDisplay(null);
} catch (java.io.IOException ioe) {
Log.d(TAG, "IOException nullifying preview display: " + ioe.getMessage());
}
mCamera.stopPreview();
mCamera.unlock();
More info here: https://code.google.com/p/google-glass-api/issues/detail?id=360#c6

After much frustration and iteration, I'm pleased to report that it IS possible to use MediaRecorder on Glass XE12. The below code works for me on my Glass Version 1 running XE12:
mMediaRecorder = new MediaRecorder();
mMediaRecorder.setCamera(mCamera);
mMediaRecorder.setPreviewDisplay(preview.getHolder().getSurface());
mCamera.unlock();
// Step 2: Set sources
mMediaRecorder.setOnErrorListener(new android.media.MediaRecorder.OnErrorListener() {
public void onError(MediaRecorder mediarecorder1, int k, int i1)
{
Log.e(TAG,String.format("Media Recorder error: k=%d, i1=%d", k, i1));
}
});
mMediaRecorder.setVideoSource(0);
mMediaRecorder.setAudioSource(0);
mMediaRecorder.setOutputFormat(2);
mMediaRecorder.setVideoEncoder(2);
mMediaRecorder.setVideoEncodingBitRate(0x4c4b40);
mMediaRecorder.setVideoFrameRate(30);
mMediaRecorder.setVideoSize(1280, 720);
mMediaRecorder.setAudioChannels(2);
mMediaRecorder.setAudioEncoder(3);
mMediaRecorder.setAudioEncodingBitRate(0x17700);
mMediaRecorder.setAudioSamplingRate(44100);
mMediaRecorder.setMaxDuration(0);
mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());
mMediaRecorder.setMaxDuration(-1);
// Step 5: Set the preview output
// Step 6: Prepare configured MediaRecorder
try {
mMediaRecorder.prepare();
mMediaRecorder.start();
} 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;
} catch (Exception e) {
Log.d(TAG, "Unknown exception preparing MediaRecorder: " + e.getMessage());
releaseMediaRecorder();
return false;
}

When do you prepare the recorder, is it something that happens when you launch your application? If so, are you launching it with a voice trigger?
If both of those things are true, this might be an instance of the bug described here. Can you try the workaround in that thread (exponential backoff) and see if it works?

Related

Unable to start mediarecorder for front camera, it throws illegalStateException start failed: -38

What is the meaning of start failed: -38?
I created two CameraView(Preview) Objects and one Camera Object.
Using Handlers I am able to switch the camera for some times, say, 10 secs open back camera and 10 secs open front camera.
Now I am facing some issue in recording front camera.
When I click the record button, first it will record back camera for 10 secs, and save it into sd card. and after 10 secs automatically it switches to front camera to record.
Back camera recording is working fine. But front camera preview is showing correctly, but it is not recording video, it says IllegalStateException start failed: -38 when I call mediarecorder.start();
here is the code
Prepare Media
mediaRecorder = new MediaRecorder();
mCamera.unlock(); // lock camera for later use
mediaRecorder.setCamera(mCamera); // lock camera for later use
//mediaRecorder.setCamera(mCamera); // lock camera for later use
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
//mediaRecorder.setOrientationHint(90);
mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_720P));
//mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));
//mediaRecorder.setVideoSize(320, 240);
//mediaRecorder.setVideoFrameRate(15);
//mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
//mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
long l = System.currentTimeMillis();
mediaRecorder.setOutputFile("/sdcard/" + l + ".mp4");
mediaRecorder.setMaxDuration(600000); // Set max duration 60 sec.
mediaRecorder.setMaxFileSize(50000000); // Set max file size 50M
try {
mediaRecorder.prepare();
Log.e(TAG, "prepareMediaRecorder: ");
} catch (IllegalStateException e) {
releaseMediaRecorder();
return false;
} catch (IOException e) {
releaseMediaRecorder();
return false;
}
Log.e(TAG, "prepareMediaRecorder: RETURN TRUE");
Start Record
if (!prepareMediaRecorder()) {
Toast.makeText(getContext(), "Fail in prepareMediaRecorder()!\n - Ended -", Toast.LENGTH_LONG).show();
//finish();
return;
}
try {
Log.e(TAG, "recordBackCameraVideo: START START ");
mediaRecorder.start();
Log.e(TAG, "recordBackCameraVideo: START END ");
} catch (final Exception ex) {
Log.i("---", "Exception in thread");
}
recording = true;
Stop Recording
try {
Log.e(TAG, "stopRecording: STOP START");
mediaRecorder.stop(); // stop the recording
Log.e(TAG, "stopRecording: STOP END");
} catch (RuntimeException stopException) {
Log.e(TAG, "stopRecording: " + stopException.getMessage());
}
releaseMediaRecorder(); // release the MediaRecorder object
Toast.makeText(getContext(), "Video captured!", Toast.LENGTH_LONG).show();
recording = false;
Try this one. First you need to check CamcorderProfile.hasProfile("CameraID", "Pass your desired quality") If it is true then you can use that quality for video recording otherwise you need to use some other CamcorderProfile quality.
Below code is just for the reference which I used in my project you can change the code according to your need.
private boolean prepareMediaRecorder() {
mediaRecorder = new MediaRecorder();
CamcorderProfile profile = null;
if (!cameraFront) {
profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
} else {
if (CamcorderProfile.hasProfile(0, CamcorderProfile.QUALITY_HIGH)) {
profile = CamcorderProfile.get(0, CamcorderProfile.QUALITY_HIGH);
} else {
profile = CamcorderProfile.get(0, CamcorderProfile.QUALITY_LOW);
}
}
mCamera.unlock();
if (!cameraFront) {
// Back
mediaRecorder.setOrientationHint(90);
} else {
// Front
mediaRecorder.setOrientationHint(270);
}
mediaRecorder.setCamera(mCamera);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setOutputFormat(profile.fileFormat);
mediaRecorder.setVideoEncoder(profile.videoCodec);
mediaRecorder.setVideoEncodingBitRate(profile.videoBitRate);
mediaRecorder.setVideoFrameRate(profile.videoFrameRate);
mediaRecorder.setOutputFile(Utils.getOriginalFileName());
mediaRecorder.setVideoSize(640, 480);
mediaRecorder.setMaxDuration(50000); // Set max duration 5 sec.
mediaRecorder.setMaxFileSize(50000000); // Set max file size 50M
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
releaseMediaRecorder();
return false;
} catch (IOException e) {
releaseMediaRecorder();
return false;
}
return true;
}

Android, API < 14 mediarecord cause the camera brightness to dim

i have a piece of code here,
public boolean prepareMediaRecorder(ICameraService cameraService, String path) {
mediaRecorder = new MediaRecorder();
cameraService.bindRecorder(mediaRecorder);
// Step 2: Set sources
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
// Step 4: Set output file
mediaRecorder.setOutputFile(path);
// Step 5: Set the preview output
mediaRecorder.setOrientationHint(0);
// Step 6: Prepare configured MediaRecorder
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
Log.d("ERR", "IllegalStateException preparing MediaRecorder: " + e.getMessage());
releaseMediaRecorder(cameraService);
return false;
} catch (IOException e) {
Log.d("ERR", "IOException preparing MediaRecorder: " + e.getMessage());
releaseMediaRecorder(cameraService);
return false;
}
return true;
}
when its previewing on camera the camera brightness looks normal,
but when i click record that trigger this code:
public void startRecordSimple(ICameraService cameraService, String path) {
while (!prepareMediaRecorder(cameraService, path)) {
releaseMediaRecorder(cameraService);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
mediaRecorder.start();
isRecording = true;
}
the camera brightness is become dim i have tried to change the paramters with
camera.getParameters().set("brightness",10);
and the camera still the same
what should i do to make the camera brightness become bright when record via MediaRecorder.
i am using the API 14 anyway, mostly users here using API 14 to lollipop

MediaRecorder start failed -19 and Camera error 100

I am developing an app to record video.
I got this code in my App which is running fine in Nexus 4 and Sony Ericsson mini pro, but when I test in other devices, like Archos 80G9 and Jiayu G3ST, the app gives me the following error
"MediaRecorder start failed -19"
or sometimes
"camera error 100 ".
I tried implementing some changes suggested in other stackoverflow posts but the error still appears.
private boolean prepareVideoRecorder() {
/** ADDED Sony Ericsson Stoped */
try {
mCamera.setPreviewDisplay(null);
} catch (java.io.IOException ioe) {
Log.d(TAG,
"IOException nullifying preview display: "
+ ioe.getMessage());
}
mCamera.stopPreview();
mMediaRecorder = new MediaRecorder();
// Step 1: Unlock and set camera to MediaRecorder
mCamera.unlock();
mMediaRecorder.setCamera(mCamera);
// Step 2: Set sources
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
CameraBackFront cm = new CameraBackFront();
int id = cm.getBackCameraId();
if (qualityString().equalsIgnoreCase("Low")) {
mMediaRecorder.setProfile(CamcorderProfile.get(id,
CamcorderProfile.QUALITY_LOW));
} else if (qualityString().equalsIgnoreCase("High")) {
mMediaRecorder.setProfile(CamcorderProfile.get(id,
CamcorderProfile.QUALITY_HIGH));
} else if (qualityString().equalsIgnoreCase("480p")) {
mMediaRecorder.setProfile(CamcorderProfile.get(id,
CamcorderProfile.QUALITY_480P));
} else if (qualityString().equalsIgnoreCase("720p")) {
mMediaRecorder.setProfile(CamcorderProfile.get(id,
CamcorderProfile.QUALITY_720P));
} else if (qualityString().equalsIgnoreCase("1080p")) {
try {
mMediaRecorder.setProfile(CamcorderProfile.get(id,
CamcorderProfile.QUALITY_1080P));
} catch (Exception e) {
mMediaRecorder.setProfile(CamcorderProfile.get(id,
CamcorderProfile.QUALITY_HIGH));
}
} else {
mMediaRecorder.setProfile(CamcorderProfile.get(0,
CamcorderProfile.QUALITY_HIGH));
}
// Step 4: Set output file
mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO)
.toString());
/** ADD FILE NAME */
addFileNameDB();
// Step 5: Set the preview output
mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());
// Step 6: Prepare configured MediaRecorder
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;
}
I have tried:
put a thread.sleep(1000); before mediarecorder.start() but this gives me a error.
put a Default CameraPreview in development.android.com.
my app works with a custom CameraPreview that resized the preview.
I obtain the camera qualitys with CamcorderProfile.hasProfile(CamcorderProfile.QUALITY_720P) , because this always uses profiles that work on phone.
Finally i fix my problem using
...
releaseCamera();
if(prepareVideoRecorder){
...
}
before prepareVideoRecorder().
and into prepareVideoRecorder add a new instance of camera.
public void prepareVideoRecorder(){
mCamera = getCameraInstance();
...
}
With this things i have fixed:
MediaRecorder start failed -19.
Camera Error 100 (media server died and camera died ).

Mediarecorder start failed -19

I am getting this error when running start() for mediarecorder.
06-28 18:46:22.570: E/MediaRecorder(9540): start failed: -19
06-28 18:46:22.570: W/System.err(9540): java.lang.RuntimeException: start failed.
I am extending mediarecorder class
My code:
camera = Camera.open(cameraId);
super.setCamera(camera);
super.setVideoSource(MediaRecorder.VideoSource.CAMERA);
super.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
if (mode==MODE_DEFAULT) {
super.setMaxDuration(1000);
super.setMaxFileSize(Integer.MAX_VALUE);
} else {
// On some phones a RuntimeException might be thrown :/
try {
super.setMaxDuration(0);
super.setMaxFileSize(Integer.MAX_VALUE);
} catch (RuntimeException e) {
Log.e(TAG,"setMaxDuration or setMaxFileSize failed !");
}
}
super.setVideoEncoder(videoEncoder);
if(surfaceHolder!=null)
super.setPreviewDisplay(surfaceHolder.getSurface());
//super.setVideoSize(quality.resX,quality.resY);
super.setVideoFrameRate(quality.frameRate);
super.setVideoEncodingBitRate(quality.bitRate);
I saw these pages
Error opening android camera for streaming video
Android MediaRecorder - "start failed: -19"
But non of them worked for me...
Running on archos 80 g9, android 3.2
Any one got any ideas?
Fixed by removing
super.setVideoFrameRate(quality.frameRate);
I was facing the same proble during video recording and i solved it by adding this for video recording
/**
* Start video recording by cleaning the old camera preview
*/
private void startVideoRecorder() {
// THIS IS NEEDED BECAUSE THE GLASS CURRENTLY THROWS AN ERROR OF
// "MediaRecorder start failed: -19"
// THIS WONT BE NEEDED INCASE OF PHONE AND TABLET
// This causes crash in glass kitkat version so remove it
// try {
// mCamera.setPreviewDisplay(null);
// } catch (java.io.IOException ioe) {
// Log.d(TAG,
// "IOException nullifying preview display: "
// + ioe.getMessage());
// }
// mCamera.stopPreview();
// mCamera.unlock();
recorder = new MediaRecorder();
// Let's initRecorder so we can record again
initRecorder();
}
/**
* Initialize video recorder to record video
*/
private void initRecorder() {
try {
File dir = new File(folderPath);
if (!dir.exists()) {
dir.mkdirs();
}
mCamera.stopPreview();
mCamera.unlock();
videofile = new File(dir, fileName + ".mp4");
recorder.setCamera(mCamera);
// Step 2: Set sources
recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
recorder.setProfile(CamcorderProfile
.get(CamcorderProfile.QUALITY_HIGH));
// Step 4: Set output file
recorder.setOutputFile(videofile.getAbsolutePath());
// Step 5: Set the preview output
recorder.setPreviewDisplay(mPreview.getHolder().getSurface());
// Step 6: Prepare configured MediaRecorder
recorder.setMaxDuration(video_duration * 1000);
recorder.setOnInfoListener(new OnInfoListener() {
#Override
public void onInfo(MediaRecorder mr, int what, int extra) {
if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
mCamera.stopPreview();
releaseMediaRecorder();
/*
* initiate media scan and put the new things into the
* path array to make the scanner aware of the location
* and the files you want to see
*/MediaScannerConnection.scanFile(
CuxtomCamActivity.this,
new String[] { videofile.getPath() }, null,
null);
Intent intent = new Intent();
intent.putExtra(CuxtomIntent.FILE_PATH,
videofile.getPath());
intent.putExtra(CuxtomIntent.FILE_TYPE, FILE_TYPE.VIDEO);
setResult(RESULT_OK, intent);
finish();
}
}
});
recorder.prepare();
recorder.start();
} catch (Exception e) {
Log.e("Error Stating CuXtom Camera", e.getMessage());
}
}
private void releaseMediaRecorder() {
if (recorder != null) {
recorder.reset(); // clear recorder configuration
recorder.release(); // release the recorder object
recorder = null;
}
}
To see detail of how a camera is actually implemented refer to Open Source Cuxtom Cam
I found a subtle hint in documentation for the MediaRecorder.start() method that suggest if it fails to lock() the Camera before attempting to re-record. This worked for me. Implies a Camera state bug was fixed post API level 13 - calling Camera.lock() is the known workaround.
This code worked for me (found here)
mCamera.unlock(); // maybe not for your activity flow
//1st. Initial state
mProfile = CamcorderProfile.get( CamcorderProfile.QUALITY_HIGH );
mMediaRecorder = new MediaRecorder();
mMediaRecorder.setCamera( mCamera );
//2nd. Initialized state
mMediaRecorder.setAudioSource( MediaRecorder.AudioSource.CAMCORDER );
mMediaRecorder.setVideoSource( MediaRecorder.VideoSource.CAMERA );
//3rd. config
mMediaRecorder.setOutputFormat( mProfile.fileFormat );
mMediaRecorder.setAudioEncoder( mProfile.audioCodec );
mMediaRecorder.setVideoEncoder( mProfile.videoCodec );
mMediaRecorder.setOutputFile( "/sdcard/FBVideo.3gp" );
mMediaRecorder.setVideoSize( mProfile.videoFrameWidth, mProfile.videoFrameHeight );
mMediaRecorder.setVideoFrameRate( mProfile.videoFrameRate );
mMediaRecorder.setVideoEncodingBitRate( mProfile.videoBitRate );
mMediaRecorder.setAudioEncodingBitRate( mProfile.audioBitRate );
mMediaRecorder.setAudioChannels( mProfile.audioChannels );
mMediaRecorder.setAudioSamplingRate( mProfile.audioSampleRate );
mMediaRecorder.setPreviewDisplay( mHolder.getSurface() );
try {
mMediaRecorder.prepare();
mMediaRecorder.start();
} catch ( IllegalStateException e ) {
e.printStackTrace();
} catch ( IOException e ) {
e.printStackTrace();
}
The problem here is about the camera. Just use camera.unlock() to allow the media process to access the camera.
This must be done before calling MediaRecorder.setCamera(Camera). This cannot be called after recording starts.
Read more here.

MediaRecorder video framerate

I'm recording videos with MediaRecorder, but it appears that whatever setting I use, the framerate is appalling (~ 1fps)
This is my code:
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
CamcorderProfile cp = CamcorderProfile.get(HIGH_QUALITY ? CamcorderProfile.QUALITY_HIGH : CamcorderProfile.QUALITY_LOW);
System.out.println("RECORDING AT " + cp.videoFrameRate); // Says 30fps
recorder.setProfile(cp);
recordingFilename = tempFileName();
recorder.setOutputFile(recordingFilename);
recorder.setPreviewDisplay(holder.getSurface());
try {
recorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
finish();
} catch (IOException e) {
e.printStackTrace();
finish();
}
recorder.start();
It appears that it is the ROM I am using. I didn't realise I get the same crappy frame rate using the standard Camera app when recording video.
Nevermind :)

Categories

Resources