Record video with front facing camea - android

I getting error when I open camera in front facing mode and start recording. The app crashes.
My code is the following:
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity implements SurfaceHolder.Callback{
Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;
MediaRecorder m_recorder;
int MAX_TIME=5;
String m_path;
String stringPath = "/sdcard/samplevideo.3gp";
MediaRecorder videoRecorder;
Surface surface;
MediaRecorder mrec;
/** Called when the activity is first created.**/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Button buttonStartCameraPreview = (Button)findViewById(R.id.startcamerapreview);
Button buttonStopCameraPreview = (Button)findViewById(R.id.stopcamerapreview);
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.surfaceview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
buttonStartCameraPreview.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v)
{
if(!previewing){
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
for (int camNo = 0; camNo < Camera.getNumberOfCameras(); camNo++) {
CameraInfo camInfo = new CameraInfo();
Camera.getCameraInfo(camNo, camInfo);
//Toast.makeText(MainActivity.this, camNo, Toast.LENGTH_LONG).show();
if (camInfo.facing==(Camera.CameraInfo.CAMERA_FACING_FRONT)) {
camera = Camera.open(1);
}
}
try{
camera.setDisplayOrientation(90);
camera.setPreviewDisplay(surfaceHolder);
}catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (camera != null){
try
{
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewing = true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("before MediaRecorder");
mrec = new MediaRecorder(); // Works well
System.out.println("after MediaRecorder");
camera.unlock();
System.out.println("after Unlock");
mrec.setCamera(camera);
System.out.println("setcamera");
mrec.setPreviewDisplay(surfaceHolder.getSurface());
System.out.println("setcamera1");
mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
System.out.println("setcamera2");
mrec.setAudioSource(MediaRecorder.AudioSource.MIC);
mrec.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
mrec.setPreviewDisplay(surfaceHolder.getSurface());
mrec.setOutputFile("/sdcard/recordvideooutput.3gpp");
try {
System.out.println("before prepare");
mrec.prepare();
System.out.println("after prepare");
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//
}
}});
buttonStopCameraPreview.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
try {
camera.reconnect();
camera.unlock();
} catch (Exception e) {
// TODO: handle exception
}
mrec.start();
// TODO Auto-generated method stub
/*if(camera != null && previewing){
camera.stopPreview();
camera.release();
camera = null;
previewing = false;
}*/
}});
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}

Related

How to showing current time on Recorded video? (android)

