Android 录制MP4添加时间水印
时间: 2023-08-01 17:06:55 浏览: 134
Android视频添加时间水印
要在 Android 上录制 MP4 并添加时间水印,你可以使用 MediaRecorder 和 Canvas 对象。下面是一个简单的示例代码:
```java
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private MediaRecorder mediaRecorder;
private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;
private boolean isRecording = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化 SurfaceView 和 SurfaceHolder
surfaceView = findViewById(R.id.surface_view);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
// 初始化 MediaRecorder
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setVideoEncodingBitRate(10000000);
mediaRecorder.setVideoFrameRate(30);
mediaRecorder.setVideoSize(1280, 720);
mediaRecorder.setOutputFile(getOutputFile().getAbsolutePath());
mediaRecorder.setOrientationHint(90);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
try {
mediaRecorder.prepare();
} catch (IOException e) {
Log.e(TAG, "prepare() failed: " + e.getMessage());
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
releaseMediaRecorder();
}
});
}
private File getOutputFile() {
File dir = new File(Environment.getExternalStorageDirectory(), "MyVideo");
if (!dir.exists()) {
dir.mkdirs();
}
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault());
String currentTimeStamp = dateFormat.format(new Date());
return new File(dir.getAbsolutePath() + File.separator + "VID_" + currentTimeStamp + ".mp4");
}
private void releaseMediaRecorder() {
if (mediaRecorder != null) {
try {
mediaRecorder.stop();
mediaRecorder.reset();
mediaRecorder.release();
mediaRecorder = null;
} catch (Exception e) {
Log.e(TAG, "releaseMediaRecorder() failed: " + e.getMessage());
}
}
}
private void startRecording() {
mediaRecorder.start();
isRecording = true;
// 开始绘制时间水印
new Thread(new Runnable() {
@Override
public void run() {
Canvas canvas = surfaceHolder.lockCanvas();
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setTextSize(60);
while (isRecording) {
canvas.drawText(getCurrentTime(), 100, 100, paint);
surfaceHolder.unlockCanvasAndPost(canvas);
SystemClock.sleep(1000);
}
}
}).start();
}
private void stopRecording() {
isRecording = false;
releaseMediaRecorder();
}
private String getCurrentTime() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
return dateFormat.format(new Date());
}
public void onRecordBtnClick(View view) {
if (isRecording) {
stopRecording();
((Button) view).setText("Record");
} else {
startRecording();
((Button) view).setText("Stop");
}
}
}
```
上面的代码中,我们首先使用 MediaRecorder 和 SurfaceHolder 来初始化录制器和 SurfaceView。然后,我们在 surfaceCreated() 方法中初始化了 MediaRecorder 并将其准备好。在录制开始时,我们使用 Canvas 对象绘制时间水印,并将其加入到 SurfaceView 中。在录制结束时,我们释放了 MediaRecorder 并停止了绘制水印的线程。
需要注意的是,为了在 Android 上录制视频,你需要在 AndroidManifest.xml 文件中添加以下权限:
```xml
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
```
同时,你也需要在运行时请求这些权限。
阅读全文