How to showing current time on Recorded video? (android) - 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

Related

preview display in android media recorder

In android how to show preview surface before media recorder start.
my app have video recording functionality,when i navigate to video recording fragment it display black screen,when i start recording using start button camera preview is display and recording start.
how to start preview before recording.
i have added code that i used in onCreateView() of fragment-
surfaceHolder = mySurfaceView.getHolder();
camera = Camera.open();
if(camera!=null){
camera.setDisplayOrientation(90);
Camera.Parameters param;
param = camera.getParameters();
param.setPreviewFrameRate(20);
param.setPreviewSize(176, 144);
camera.setParameters(param);
camera.setPreviewDisplay(surfaceHolder);
}
mediaRecorder = new MediaRecorder();
camera.unlock();
mediaRecorder.setCamera(camera);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
CamcorderProfile camcorderProfile_HQ = CamcorderProfile
.get(CamcorderProfile.QUALITY_HIGH);
mediaRecorder.setProfile(camcorderProfile_HQ);
String filePath = getOutputMediaFile(MEDIA_TYPE_VIDEO).getPath();
fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
mediaRecorder.setOutputFile(filePath);
and the code that i used on start button click-
mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
mediaRecorder.prepare();
mediaRecorder.start();
so i am able to recode video by using above code,but not able to show preview before recording start.
Please help me where i am missing.for that black screen is displayed before recoding video.
Thanks in advance.
public void surfaceCreated(SurfaceHolder holder) {
if (mCamera != null) {
Parameters params = mCamera.getParameters();
mCamera.setParameters(params);
try {
//mCamera.setDisplayOrientation(90);
mCamera.setPreviewDisplay(holder);
} catch (IOException e) {
e.printStackTrace();
}
mCamera.startPreview();
}
}
If you implement SurfaceHolder.Callback, override the surfaceCreated method like this. This worked for me.
I ran into the same problem. I looked it up and here's my Activity. It took me a bit of effort to make it not crash, so here's the final result. It displays the preview before the user clicks the REC button. (I am also displaying a countdown, but don't mind that). Notice also that there is too much work done on the main thread in this example (when clicking). There are a few things here that aren't best practices, but for a working example I think it's good enough.
import android.app.Activity;
import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.File;
import java.io.IOException;
public class MainActivity extends Activity implements SurfaceHolder.Callback {
private static final String LOG_TAG = MainActivity.class.getCanonicalName();
Button myButton;
MediaRecorder mediaRecorder;
SurfaceHolder surfaceHolder;
boolean recording;
private TextView mTimer;
private Camera mCamera;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recording = false;
mediaRecorder = new MediaRecorder();
mCamera = Camera.open();
initMediaRecorder();
SurfaceView myVideoView = (SurfaceView) findViewById(R.id.videoview);
surfaceHolder = myVideoView.getHolder();
surfaceHolder.addCallback(this);
myButton = (Button) findViewById(R.id.mybutton);
myButton.setOnClickListener(myButtonOnClickListener);
mTimer = (TextView) findViewById(R.id.timer);
}
private Button.OnClickListener myButtonOnClickListener
= new Button.OnClickListener() {
#Override
public void onClick(final View arg0) {
CountDownTimer countDownTimer = new CountDownTimer(90 * 1000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
mTimer.setText(String.valueOf(millisUntilFinished / 1000));
}
#Override
public void onFinish() {
onClick(arg0);
}
};
if (recording) {
// Stop recording and finish
try {
mediaRecorder.stop();
mediaRecorder.reset();
mediaRecorder.release();
finish();
} catch (Exception e) {
Log.e(LOG_TAG, "Failed to stop recorder", e);
}
} else {
// Release the camera and start recording
mCamera.release();
countDownTimer.start();
mediaRecorder.start();
recording = true;
myButton.setText("STOP");
}
}
};
#Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
if (mCamera != null) {
Camera.Parameters params = mCamera.getParameters();
mCamera.setParameters(params);
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException e) {
e.printStackTrace();
}
mCamera.startPreview();
}
prepareMediaRecorder();
}
#Override
public void surfaceDestroyed(SurfaceHolder arg0) {
}
private void initMediaRecorder() {
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
CamcorderProfile camcorderProfile_HQ = CamcorderProfile.get(CamcorderProfile.QUALITY_CIF);
mediaRecorder.setProfile(camcorderProfile_HQ);
File file = new File(getExternalFilesDir(Environment.DIRECTORY_MOVIES), "myvideo.mp4");
if (file.exists()) {
file.delete();
}
mediaRecorder.setOutputFile(file.getAbsolutePath());
mediaRecorder.setMaxDuration(90000); // Set max duration 90 sec.
mediaRecorder.setMaxFileSize(15000000); // Set max file size 15M
}
private void prepareMediaRecorder() {
mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
try {
mediaRecorder.prepare();
} catch (IllegalStateException | IOException e) {
Log.e(LOG_TAG, "Failed to prepare recorder", e);
}
}
}

