CamcorderProfile.QUALITY_HIGH not working on specific device - android

I have the following method to start a camera preview and prepare a MediaRecorder
private boolean prepareVideoRecorder(){
// BEGIN_INCLUDE (configure_preview)
//mCamera = CameraHelper.getDefaultCameraInstance();
if(Utils.hasFrontCamera())
mCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);
else
mCamera = Camera.open();
// We need to make sure that our preview and recording video size are supported by the
// camera. Query camera to find all the sizes and choose the optimal size given the
// dimensions of our preview surface.
Camera.Parameters parameters = mCamera.getParameters();
List<Camera.Size> mSupportedPreviewSizes = parameters.getSupportedPreviewSizes();
Camera.Size optimalSize = CameraHelper.getOptimalPreviewSize(mSupportedPreviewSizes,
mSurfaceView.getWidth(), mSurfaceView.getHeight());
// Use the same size for recording profile.
CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_LOW);
profile.audioChannels = 2;
// profile.videoFrameWidth = optimalSize.width;
// profile.videoFrameHeight = optimalSize.height;
Log.e("Resolution ",profile.videoFrameWidth + " - "+profile.videoFrameHeight);
//profile.videoBitRate = 3000000;
//profile.videoFrameRate = 24;
// likewise for the camera object itself.
parameters.setPreviewSize(profile.videoFrameWidth, profile.videoFrameHeight);
mCamera.setParameters(parameters);
try {
// Requires API level 11+, For backward compatibility use {#link setPreviewDisplay}
// with {#link SurfaceView}
mCamera.setPreviewDisplay(mSurfaceView.getHolder());
if (Utils.isModeCameraLandScape(getActivity())) {
mCamera.setDisplayOrientation(ORIENTATIONS_LAND.get(Utils.getRotation(context)));
Log.e("IMAGE", "preCreateCamera land "+Utils.getRotation(context));
} else {
mCamera.setDisplayOrientation(ORIENTATIONS_PORT.get(Utils.getRotation(context)));
Log.e("IMAGE", "preCreateCamera port "+Utils.getRotation(context));
}
//mCamera.setDisplayOrientation(90);
} catch (IOException e) {
Log.e(TAG, "Surface texture is unavailable or unsuitable" + e.getMessage());
return false;
}
// END_INCLUDE (configure_preview)
// BEGIN_INCLUDE (configure_media_recorder)
mMediaRecorder = new MediaRecorder();
// Step 1: Unlock and set camera to MediaRecorder
mCamera.unlock();
mMediaRecorder.setCamera(mCamera);
// Step 2: Set sources
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC );
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
mMediaRecorder.setProfile(profile);
// Step 4: Set output file
String path = getVideoFile(MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO).getAbsolutePath();
currentFile = path;
mMediaRecorder.setOutputFile(path);
mMediaRecorder.setOrientationHint(270);
// END_INCLUDE (configure_media_recorder)
// Step 5: 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 had to comment this because apparently in a specific device i had for tests ( and i assume it happens on others ) i couldnt see the video preview and i got a MediaRecorder - “start failed: -19”
// profile.videoFrameWidth = optimalSize.width;
// profile.videoFrameHeight = optimalSize.height;
My problem
When i set the
CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_LOW);
to
CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
the camera doesnt seem to start the preview, and on the Log.e i get "E/Resolution: 1280 - 720", but from what i've seen on the specs of the device, it should only go up to 640 - 480
Do i need to set some other parameters?

Before setting the quality you have to check if the camera supports that quality.

I ran into this problem on a nexus 6p device when setting the QUALITY_HIGH on the MediaRecorder.
I found the issue to the setMaxDuration() method call caused MediaRecorder to crash with a value that was too high.

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 ).

Recording video using MediaRecorder and FileOutputStream produces video file that can't be played