recently when I record button click, start camera preview and digital clock view record
but, when I showing my record video.
not showing digitalclock.
only GLSurfaceView camera preview display.
perhaps, If you've ever added a clock on recorded video,
please advice for me
thanks.
Try this answer ,Thanks to him
import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
public class VideoRD extends Activity implements OnClickListener,
SurfaceHolder.Callback {
MediaRecorder recorder;
SurfaceHolder holder;
boolean recording = false;
public static final String TAG = "VIDEOCAPTURE";
String str_getValue ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Intent i1 = getIntent();
str_getValue = i1.getStringExtra("videoImagename");
recorder = new MediaRecorder();
initRecorder();
setContentView(R.layout.surface);
SurfaceView cameraView = (SurfaceView) findViewById(R.id.CameraView);
holder = cameraView.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
cameraView.setClickable(true);
cameraView.setOnClickListener(this);
final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
if (recording) {
try {
recorder.stop();
recorder.release();
recording = false;
Log.v(TAG, "Recording Stopped");
initRecorder();
prepareRecorder();
} catch (Exception e) {
// TODO: handle exception
}
} else {
try {
recording = true;
recorder.start();
button.setText("stop");
} catch (Exception e) {
// TODO: handle exception
}
}
}
});
}
private void initRecorder() {
try {
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
// recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
CamcorderProfile cpHigh = CamcorderProfile
.get(CamcorderProfile.QUALITY_HIGH);
recorder.setProfile(cpHigh);
recorder.setOutputFile("/sdcard/audiometer/video/"+str_getValue+"");
recorder.setMaxDuration(1200000000); // 50 seconds
recorder.setMaxFileSize(22000000); // Approximately 5 megabytes
} catch (Exception e) {
// TODO: handle exception
}
}
private void prepareRecorder() {
try {
recorder.setPreviewDisplay(holder.getSurface());
try {
recorder.prepare();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (Exception e) {
// TODO: handle exception
}
}
public void onClick(View v) {
/*
* if (recording) { recorder.stop(); recorder.release(); recording =
* false; Log.v(TAG, "Recording Stopped"); initRecorder();
* prepareRecorder(); } else { recording = true; recorder.start(); }
*/
}
public void surfaceCreated(SurfaceHolder holder) {
prepareRecorder();
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
public void surfaceDestroyed(SurfaceHolder holder) {
finish();
}
}
dont Forget to add Run time permission for write external storage

Android MediaRecorder preview size changed after start capturing video

i'm developing an camera app which allows users to capture video, and i started following some tutorials.
But the problem is when i press the Capture video button to start recording, the preview is just stretched
Before
After press record button
This is my code:
package com.example.phuongnguyen.cameraz;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Rect;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class MainActivity extends Activity implements SurfaceHolder.Callback {
Camera mCamera;
Button btnCapture;
Button btnSwitch;
Button btnRecord;
SurfaceHolder surfaceHolder;
int cameraID;
private PreviewSurfaceView mOpenCvCameraView;
private DrawingView drawingView;
static MediaRecorder mediaRecorder;
boolean recording;
String RECORD = "Record \nvideo";
String STOP_RECORD = "Stop";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createImageGallary();
btnCapture = (Button) findViewById(R.id.captureImage);
btnSwitch = (Button) findViewById(R.id.button_switch);
btnRecord = (Button) findViewById(R.id.button_record);
mOpenCvCameraView = (PreviewSurfaceView) findViewById(R.id.surfaceView);
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
mOpenCvCameraView.setListener(this);
drawingView = (DrawingView) findViewById(R.id.drawing_view);
mOpenCvCameraView.setDrawingView(drawingView);
surfaceHolder = mOpenCvCameraView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
btnCapture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
captureImage();
}
});
btnSwitch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switchCamera();
}
});
mediaRecorder = new MediaRecorder();
recording = false;
//initMediaRecorder();
btnRecord.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(recording){
mediaRecorder.stop();
releaseMediaRecorder();
btnRecord.setText(RECORD);
recording = false;
//finish();
}else{
//refreshCamera();
//MediaRecorder test = mediaRecorder;
try {
initMediaRecorder();
mediaRecorder.start();
} catch (Exception e) {
e.printStackTrace();
}
recording = true;
btnRecord.setText(STOP_RECORD);
}
}
});
}
private void initMediaRecorder(){
//mediaRecorder.setVideoSize(mOpenCvCameraView.getWidth(), mOpenCvCameraView.getHeight());
mCamera.unlock();
mediaRecorder.setCamera(mCamera);
//mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
CamcorderProfile camcorderProfile_HQ = CamcorderProfile.get(cameraID, CamcorderProfile.QUALITY_HIGH);
mediaRecorder.setProfile(camcorderProfile_HQ);
//mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
//mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setOutputFile("/sdcard/myvideo.mp4");
mediaRecorder.setMaxDuration(5000); // Set max duration 5 sec.
mediaRecorder.setMaxFileSize(5000000); // Set max file size 5M
if(cameraID == Camera.CameraInfo.CAMERA_FACING_BACK){
mediaRecorder.setOrientationHint(90);
}else{
mediaRecorder.setOrientationHint(270);
}
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void prepareMediaRecorder(){
mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
releaseMediaRecorder();
//e.printStackTrace();
} catch (IOException e) {
releaseMediaRecorder();
//e.printStackTrace();
}
}
private void releaseMediaRecorder() {
if (mediaRecorder != null) {
mediaRecorder.reset(); // clear recorder configuration
mediaRecorder.release(); // release the recorder object
mediaRecorder = null;
//mCamera.lock(); // lock camera for later use
}
}
private String GALLARY_LOCATION_NAME = "Camera Z";
File galleryFolder;
private void createImageGallary(){
File storageDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
galleryFolder = new File(storageDirectory,GALLARY_LOCATION_NAME);
if (!galleryFolder.exists()){
galleryFolder.mkdirs();
}
}
private String imagePath(File imageFile){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyymmddhhmmss");
String date = simpleDateFormat.format(new Date());
String photo = "CameraZ_"+date+".jpg";
String file_name = imageFile.getAbsolutePath()+"/"+photo;
return file_name;
}
Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
new BitmapAsyncTask(getApplicationContext(), imagePath(galleryFolder), cameraID, data).execute();
//Toast.makeText(getApplicationContext(), "Picture saved : "+imagePath(gallaryFolder), Toast.LENGTH_SHORT).show();
refreshCamera();
//refreshGallery(photoFile);
}
};
AutoFocusCallback autoFocusCallback = new AutoFocusCallback() {
#Override
public void onAutoFocus(boolean success, Camera camera) {
btnCapture.setEnabled(true);
}
};
#Override
protected void onResume() {
super.onResume();
refreshCamera();
}
#Override
protected void onPause() {
super.onPause();
if(mOpenCvCameraView != null){
releaseCamera();
}
}
#Override
protected void onDestroy() {
super.onDestroy();
if(mOpenCvCameraView != null){
}
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);
cameraID = Camera.CameraInfo.CAMERA_FACING_BACK;
}catch (RuntimeException e){
e.printStackTrace();
}
mediaRecorder.setPreviewDisplay(holder.getSurface());
Camera.Parameters parameter = mCamera.getParameters();
parameter.setPreviewFrameRate(20);
parameter.setPreviewSize(mOpenCvCameraView.getWidth(), mOpenCvCameraView.getHeight());
mCamera.setParameters(parameter);
mCamera.setDisplayOrientation(90);
try{
mCamera.setPreviewDisplay(surfaceHolder);
mCamera.startPreview();
}catch (Exception e){
}
//prepareMediaRecorder();
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
refreshCamera();
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
releaseCamera();
}
public void switchCamera(){
mCamera.stopPreview();
mCamera.release();
if(cameraID == Camera.CameraInfo.CAMERA_FACING_BACK){
cameraID = Camera.CameraInfo.CAMERA_FACING_FRONT;
}else{
cameraID = Camera.CameraInfo.CAMERA_FACING_BACK;
}
try {
mCamera = Camera.open(cameraID);
}catch (RuntimeException e){
}
Camera.Parameters parameter = mCamera.getParameters();
parameter.setPreviewFrameRate(20);
parameter.setPreviewSize(mOpenCvCameraView.getWidth(), mOpenCvCameraView.getHeight());
mCamera.setParameters(parameter);
mCamera.setDisplayOrientation(90);
try{
mCamera.setPreviewDisplay(surfaceHolder);
mCamera.startPreview();
}catch (Exception e){
}
}
public void captureImage(){
mCamera.takePicture(null, null, pictureCallback);
}
public void refreshGallery( File file){
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
sendBroadcast(intent);
}
public void refreshCamera(){
if(surfaceHolder.getSurface()== null){
return;
}
try{
mCamera.stopPreview();
}catch (Exception e){
}
try{
mCamera.setPreviewDisplay(surfaceHolder);
mCamera.startPreview();
}catch (Exception e){
}
}
public void releaseCamera() {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.setPreviewCallback(null);
mCamera.release();
mCamera = null;
}
}
public void doTouchFocus(final Rect tfocusRect) {
try {
final List<Camera.Area> focusList = new ArrayList<Camera.Area>();
Camera.Area focusArea = new Camera.Area(tfocusRect, 1000);
focusList.add(focusArea);
Camera.Parameters para = mCamera.getParameters();
para.setFocusAreas(focusList);
para.setMeteringAreas(focusList);
mCamera.setParameters(para);
mCamera.autoFocus(autoFocusCallback);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Cant setCamera to front for recording

This is my code:
package eye.sight.test;
import java.io.IOException;
import android.app.Activity;
import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
public class AndroidVideoCapture extends Activity implements SurfaceHolder.Callback{
Button myButton;
MediaRecorder mediaRecorder;
SurfaceHolder surfaceHolder;
boolean recording;
Camera c;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
recording = false;
setContentView(R.layout.main1);
initMediaRecorder();
SurfaceView myVideoView = (SurfaceView)findViewById(R.id.videoview);
surfaceHolder = myVideoView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
myButton = (Button)findViewById(R.id.mybutton);
myButton.setOnClickListener(myButtonOnClickListener);
}
private Button.OnClickListener myButtonOnClickListener
= new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(recording){
mediaRecorder.stop();
mediaRecorder.release();
finish();
}else{
mediaRecorder.start();
recording = true;
myButton.setText("STOP");
}
}};
#Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void surfaceCreated(SurfaceHolder arg0) {
// TODO Auto-generated method stub
prepareMediaRecorder();
}
#Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}
private void initMediaRecorder(){
mediaRecorder = new MediaRecorder();
// c.unlock();
// c = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);
// mediaRecorder.setCamera(c);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
CamcorderProfile camcorderProfile_HQ = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
mediaRecorder.setProfile(camcorderProfile_HQ);
mediaRecorder.setOutputFile("/sdcard/myvideo.mp4");
mediaRecorder.setMaxDuration(60000);
//mediaRecorder.setMaxFileSize(5000000);
}
private void prepareMediaRecorder(){
mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I've been googling around for quite a bit on how to use the front camera but no luck there whatsoever. What am I missing here? It works just fine on default but I'm unsure on when and how to use Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT); and Came.unlock();.
Thanks
Try doing this
private Camera FrontCamera()
{
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;
}
Also add following in manifest file
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-feature android:name="android.hardware.camera.front" android:required="false" />

