public class RecentVideo extends Activity implements SurfaceHolder.Callback {
private Button startRecording = null;
private Button stopRecording = null;
MediaRecorder recorder;
SurfaceHolder holder;
boolean recording = false;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
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();
initRecorder();
setContentView(R.layout.try_video);
startRecording = (Button)findViewById(R.id.buttonstart);
stopRecording =(Button)findViewById(R.id.buttonstop);
SurfaceView cameraView = (SurfaceView) findViewById(R.id.surface_camera);
holder = cameraView.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
cameraView.setClickable(true);
initGUI();
}
private void initGUI() {
startRecording.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Log.d("**************", "started");
// initRecorder();
// if(!recording){
// initRecorder();
// prepareRecorder();
recording = true;
recorder.start();
// }
/* if (recording) {
recorder.stop();
recording = false;
// Let's initRecorder so we can record again
initRecorder();
prepareRecorder();
} else {
recording = true;
recorder.start();
}*/
}
});
stopRecording.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
recorder.stop();
recording = false;
//initRecorder();
// prepareRecorder();
}
}) ;
}
@SuppressLint("SdCardPath") private void initRecorder() {
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
CamcorderProfile cpHigh = CamcorderProfile
.get(CamcorderProfile.QUALITY_LOW);
recorder.setProfile(cpHigh);
// String file = Environment.getDataDirectory()+"/videocapture_example.mp4";
// File f = new File(file);
// if (!f.isFile()){
// try {
// f.createNewFile();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
recorder.setOutputFile("/sdcard/videocapture_example.mp4");
recorder.setMaxDuration(50000); // 50 seconds
// recorder.setMaxFileSize(5000000); // 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();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
prepareRecorder();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
this activity can be called the necessary button
private Button startRecording = null;
private Button stopRecording = null;
MediaRecorder recorder;
SurfaceHolder holder;
boolean recording = false;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
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();
initRecorder();
setContentView(R.layout.try_video);
startRecording = (Button)findViewById(R.id.buttonstart);
stopRecording =(Button)findViewById(R.id.buttonstop);
SurfaceView cameraView = (SurfaceView) findViewById(R.id.surface_camera);
holder = cameraView.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
cameraView.setClickable(true);
initGUI();
}
private void initGUI() {
startRecording.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Log.d("**************", "started");
// initRecorder();
// if(!recording){
// initRecorder();
// prepareRecorder();
recording = true;
recorder.start();
// }
/* if (recording) {
recorder.stop();
recording = false;
// Let's initRecorder so we can record again
initRecorder();
prepareRecorder();
} else {
recording = true;
recorder.start();
}*/
}
});
stopRecording.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
recorder.stop();
recording = false;
//initRecorder();
// prepareRecorder();
}
}) ;
}
@SuppressLint("SdCardPath") private void initRecorder() {
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
CamcorderProfile cpHigh = CamcorderProfile
.get(CamcorderProfile.QUALITY_LOW);
recorder.setProfile(cpHigh);
// String file = Environment.getDataDirectory()+"/videocapture_example.mp4";
// File f = new File(file);
// if (!f.isFile()){
// try {
// f.createNewFile();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
recorder.setOutputFile("/sdcard/videocapture_example.mp4");
recorder.setMaxDuration(50000); // 50 seconds
// recorder.setMaxFileSize(5000000); // 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();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
prepareRecorder();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
this activity can be called the necessary button
No comments:
Post a Comment