当检测到图像时,我能够成功在图像上渲染视频。问题是,当我从该位置删除图像时,视频仍继续在该位置显示,而该图像不再存在。下面是我用来创建ARScene
时的代码。
private fun createArScene() {
// Create an ExternalTexture for displaying the contents of the video.
externalTexture = ExternalTexture().also {
mediaPlayer.setSurface(it.surface)
}
// Create a renderable with a material that has a parameter of type 'samplerExternal' so that
// it can display an ExternalTexture.
ModelRenderable.builder()
.setSource(requireContext(), R.raw.augmented_video_model)
.build()
.thenAccept { renderable ->
videoRenderable = renderable
renderable.isShadowCaster = false
renderable.isShadowReceiver = false
renderable.material.setExternalTexture("videoTexture", externalTexture)
}
.exceptionally { throwable ->
Log.e(TAG, "Could not create ModelRenderable", throwable)
return@exceptionally null
}
videoAnchorNode = VideoAnchorNode().apply {
setParent(arSceneView.scene)
}
}
所以我相信我需要继续检查externalTexture对象,但问题是我的系统将如何知道我不再关注图像了。这样我才能杀死它?
在更新侦听器上,您可以跟踪下面的图像,
Frame frame = arFragment.getArSceneView().getArFrame();
Collection<AugmentedImage> augmentedImages =
frame.getUpdatedTrackables((AugmentedImage.class));
for (AugmentedImage image: augmentedImages){
if(image.getTrackingState() == TrackingState.TRACKING){
if(image.getName().equals("image")){
isImageDetected = true;
playVideo(image.createAnchor(image.getCenterPose()),
image.getExtentX(), image.getExtentZ());
break;
}
}
}
您可以检查跟踪状态,例如已停止,已暂停和正在跟踪