? This is recommended when using CameraX to record video.
You can learn it from
here and
here.
If you really need MediaRecorder, there is a workaround but not guarantee to work normally on every device. Sometimes on some devices it may need some extra work to get things going.
i.e. create a Preview use case as the surface receiver of MediaRecorder.
ex:
Size videoSize = new Size(640, 480); // you have to find out a device supported resolution, see
sample
mRecorder = new MediaRecorder();
// setup recorder
mRecorder.setVideoSize(videoSize.getWidth(), videoSize.getHeight());
mRecorder.prepare();
Preview preview = new Preview.Builder()
.setTargetResolution(videoSize)
.build();
preview.setSurfaceProvider(new Preview.SurfaceProvider() {
@Override
public void onSurfaceRequested(@NonNull SurfaceRequest request) {
request.provideSurface(mRecorder.getSurface(), mExecutor,
new Consumer<SurfaceRequest.Result>() {
@Override
public void accept(SurfaceRequest.Result result) {
// A safe place to release mRecorder since camera no longer use the surface.
}
});
}
});