Freezes app when called MediaRecorder.stop() - android

I have activity with SurfaceView, i want start and stop video record in this Activity, but i want continue record video if i go to other Activity. Now i start record video(in activity 1) and go to other Activity, when i back (to activity 1) and want stop record video, my app is freezes when called recorder.stop(); How i can fix it?
Init SurfaceView and SurfaceHolder:
SurfaceView cameraView = (SurfaceView) findViewById(R.id.surfaceView);
SurfaceHolder holder = cameraView.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
Start record video:
protected void startRecordVideo() {
if (cam == null) {
cam = Camera.open();
try {
cam.setPreviewDisplay(holder);
} catch (IOException e) {
e.printStackTrace();
}
}
recording = true;
recorder = new MediaRecorder();
recorder.setCamera(cam);
initRecorder();
prepareRecorder();
try {
cam.unlock();
} catch (RuntimeException e) {
e.printStackTrace();
}
recorder.start();
}
Stop record video (this is app freezes):
protected void stopRecordVideo() { //cam - Camera, recorder - MediaRecorder
mc.IS_RECORD = false;
recording = false;
try {
cam.lock();
recorder.stop(); //app freezes in this line (i used Log)
recorder = null;
cam.reconnect();
cam.release();
cam = null;
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (RuntimeException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
P.S. i do not use camera.startPreview(); and camera.stopPreview();

Try to wrap stopRecordVideo inside a Thread, so it doesnt block the main (UI) Thread.
If it doesnt work, take a look at this question :
Android Media Recording using threads
It seems you must do a little "hack"

Related

IllegalStateException in MediaRecorder start method call

My app tries to record the sound by using the MediaRecorder API. My start recording method looks like this:
public void start() {
if(this.mRecorder == null) {
try{
this.mRecorder = new MediaRecorder();
this.mRecorder.setAudioSource(1);
this.mRecorder.setOutputFormat(1);
this.mRecorder.setAudioEncoder(1);
this.mRecorder.setOutputFile("/dev/null");
} catch (RuntimeException re) {
}
try {
this.mRecorder.prepare();
} catch (IllegalStateException var3) {
var3.printStackTrace();
} catch (IOException var4) {
var4.printStackTrace();
}
this.mRecorder.start();
this.mEMA = 0.0D;
}
}
But I get crash reports from users on the following line:
this.mRecorder.start();
The IllegalStateException stack trace is:
java.lang.RuntimeException:
atandroid.app.ActivityThread.performResumeActivity(ActivityThread.java:3493)
atandroid.app.ActivityThread.handleResumeActivity(ActivityThread.java:3533)
atandroid.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2791)
atandroid.app.ActivityThread.-wrap12(ActivityThread.java)
atandroid.app.ActivityThread$H.handleMessage(ActivityThread.java:1532)
atandroid.os.Handler.dispatchMessage(Handler.java:102)
atandroid.os.Looper.loop(Looper.java:163)
atandroid.app.ActivityThread.main(ActivityThread.java:6342)
atjava.lang.reflect.Method.invoke(NativeMethod)
atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
atcom.android.internal.os.ZygoteInit.mai(ZygoteInit.java:770)
Causedby:java.lang.IllegalStateException:
atandroid.media.MediaRecorder.start(NativeMethod)
atmeasure.sound.decibels.SoundMeter.start(SoundMeter.java:43)
atmeasure.sound.decibels.MyActivity.start(MyActivity.java:138)
atmeasure.sound.decibels.MyActivity.onResume(MyActivity.java:521)
atandroid.app.Instrumentation.callActivityOnResume(Instrumentation.java:1270)
atandroid.app.Activity.performResume(Activity.java:6960)
atandroid.app.ActivityThread.performResumeActivity(ActivityThread.java:3470)
The app is crashing for users. Any help!

Recorded video with front camera is showing blurry in android tablet

I am recording a video in android using media recorder. The video is recorded nicely in phone for both front and back camera but for tablet the video is recorded nicely for back camera but for front camera it is showing blurry. This is how my recorded video looks:
my code for video recording is
private Camera getCameraInstance() {
// TODO Auto-generated method stub
Camera c = null;
try {
if (Camera.getNumberOfCameras() >= 2) {
//if you want to open front facing camera use this line
c = Camera.open(CameraInfo.CAMERA_FACING_FRONT);}
// c.setDisplayOrientation(90);
//c = Camera.open(1); // attempt to get a Camera instance
} catch (Exception e) {
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
private boolean prepareMediaRecorder() {
myCamera = getCameraInstance();
mediaRecorder = new MediaRecorder();
myCamera.unlock();
mediaRecorder.setCamera(myCamera);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setProfile(CamcorderProfile
.get(CamcorderProfile.QUALITY_HIGH));
mediaRecorder.setOutputFile("/sdcard/myvideo.mp4");
//mediaRecorder.setMaxDuration(60000); // Set max duration 60 sec.
//mediaRecorder.setMaxFileSize(5000000); // Set max file size 5M
mediaRecorder.setPreviewDisplay(myCameraSurfaceView.getHolder()
.getSurface());
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
releaseMediaRecorder();
return false;
} catch (IOException e) {
releaseMediaRecorder();
return false;
}
return true;
}

Video recorder app in Android

I'm new to Android and Java so forgive me if the answer is obvious, but I can't get my video recorder app to work. I can see the image just fine in the camera preview (that code works fine) but when I go to record, I only record sound. I've implemented the code fragments recommended by Google (below) but it's not working. I've tried everything I can think of. Any insight would be appreciated.
protected void onCreate(Bundle savedInstanceState) {
int x;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_camera);
// if (savedInstanceState == null) {
// getSupportFragmentManager().beginTransaction()
// .add(R.id.container, new PlaceholderFragment()).commit();
// Create an instance of Camera
x = Camera.getNumberOfCameras();
mCamera = getCameraInstance(x-1);
recorder = new MediaRecorder();
// Create our Preview view and set it as the content of our activity.
mPreview = new VideoCameraPreview(this, x-1, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.video_camera_preview);
preview.addView(mPreview);
try {
mCamera.setPreviewDisplay(VideoCameraPreview.mHolder);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Add a listener to the Capture button
final Button captureButton = (Button) findViewById(R.id.button_capture);
captureButton.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isRecording) {
// stop recording and release camera
recorder.stop(); // stop the recording
// 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
recorder.start();
// inform the user that recording has started
captureButton.setText("Stop");
isRecording = true;
} else {
// prepare didn't work, release the camera
releaseMediaRecorder();
// inform user
}
}
}
}
);
}
private boolean prepareVideoRecorder (){
//Record a video in response to a user pressing the button
int x = Camera.getNumberOfCameras();
mCamera.unlock(); //Unlock the camera for use by the Media Recorder
recorder.setCamera(mCamera); // Get the camera ready for recording
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
recorder.setProfile(CamcorderProfile.get(x-1, CamcorderProfile.QUALITY_HIGH));
recorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());
recorder.setPreviewDisplay(VideoCameraPreview.mHolder.getSurface());
try {
recorder.prepare();
} catch (IllegalStateException e) {
Log.d("Wink", "Illegal stateException preparing MediaRecorder: " + e.getMessage());
recorder.release();
return false;
} catch (IOException e) {
Log.d("Wink", "IOException preparing MediaRecorder: " + e.getMessage());
recorder.release();
return false;
}
return true;
}
I haven't gone over that in much depth but here's a link to another video recorder for Android that might help you.
Display timer while video recording android
Change setVideoSource and setAudioSource and try to set the output format for the video and set both encoders.
recorder.setCamera(mCamera); // Get the camera ready for recording
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
recorder.setProfile(CamcorderProfile.get(x-1, CamcorderProfile.QUALITY_HIGH));
recorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());
recorder.setPreviewDisplay(VideoCameraPreview.mHolder.getSurface());

