I am trying to make an app that records a video using the camera app, and then saves that video on SD card so I can play it. I have some code but I'm lost on how to continue as I'm a beginner in Android.
My Activity:
public class Camcorder extends Activity {
private CamcorderView camcorderView;
private boolean recording = false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//irrelevant code
camcorderView = (CamcorderView) findViewById(R.id.camcorder_preview);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER)
{
if (recording) {
camcorderView.stopRecording();
finish();
} else {
recording = true;
camcorderView.startRecording();
}
return true;
}
return super.onKeyDown(keyCode, event);
}
}
CamcorderView class:
public class CamcorderView extends SurfaceView implements
SurfaceHolder.Callback {
MediaRecorder recorder;
SurfaceHolder holder;
String outputFile = "/sdcard/default.mp4";
public CamcorderView(Context context, AttributeSet attrs) {
super(context, attrs);
holder = getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
// recorder.setVideoSize(480, 320);
// recorder.setVideoFrameRate(15);
// recorder.setMaxDuration(10000);
}
public void surfaceCreated(SurfaceHolder holder) {
recorder.setOutputFile(outputFile);
recorder.setPreviewDisplay(holder.getSurface());
if (recorder != null) {
try {
recorder.prepare();
} catch (IllegalStateException e) {
Log.e("IllegalStateException", e.toString());
} catch (IOException e) {
Log.e("IOException", e.toString());
}
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
public void surfaceDestroyed(SurfaceHolder holder) {
}
public void setOutputFile(String filename)
{
outputFile = filename;
recorder.setOutputFile(filename);
}
public void startRecording()
{
recorder.start();
}
public void stopRecording()
{
recorder.stop();
recorder.release();
}
}
Record a video with a camera app The Android way of delegating actions to other applications is to invoke an Intentthat describes what you want done. This process involves three pieces: The Intentitself, a call to start the external Activity, and some code to handle the video when focus returns to your activity.
To capture moving pictures, or video, with your Android phone, switch the camera mode in the Camera app to video recording. The same icon is used to switch between still and moving images. When video mode is active, the Camera app’s screen changes subtly: The Shutter icon becomes a Record icon. Touch that icon to start recording video.
This Android version supports video recording and playback. Android 1.5 (Cupcake) and later releases (Donut and Eclair) have APIs for recording video. But, it is difficult to find relevant, lucid and simple articles that explain how to use the Android APIs for recording video.
Tap the overlay to start a video, take a screenshot, or view captured media and change settings. You can start a screen recording from the AirCircle menu, then pause or stop the video. Commands in the notification shade allow you some degree of control as well.
Well it's very simple to record videos in android by using this simple code
First on a button click simple start an Intent
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takeVideoIntent, CAMERA_REQUEST_CODE_VEDIO);
}
then in onActivityResult
method
Uri videoUri = data.getData();
path = Utils.getRealPathFromURI(videoUri, this);
manageVideo(path); //Do whatever you want with your video
and finally add permissions to the manifest
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
Hope it help others who are looking for help :)
Thanks
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With