Android点击拍摄长按录像代码
时间: 2023-08-08 16:12:32 浏览: 130
以下是一个示例代码,可以在 Android 应用程序中实现点击拍照和长按录制视频的功能:
1. 在布局文件中添加一个 Button 和一个 TextureView 控件:
```
<Button
android:id="@+id/btn_capture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="拍照" />
<TextureView
android:id="@+id/textureView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
2. 在 Activity 中定义变量和初始化控件:
```
private static final int REQUEST_VIDEO_CAPTURE = 1;
private TextureView textureView;
private Button btnCapture;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textureView = findViewById(R.id.textureView);
btnCapture = findViewById(R.id.btn_capture);
textureView.setSurfaceTextureListener(textureListener);
btnCapture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
takePicture();
}
});
// 设置长按事件监听器
btnCapture.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
startRecording();
return true;
}
});
}
```
3. 实现拍照功能:
```
private void takePicture() {
if (textureView == null) {
return;
}
Bitmap bitmap = textureView.getBitmap();
if (bitmap == null) {
return;
}
// 保存图片
String fileName = "IMG_" + System.currentTimeMillis() + ".jpg";
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + fileName);
try {
FileOutputStream outputStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.flush();
outputStream.close();
Toast.makeText(this, "照片已保存", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "保存照片失败", Toast.LENGTH_SHORT).show();
}
}
```
4. 实现录制视频功能:
```
private MediaRecorder mediaRecorder;
private String videoFileName;
private void startRecording() {
if (textureView == null) {
return;
}
try {
mediaRecorder = new MediaRecorder();
// 设置音频来源和视频来源
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
// 设置输出格式和文件名
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
videoFileName = "VID_" + System.currentTimeMillis() + ".mp4";
mediaRecorder.setOutputFile(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + videoFileName);
// 设置视频编码器和音频编码器
mediaRecorder.setVideoEncodingBitRate(10000000);
mediaRecorder.setVideoFrameRate(30);
mediaRecorder.setVideoSize(textureView.getWidth(), textureView.getHeight());
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
// 设置预览
mediaRecorder.setPreviewDisplay(textureView.getSurfaceTexture());
// 准备开始录制
mediaRecorder.prepare();
mediaRecorder.start();
Toast.makeText(this, "开始录制", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "录制视频失败", Toast.LENGTH_SHORT).show();
}
}
private void stopRecording() {
if (mediaRecorder == null) {
return;
}
mediaRecorder.stop();
mediaRecorder.release();
mediaRecorder = null;
Toast.makeText(this, "录制完成,文件保存在 " + Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + videoFileName, Toast.LENGTH_LONG).show();
}
```
5. 在 Activity 中添加 onActivityResult() 方法,处理视频录制完成后返回的结果:
```
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) {
Toast.makeText(this, "视频已保存", Toast.LENGTH_SHORT).show();
}
}
```
6. 在 startRecording() 方法中添加计时器,当长按时间达到一定时长时停止录制:
```
private static final int MAX_RECORD_TIME = 10 * 1000; // 最长录制时间为 10 秒
private Timer timer;
private boolean isRecording = false;
private void startRecording() {
// ...
isRecording = true;
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (isRecording) {
stopRecording();
}
}
});
}
}, MAX_RECORD_TIME);
}
private void stopRecording() {
// ...
isRecording = false;
timer.cancel();
}
```
阅读全文