在 Android 中录制、保存和播放视频
在 Android 应用中录制、保存和播放视频变得越来越常见。为了实现这些功能,我们可以使用以下技术:
1. 录制视频
使用 Android Camera2 API 可以实现录制视频的功能。为了控制录制过程,可以使用 MediaRecorder
。以下代码示例展示了如何使用 Camera2 API 录制视频:
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class VideoRecorderActivity extends AppCompatActivity {
private static final String TAG = "VideoRecorderActivity";
private static final int REQUEST_CODE = 1;
private static final String DEFAULT_VIDEO_PATH = "video.mp4";
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.US);
private MediaProjectionManager mediaProjectionManager;
private MediaProjection mediaProjection;
private DisplayMetrics displayMetrics;
private int screenWidth;
private int screenHeight;
private FrameLayout videoView;
private SurfaceView surfaceView;
private CameraDevice cameraDevice;
private CameraCaptureSession captureSession;
private CaptureRequest.Builder captureRequestBuilder;
private Surface surface;
private boolean isPreviewRunning;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_recorder);
mediaProjectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
displayMetrics = getResources().getDisplayMetrics();
screenWidth = displayMetrics.widthPixels;
screenHeight = displayMetrics.heightPixels;
videoView = findViewById(R.id.video_view);
surfaceView = findViewById(R.id.surface_view);
findViewById(R.id.record_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startRecording();
}
});
findViewById(R.id.stop_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopRecording();
}
});
}
private void startRecording() {
stopRecording(); // 先停止录制
Intent intent = mediaProjectionManager.createScreenCaptureIntent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(intent, REQUEST_CODE);
}
private void stopRecording() {
if (captureSession != null) {
captureSession.close();
captureSession = null;
}
if (cameraDevice != null) {
cameraDevice.close();
cameraDevice = null;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap bitmap = extras.getParcelable("data");
videoView.setVideoPath(getFilename());
videoView.setMediaController(new MediaController(this));
videoView.requestFocus();
videoView.start();
} else {
Toast.makeText(this, "Screen capture failed, please try again", Toast.LENGTH_SHORT).show();
}
}
}
private String getFilename() {
File videoFolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES), "Camera");
if (!videoFolder.exists()) {
videoFolder.mkdirs();
}
return videoFolder.getAbsolutePath() + "/" +
领取专属 10元无门槛券
手把手带您无忧上云