hi.
i have been trying to record camera using MediaRecorder.
i searched some sample source code.
but they dose not work...
actually i succeeded recording audio, but i can't record video.
is there someone who advice me?
---------------- source code -----------------------
package lbj.camera.test;
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class CameraTest2 extends Activity {
public MediaRecorder mrec = null;
File videofile;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPreview = new Preview(this);
setContentView(mPreview);
}
private Preview mPreview;
SurfaceView sv;
SurfaceHolder holder;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Capture");
menu.add("Save");
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getTitle().equals("Capture")) {
try {
mPreview.record();
} catch (Exception e) {
e.printStackTrace();
Log.d("Has not finished here:", "Starting
video");
}
} else if (item.getTitle().equals("Save")) {
mPreview.saveAndStop();
}
return true;
}
}
class Preview extends SurfaceView implements SurfaceHolder.Callback {
SurfaceHolder mHolder;
Camera mCamera;
Context context;
MediaRecorder recorder = null;
public void record() throws Exception{
File audiofile = null;
try {
String mSdCardPath =
Environment.getExternalStorageDirectory().getAbsolutePath();
audiofile = new File(mSdCardPath + File.separator +
"test.3gp");
audiofile.createNewFile();
} catch (Exception e) {
throw e;
}
recorder = new MediaRecorder();
recorder.setCamera(mCamera);
recorder.setPreviewDisplay(mHolder.getSurface());
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setOutputFile(audiofile.getAbsolutePath());
recorder.setVideoSize(176, 144);
recorder.setVideoFrameRate(15);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
Log.d("Has Entered here:", "Starting video");
if (recorder != null) {
try {
recorder.prepare();
recorder.start();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Preview(Context context) {
super(context);
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera = Camera.open();
mCamera.setPreviewDisplay(holder);
} catch (IOException e) {
e.printStackTrace();
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
Log.d("Stopping preview", "in preview class");
mCamera.stopPreview();
mCamera = null;
}
public void surfaceChanged(SurfaceHolder holder, int format, int w,
int h) {
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(w, h);
mCamera.setParameters(parameters);
mCamera.startPreview();
}
public void saveAndStop() {
if (recorder != null) {
try {
recorder.stop();
recorder.reset();
recorder.release();
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
}
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en