android 10 MediaRecorder录像成mp4文件,视频添加时间水印的实现方式
时间: 2023-11-27 10:51:56 浏览: 84
Android使用MediaRecorder实现录像功能
可以通过以下步骤实现在录制视频时添加时间水印:
1. 创建一个自定义的SurfaceView,用于预览和录制视频。
2. 在SurfaceView的回调函数中创建MediaRecorder对象,并设置相关参数:
```
mMediaRecorder = new MediaRecorder();
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mMediaRecorder.setVideoSize(1280, 720);
mMediaRecorder.setVideoFrameRate(30);
mMediaRecorder.setVideoEncodingBitRate(3 * 1024 * 1024);
mMediaRecorder.setOutputFile(mOutputFile.getAbsolutePath());
mMediaRecorder.setOrientationHint(rotation);
```
3. 在录制视频之前,创建一个Canvas对象,通过drawText方法在Canvas上绘制当前时间,再将Canvas中的Bitmap作为水印覆盖到SurfaceView上。可以使用Handler定时更新时间水印。
4. 在录制视频开始后,通过MediaCodec将SurfaceView中的内容编码为视频流,并将视频流写入文件中。同时,每次绘制时间水印时,也将SurfaceView的内容编码为视频流,并写入文件中。
5. 录制完成后,释放MediaRecorder和Canvas对象。
完整代码示例:
```
public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {
private static final String TAG = "MainActivity";
private static final int PERMISSION_REQUEST_CODE = 1;
private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;
private MediaRecorder mMediaRecorder;
private File mOutputFile;
private boolean mIsRecording = false;
private int mRotation;
private Bitmap mWatermarkBitmap;
private Paint mWatermarkPaint;
private Handler mHandler = new Handler();
private Runnable mUpdateWatermarkRunnable = new Runnable() {
@Override
public void run() {
updateWatermark();
mHandler.postDelayed(this, 1000);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSurfaceView = findViewById(R.id.surface_view);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA},
PERMISSION_REQUEST_CODE);
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
mWatermarkPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mWatermarkPaint.setColor(Color.WHITE);
mWatermarkPaint.setTextSize(40);
mWatermarkPaint.setTextAlign(Paint.Align.CENTER);
mWatermarkBitmap = Bitmap.createBitmap(1280, 720, Bitmap.Config.ARGB_8888);
mHandler.post(mUpdateWatermarkRunnable);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
mRotation = getWindowManager().getDefaultDisplay().getRotation();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mHandler.removeCallbacks(mUpdateWatermarkRunnable);
releaseMediaRecorder();
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_record:
if (!mIsRecording) {
startRecording();
} else {
stopRecording();
}
break;
}
}
private void updateWatermark() {
Canvas canvas = new Canvas(mWatermarkBitmap);
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.getDefault());
String time = sdf.format(new Date());
canvas.drawText(time, 640, 360, mWatermarkPaint);
Canvas surfaceCanvas = mSurfaceHolder.lockCanvas();
surfaceCanvas.drawBitmap(mWatermarkBitmap, 0, 0, null);
mSurfaceHolder.unlockCanvasAndPost(surfaceCanvas);
}
private void startRecording() {
try {
mIsRecording = true;
mOutputFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),
"video_" + System.currentTimeMillis() + ".mp4");
mMediaRecorder = new MediaRecorder();
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mMediaRecorder.setVideoSize(1280, 720);
mMediaRecorder.setVideoFrameRate(30);
mMediaRecorder.setVideoEncodingBitRate(3 * 1024 * 1024);
mMediaRecorder.setOutputFile(mOutputFile.getAbsolutePath());
mMediaRecorder.setOrientationHint(mRotation);
mMediaRecorder.prepare();
mMediaRecorder.start();
} catch (IOException e) {
Log.e(TAG, "startRecording: ", e);
}
}
private void stopRecording() {
mIsRecording = false;
releaseMediaRecorder();
}
private void releaseMediaRecorder() {
if (mMediaRecorder != null) {
mMediaRecorder.stop();
mMediaRecorder.reset();
mMediaRecorder.release();
mMediaRecorder = null;
}
}
}
```
注意:本示例中仅提供了添加时间水印的实现方式,如需完整的录像功能还需要添加权限申请、录制按钮等相关逻辑。
阅读全文