在Android中,可以通过编程方式捕获无音频的视频。以下是一种实现方法:
下面是一个简单的示例代码:
import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import java.io.File;
import java.io.IOException;
public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {
private Camera mCamera;
private SurfaceView mPreview;
private MediaRecorder mMediaRecorder;
private Button mRecordButton;
private boolean isRecording = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPreview = findViewById(R.id.surfaceView);
mRecordButton = findViewById(R.id.recordButton);
mRecordButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isRecording) {
stopRecording();
} else {
startRecording();
}
}
});
SurfaceHolder holder = mPreview.getHolder();
holder.addCallback(this);
}
private void startRecording() {
mCamera.unlock();
mMediaRecorder = new MediaRecorder();
mMediaRecorder.setCamera(mCamera);
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
mMediaRecorder.setOutputFile(getOutputMediaFile().toString());
try {
mMediaRecorder.prepare();
mMediaRecorder.start();
isRecording = true;
mRecordButton.setText("Stop");
} catch (IOException e) {
e.printStackTrace();
}
}
private void stopRecording() {
mMediaRecorder.stop();
mMediaRecorder.reset();
mMediaRecorder.release();
mCamera.lock();
isRecording = false;
mRecordButton.setText("Record");
}
private File getOutputMediaFile() {
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM), "Camera");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
return null;
}
}
String timeStamp = String.valueOf(System.currentTimeMillis());
return new File(mediaStorageDir.getPath() + File.separator +
"VID_" + timeStamp + ".mp4");
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
mCamera = Camera.open();
mCamera.setDisplayOrientation(90);
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if (holder.getSurface() == null) {
return;
}
try {
mCamera.stopPreview();
} catch (Exception e) {
e.printStackTrace();
}
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.release();
}
}
这个示例代码演示了如何在Android中通过编程方式捕获无音频的视频。它使用了Camera类来打开摄像头,并使用MediaRecorder类来设置视频源和音频源,并进行录制。同时,它还使用了SurfaceView来显示摄像头的预览画面。
在这个示例中,需要注意的是,需要在AndroidManifest.xml文件中添加相应的权限,如摄像头权限和存储权限。
推荐的腾讯云相关产品:腾讯云移动直播(https://cloud.tencent.com/product/mlvb)
这是腾讯云提供的移动直播解决方案,可以帮助开发者快速搭建移动直播平台,支持视频录制、推流、播放等功能。
领取专属 10元无门槛券
手把手带您无忧上云