how can I have a high quality video recorder and audio recorder on android?

Hello stackoverflow friends. I am new by android and have bellow code for capturing video.
But this has low quality on phone( specially on android version 4.2 has very very low quality).
I need a video capture with higher quality but i dont know other attribute for this. how can I have a high quality video recorder ?
protected void startRecording() throws IOException
{
mCamera.stopPreview();
mCamera.unlock();
mrec = new MediaRecorder();
mrec.setCamera(mCamera);
mrec.setAudioSource(MediaRecorder.AudioSource.MIC);
mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mrec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mrec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mrec.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
mrec.setOutputFile(Videopath);
mrec.setPreviewDisplay(surfaceHolder.getSurface());
mrec.prepare();
isRecording=true;
mrec.start();
}
//-------------------------------------------------------------
protected void stopRecording()
{
releaseOnExit();
mCamera = Camera.open();
mCamera.lock();
surfaceView = (SurfaceView) findViewById(R.id.surface_camera);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
Parameters params = mCamera.getParameters();
mCamera.setParameters(params);
mCamera.setDisplayOrientation(90);
try {
mCamera .setPreviewDisplay(surfaceHolder);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
mCamera .startPreview();
btnlistToggle.setEnabled(true);
}
//-------------------------------------------------------------
#Override
public void surfaceCreated(SurfaceHolder holder)
{
if (mCamera != null)
{
Parameters params = mCamera.getParameters();
mCamera.setParameters(params);
mCamera.setDisplayOrientation(90);
try {
mCamera .setPreviewDisplay(holder);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
mCamera .startPreview();
}
else
{
Toast.makeText(getApplicationContext(), "Camera not available!", Toast.LENGTH_LONG).show();
VideoRecorderActivity.this.finish();
}
}
Also I have a voice recorder that it has a very low quality too. what I do for upper quality voide recorder ,too?
public void Record() throws IOException
{
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(this.path);
try
{
recorder.prepare();
}
catch (IllegalStateException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
recorder.start();
}
catch (Exception e)
{
}
}
It is well discussed on stackoverflow
Please refer this link to learn about profiles
And this link can also be helpful. I hope it helps and happy coding! Welcome to stackoverflow community!

Record audio of one minute

I am working with audio recording in Android.
I want to record audio maximum of 1 minute and if user ask to stop before one minute it should be stop.
I have record audio code and it works perfectly.
How can I set time duration it?
If solution is thread.sleep then it's ok. I do something like same:
if (start) {
startRecording();
try {
Thread.sleep(60000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
//What should do ? to stop thread this Thread.sleep(60000);
callToStopRecording();
}
private void startRecording() {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(path + mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
} catch (IOException e) {
Log.e("Camera Error", "prepare() failed");
}
mRecorder.start();
}
private void stopRecording() {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
but at stop recoding button, what do I need to write?
You can use this solution:
mRecorder.setMaxDuration(max_duration_ms);
The duation is in ms, so you have to use:
mRecorder.setMaxDuration(6000);

Categories

Resources