为什么 Sceneform 1.17.1 只能正确显示音频而无法正确显示视频?

huangapple go评论76阅读模式
英文:

Why Sceneform 1.17.1 displays only audio and not video corectly?

问题

我正在使用Sceneform创建一个AR应用程序,但无论我尝试了什么,Sceneform都不能正确地在图像上显示视频。当相机检测到图像时,我得到的结果如下:

获取的视频图像

以下是我在相应类中的代码:

texture = new ExternalTexture();

mediaPlayer = MediaPlayer.create(this, R.raw.video);
mediaPlayer.setSurface(texture.getSurface());
mediaPlayer.setLooping(true);

ModelRenderable
        .builder()
        .setSource(this, Uri.parse("video_screen.sfb"))
        .build()
        .thenAccept(modelRenderable -> {
            renderable = modelRenderable;
            modelRenderable.getMaterial().setExternalTexture("videoTexture", texture);
            modelRenderable.getMaterial().setFloat4("keyColor",
                    new Color(0.01843f, 1f, 0.098f));
        })
        .exceptionally(
                throwable -> {
                    Toast toast = Toast.makeText(this, "无法加载视频可渲染项", Toast.LENGTH_LONG);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();
                    return null;
                });

arFragment = (com.stratos.syrostownhall.CustomArFragment)getSupportFragmentManager().findFragmentById(R.id.arFragment);

if (arFragment != null) {
    scene = arFragment.getArSceneView().getScene();
}

scene.addOnUpdateListener(this::onUpdate);

}

private void onUpdate(FrameTime frameTime) {

if (isImageDetected)
    return;

Frame frame = arFragment.getArSceneView().getArFrame();

Collection<AugmentedImage> augmentedImages =
        frame != null ? frame.getUpdatedTrackables(AugmentedImage.class) : null;


for (AugmentedImage painting : Objects.requireNonNull(augmentedImages)) {
    if (painting.getTrackingState() == TrackingState.TRACKING) {

        if (painting.getName().equals("painting")) {

            isImageDetected = true;

            playVideo(painting.createAnchor(painting.getCenterPose()), painting.getExtentX(),
                    painting.getExtentZ());

            break;
        }

    }
}

}

private void playVideo(Anchor anchor, float extentX, float extentZ) {

mediaPlayer.start();

AnchorNode anchorNode = new AnchorNode(anchor);

texture.getSurfaceTexture().setOnFrameAvailableListener(surfaceTexture -> {
    anchorNode.setRenderable(renderable);
    texture.getSurfaceTexture().setOnFrameAvailableListener(null);
});

anchorNode.setWorldScale(new Vector3(extentX, 1f, extentZ));

scene.addChild(anchorNode);

} 

另外我使用以下语法手动添加资产

sceneform.asset ('sampledata/video_screen.obj',
        'default',
        'sampledata/video_screen.sfa',
        'src/main/assets/video_screen')
英文:

I am creating an AR app using Sceneform, and no matter what I have tried this far Sceneform doesn't display a video correctly over an image. This is the result I get when the camera detects the image:
The Video Image that I get

Here is the code I have on the corresponding class:

texture = new ExternalTexture();

    mediaPlayer = MediaPlayer.create(this, R.raw.video);
mediaPlayer.setSurface(texture.getSurface());
mediaPlayer.setLooping(true);
ModelRenderable
.builder()
.setSource(this, Uri.parse(&quot;video_screen.sfb&quot;))
.build()
.thenAccept(modelRenderable -&gt; {
renderable = modelRenderable;
modelRenderable.getMaterial().setExternalTexture(&quot;videoTexture&quot;,
texture);
modelRenderable.getMaterial().setFloat4(&quot;keyColor&quot;,
new Color(0.01843f, 1f, 0.098f));
})
.exceptionally(
throwable -&gt; {
Toast toast = Toast.makeText(this, &quot;Unable to load video renderable&quot;, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return null;
});
arFragment = (com.stratos.syrostownhall.CustomArFragment)getSupportFragmentManager().findFragmentById(R.id.arFragment);
if (arFragment != null) {
scene = arFragment.getArSceneView().getScene();
}
scene.addOnUpdateListener(this::onUpdate);
}
private void onUpdate(FrameTime frameTime) {
if (isImageDetected)
return;
Frame frame = arFragment.getArSceneView().getArFrame();
Collection&lt;AugmentedImage&gt; augmentedImages =
frame != null ? frame.getUpdatedTrackables(AugmentedImage.class) : null;
for (AugmentedImage painting : Objects.requireNonNull(augmentedImages)) {
if (painting.getTrackingState() == TrackingState.TRACKING) {
if (painting.getName().equals(&quot;painting&quot;)) {
isImageDetected = true;
playVideo(painting.createAnchor(painting.getCenterPose()), painting.getExtentX(),
painting.getExtentZ());
break;
}
}
}
}
private void playVideo(Anchor anchor, float extentX, float extentZ) {
mediaPlayer.start();
AnchorNode anchorNode = new AnchorNode(anchor);
texture.getSurfaceTexture().setOnFrameAvailableListener(surfaceTexture -&gt; {
anchorNode.setRenderable(renderable);
texture.getSurfaceTexture().setOnFrameAvailableListener(null);
});
anchorNode.setWorldScale(new Vector3(extentX, 1f, extentZ));
scene.addChild(anchorNode);
} 

Also, I add the asset manually using the following syntax:

sceneform.asset (&#39;sampledata/video_screen.obj&#39;,
&#39;default&#39;,
&#39;sampledata/video_screen.sfa&#39;,
&#39;src/main/assets/video_screen&#39;)

答案1

得分: 0

问题是由 video_screen.sfb 文件引起的。

如果还有其他人遇到同样的问题,请尝试使用以下的 video_screen 文件:

链接:https://github.com/heyletscode/Play-Video-On-Augmented-Image/blob/master/Project/app/src/main/assets/video_screen.sfb

英文:

The problem was caused by the video_screen.sfb file.

If anyone else is facing the same issue try using the following video_screen file:

https://github.com/heyletscode/Play-Video-On-Augmented-Image/blob/master/Project/app/src/main/assets/video_screen.sfb

huangapple
  • 本文由 发表于 2020年8月20日 05:06:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63494992.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定