how to record from front camera

I have to record the video from the front camera only, I Googled a lot, but have not been able to find a solution (simple)
if i set the cameratype to 1, the app crashes ..
here is my code
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
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.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;
public class VideoCapture extends Activity implements OnClickListener, SurfaceHolder.Callback {
MediaRecorder recorder;
SurfaceHolder holder;
boolean recording = false;
String pathVideo;
private int cameraType = 1;
#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); */
pathVideo = "/reg_" + System.currentTimeMillis() + ".mp4";
recorder = new MediaRecorder();
initRecorder();
setContentView(R.layout.camera);
SurfaceView cameraView = (SurfaceView) findViewById(R.id.surface_camera);
holder = cameraView.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
Button rec =(Button) findViewById(R.id.buttonstart);
rec.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
if (recording) {
recorder.stop();
recording = false;
recorder.release();
// Let's initRecorder so we can record again
initRecorder();
prepareRecorder();
} else {
recording = true;
recorder.start();
}
}
});
}
private void initRecorder() {
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
CamcorderProfile cpHigh = CamcorderProfile
.get(CamcorderProfile.QUALITY_HIGH);
recorder.setProfile(cpHigh);
File Directory = new File("/sdcard/CantaTu/");
// have the object build the directory structure, if needed.
Directory.mkdirs();
File mediaFile = new File(Directory,pathVideo);
if(mediaFile.exists()){
mediaFile.delete();
}
recorder.setOutputFile(mediaFile.getAbsolutePath());
recorder.setMaxDuration(400000); // 50 seconds
recorder.setMaxFileSize(50000000); // Approximately 5 megabytes
}
private void prepareRecorder() {
recorder.setPreviewDisplay(holder.getSurface());
try {
recorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
finish();
} catch (IOException e) {
e.printStackTrace();
finish();
}
}
public void surfaceCreated(SurfaceHolder holder) {
//camera = Camera.open(cameraType);
prepareRecorder();
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
public void surfaceDestroyed(SurfaceHolder holder) {
if (recording) {
recorder.stop();
recording = false;
recorder.release();
}
recorder.release();
//finish();
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
there're no way to simply recordo to the front camera?
thank's!
EDIT
This is my actual code (and it don't work if i try to open front camera)
public void inizializzazione(){
cameraView.setVisibility(0);
boolean found = false;
int i;
for(i=0; i< Camera.getNumberOfCameras(); i++){
System.out.println("camera n " +i);
Camera.CameraInfo newInfo = new Camera.CameraInfo();
Camera. getCameraInfo(i, newInfo);
if (newInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
try {
found = true;
cam = Camera.open(i);
System.out.println("trovata la fotocamera frontale");
} catch (RuntimeException e) {
Log.e("Your_TAG", "Camera failed to open: " + e.getLocalizedMessage());
}
}
pathVideo = "/reg_" + System.currentTimeMillis() + ".mp4";
File Directory = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/CantaTu/");
// have the object build the directory structure, if needed.
Directory.mkdirs();
File mediaFile = new File(Directory,pathVideo);
if(mediaFile.exists()){
mediaFile.delete();
}
recorder = new MediaRecorder();
holder = cameraView.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
recorder.setVideoSource(1);
CamcorderProfile cpHigh = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
recorder.setProfile(cpHigh);
recorder.setOutputFile(mediaFile.getAbsolutePath());
recorder.setMaxDuration(400000); // 50 seconds
recorder.setPreviewDisplay(holder.getSurface());
try {
if(found == true){
recorder.setCamera(cam);
}
recorder.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
recorder.start();
scritta.setVisibility(0);
caricamento.setVisibility(4);
}
}
i have no idea why it don't open the front camera...
The error is "start called in an invalid state" (Because it found the front camera, and try to set)
You need to find the id of the front camera. To do that, go
boolean found = false;
int i;
for (i=0; i< Camera.getNumberOfCameras(); i++) {
Camera.CameraInfo newInfo = new Camera.CameraInfo();
Camera.getCameraInfo(i, newInfo);
if (newInfo.facing == CameraInfo.CAMERA_FACING_FRONT) {
found = true;
break;
}
}
If found is true, i is the front camera id. Then you need to open that camera and pass it in
recorder.setCamera(Camera.open(i));
it's too late to be answered but i ran into same problem and then finally I found out the problem on stack. You can set high profile for the back camera.
//For Front Camera
CamcorderProfile cpHigh = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
but you can't set high profile for the front camera. I used 480P in my case like
//For back camera
CamcorderProfile cpHigh = CamcorderProfile.get(CamcorderProfile.QUALITY_480P);
you can find details at this post. Android can't record video with Front Facing Camera, MediaRecorder start failed: -19

Record video with front facing camea

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) {
}
}

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