I am trying to implement the function where i can start and stop the video recording multiple times, and accumulate video data into a File.
This is how i prepare my media recorder:
private boolean prepareVideoRecorder(){
mMediaRecorder = new MediaRecorder();
//0 for landscape
//90 for portrait
//Check for available profile
CamcorderProfile profile = null;
if(CamcorderProfile.hasProfile(CamcorderProfile.QUALITY_480P)){
Log.d(TAG, "480p");
profile = CamcorderProfile.get(CamcorderProfile.QUALITY_480P);
}else if(CamcorderProfile.hasProfile(CamcorderProfile.QUALITY_720P)){
Log.d(TAG, "720p");
profile = CamcorderProfile.get(CamcorderProfile.QUALITY_720P);
}else{
Log.d(TAG, "LOW");
profile = CamcorderProfile.get(CamcorderProfile.QUALITY_LOW);
}
// 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 profile
mMediaRecorder.setProfile(profile);
// Step 4: Set output file and pass media recorder the file descriptor
if(mStoreFile == null){
mStoreFile = MediaUtil.getOutputMediaFile(MediaUtil.MEDIA_TYPE_VIDEO);
try {
mStoreFile.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
}
try {
mOutputStream = new FileOutputStream(mStoreFile, true);
mMediaRecorder.setOutputFile(mOutputStream.getFD());
} catch (Exception e1) {
e1.printStackTrace();
}
mMediaRecorder.setMaxDuration(30000);
// Step 5: Set the preview output
mMediaRecorder.setPreviewDisplay(mPreviewSurface.getHolder().getSurface());
//Check orientation and set hint
switch(mOrientation){
case ORIENTATION_PORTRAIT_NORMAL:
mMediaRecorder.setOrientationHint(90);
break;
case ORIENTATION_PORTRAIT_INVERTED:
mMediaRecorder.setOrientationHint(270);
break;
case ORIENTATION_LANDSCAPE_NORMAL:
mMediaRecorder.setOrientationHint(0);
break;
case ORIENTATION_LANDSCAPE_INVERTED:
mMediaRecorder.setOrientationHint(180);
break;
}
// 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;
}
This is the button on click code:
#Override
public void onClick(View v) {
if (isRecording) {
try{
mOutputStream.close();
}catch(Exception ee){
}
// stop recording and release camera
mMediaRecorder.stop(); // stop the recording
mMediaRecorder.reset();
mCamera.lock(); // take camera access back from MediaRecorder
// inform the user that recording has stopped
mRecordButton.setImageResource(R.drawable.record_button);
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
mRecordButton.setImageResource(R.drawable.record_button_on);
isRecording = true;
} else {
// prepare didn't work, release the camera
releaseMediaRecorder();
// inform user
}
}
}
});
MediaUtil.getOutputMediaFile(MediaUtil.MEDIA_TYPE_VIDEO) will give me something like:
/storage/sdcard0/Pictures/Project/VID_2013.mp4
Current problem:
The way i test it at the moment is by:
Start recording
Stop Recording
Start recording
stop recording
Go to android's file manager on /Pictures/Project path
I can see the file that was created and "appended" with multiple segments of data. But it won't play. And it doesn't have a cover image like other video files.
Somewhere along the line, the file is corrupted? It doesn't work as well if i just record once and check the file in the storage. I am able to record video if i just define a File for the MediaRecorder in setOutputFile, but i'm trying append video data for multiple shoot. Is this possible?

Can sound be turned off in MediaRecorder of the Android API?

I would like to write a program that records video but not sound.
Can anyone help me on how to not record the sound while recording the video using MediaRecorder?
This is indeed possible. See the setAudioEncoder method in MediaRecorder:
If this method is not called, the output file will not contain an audio track.
So if you only want video, simply do not invoke this method.
You should just prepare your mediarecorder's video requirements, like:
private boolean prepareMediaRecorder(){
myCamera = getCameraInstance();
mediaRecorder = new MediaRecorder();
// store the quality profile required
CamcorderProfile profile = CamcorderProfile.get(mCameraId, CamcorderProfile.QUALITY_HIGH);
myCamera.unlock();
mediaRecorder.setCamera(myCamera);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setOutputFormat(profile.fileFormat);
mediaRecorder.setVideoEncoder(profile.videoCodec);
mediaRecorder.setVideoEncodingBitRate(profile.videoBitRate);
mediaRecorder.setVideoFrameRate(profile.videoFrameRate);
mediaRecorder.setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight);
mediaRecorder.setOutputFile("/sdcard/myvideo.mp4");
mediaRecorder.setMaxDuration(60000 * 20); // Set max duration 60 *20 sec.
mediaRecorder.setMaxFileSize(5000000 * 4); // Set max file size 5M * 4
mediaRecorder.setPreviewDisplay(myCameraSurfaceView.getHolder().getSurface());
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
releaseMediaRecorder();
return false;
} catch (IOException e) {
releaseMediaRecorder();
return false;
}
return true;
}

Categories

Resources