Record video and over the video show time and date over the video

Hi does anybody knows how to record video in android and for example above the recording screen at the bottom show current time and date
Here is code that record video in surface view and store in sdcard and for date and time by This
package com.po;
import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
public class VideoRD extends Activity implements OnClickListener,
SurfaceHolder.Callback {
MediaRecorder recorder;
SurfaceHolder holder;
boolean recording = false;
public static final String TAG = "VIDEOCAPTURE";
String str_getValue ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Intent i1 = getIntent();
str_getValue = i1.getStringExtra("videoImagename");
recorder = new MediaRecorder();
initRecorder();
setContentView(R.layout.surface);
SurfaceView cameraView = (SurfaceView) findViewById(R.id.CameraView);
holder = cameraView.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
cameraView.setClickable(true);
cameraView.setOnClickListener(this);
final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
if (recording) {
try {
recorder.stop();
recorder.release();
recording = false;
Log.v(TAG, "Recording Stopped");
initRecorder();
prepareRecorder();
} catch (Exception e) {
// TODO: handle exception
}
} else {
try {
recording = true;
recorder.start();
button.setText("stop");
} catch (Exception e) {
// TODO: handle exception
}
}
}
});
}
private void initRecorder() {
try {
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
// recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
CamcorderProfile cpHigh = CamcorderProfile
.get(CamcorderProfile.QUALITY_HIGH);
recorder.setProfile(cpHigh);
recorder.setOutputFile("/sdcard/audiometer/video/"+str_getValue+"");
recorder.setMaxDuration(1200000000); // 50 seconds
recorder.setMaxFileSize(22000000); // Approximately 5 megabytes
} catch (Exception e) {
// TODO: handle exception
}
}
private void prepareRecorder() {
try {
recorder.setPreviewDisplay(holder.getSurface());
try {
recorder.prepare();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (Exception e) {
// TODO: handle exception
}
}
public void onClick(View v) {
/*
* if (recording) { recorder.stop(); recorder.release(); recording =
* false; Log.v(TAG, "Recording Stopped"); initRecorder();
* prepareRecorder(); } else { recording = true; recorder.start(); }
*/
}
public void surfaceCreated(SurfaceHolder holder) {
prepareRecorder();
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
public void surfaceDestroyed(SurfaceHolder holder) {
finish();
}
}

Record Video from Camera via Version Android 2.2

enter code hereWhen I try to recording video from camera at version Android 2.2. It has some errors.No one could find the solution. İs there any bug Android MediaRecorder API. How can I solve this.
I got more errors.You can see some of them in picture.
And an error like that:Camera Preview -13
Thanks a lot.
http://i.stack.imgur.com/72lp7.png
recorder.prepare() fails and throws Java.lang.illegalexeption
Here is Code:
package app.raceway.com;
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;
public class VideoCapture extends Activity implements SurfaceHolder.Callback {
MediaRecorder recorder;
SurfaceHolder holder;
public Camera camera;
File video;
String filePath;
boolean recording = false;
private static final int FRAME_RATE = 15;
private static final int CIF_WIDTH = 320;
private static final int CIF_HEIGHT = 240;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
recorder = new MediaRecorder();
setContentView(R.layout.main);
SurfaceView cameraView = (SurfaceView) findViewById(R.id.cameraView);
holder = cameraView.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
String path=Environment.getExternalStorageDirectory().getAbsolutePath()+
"/video/videocapture_example.mpg4";
// make sure the directory we plan to store the recording in exists
File sampleDir = Environment.getExternalStorageDirectory();
try {
video = new File(sampleDir+"/videofile.3gp");
sampleDir.createNewFile();
//video = File.createTempFile("videofile", ".3gp", sampleDir);
}
catch (IOException e)
{
Log.e("deneme","sdcard access error");
}
filePath=video.getAbsolutePath();
}
private void initRecorder() {
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
}
private void prepareRecorder() throws IOException{
recorder.setCamera(camera);
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
recorder.setOutputFile(filePath);
recorder.setMaxDuration(50000); // 50 seconds
recorder.setMaxFileSize(5000000); // Approximately 5 megabytes
try {
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void onclickSaveVideo(View v) throws IOException {
if (recording) {
Toast t=new Toast(getApplicationContext());
t.makeText(getApplicationContext(), "Video Recording stopped",Toast.LENGTH_SHORT);
t.show();
recorder.stop();
recording = false;
// Let's initRecorder so we can record again
initRecorder();
} else {
try {
prepareRecorder();
//recorder.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
recording = true;
recorder.prepare();
recorder.start();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
recorder.stop();
Toast t=new Toast(getApplicationContext());
t.makeText(getApplicationContext(), "Video Recording started",Toast.LENGTH_SHORT);
t.show();
}
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
camera=Camera.open();
try {
camera.setPreviewDisplay(holder);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
camera.startPreview();
camera.unlock();
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (recording) {
recorder.stop();
recording = false;
}
recorder.release();
finish();
camera.release();
}
}
Emin,
Based on the image of the logcat output you provide the crash is occuring with the start() method. You will see from the documentation for the start method that prepare() must be called first or else it will throw the IllegalStateException. In your code all the calls the prepare() are commented out?
EDIT: We sorted things out in the comments below my answer. He was running this code on an emulator and MediaRecorder is not supported by the emulator.

Categories

Resources