我正在尝试将此库集成到我的片段中。在本教程中,他们说在 camera.setLifecycleOwner(this) 中调用 fragment.viewLifecycleOwner 而不是 this。我已经完成了开始视频录制的所有过程。我也尝试调试它,但在调用 camera.startCapturingVideo(file) 后它没有得到 onVideoTaken() 的回调。我的视频文件正在路径中创建,但在播放时,它显示无法播放此视频。我认为这是因为我没有收到回调。下面是我的片段:
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
covered = InhalerData.getInstance().manifest.getPlayerType();
if (covered != null) {
if (covered.equalsIgnoreCase("covered")) {
view = inflater.inflate(R.layout.fragment_video, container, false);
} else {
view = inflater.inflate(R.layout.fragment_video_side_by_side, container, false);
}
}
mContext = getActivity();
setViews();
return view;
}
private void setViews() {
//initializing views
video = view.findViewById(R.id.video);
textureView = view.findViewById(R.id.camera_texture);
parentLayout = view.findViewById(R.id.parent_layout);
overLay = view.findViewById(R.id.overlay);
tv_tips = view.findViewById(R.id.tips);
textureView.setLifecycleOwner(VideoPlayerFragment.this);
return true;
}
});
textureView.addCameraListener(new CameraListener() {
@Override
public void onVideoTaken(File video) {
super.onVideoTaken(video);
onVideo(video);
}
});
}
//This method is called on record button click
private void captureVideo(String name) {
if (textureView.getSessionType() != SessionType.VIDEO) {
return;
}
isRecordingOn = false;
File mediaFile =
new File(Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/" + name + VIDEO_EXTENSION);
textureView.setVideoCodec(VideoCodec.H_264);
textureView.startCapturingVideo(mediaFile, 8000);
if (mRecordingTimer != null) {
mRecordingTimer.start();
}
startBlinking();
textureView.postDelayed(new Runnable() {
@Override
public void run() {
// This will trigger onVideoTaken().
textureView.stopCapturingVideo();
}
}, 8000);
}
如何在片段中添加回调,我应该通过什么来代替这个。