Back to project page video.
The source code is released under:
Apache License
If you think the Android project video listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package edu.cs4730.videocapture3; // www.ja v a2 s . c o m import java.io.IOException; import android.app.Activity; import android.content.pm.ActivityInfo; import android.media.CamcorderProfile; import android.media.MediaRecorder; import android.os.Bundle; import android.os.Environment; import android.view.Menu; import android.view.MenuItem; 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; public class MainActivity extends Activity implements OnClickListener, SurfaceHolder.Callback { MediaRecorder recorder; SurfaceHolder holder; boolean recording = false; int num = 0; @Override protected 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(); initRecorder(); setContentView(R.layout.activity_main); 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); } private void initRecorder() { // recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT); recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT); CamcorderProfile cpHigh = CamcorderProfile .get(CamcorderProfile.QUALITY_HIGH); recorder.setProfile(cpHigh); num++; //so it doesn't overwrite the previous file. recorder.setOutputFile(Environment.getExternalStorageDirectory().getPath()+"/videocapture_example"+num+".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(); } } public void onClick(View v) { if (recording) { recorder.stop(); recording = false; // Let's initRecorder so we can record again 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) { if (recording) { recorder.stop(); recording = false; } recorder.release(); finish(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }