Skip to content

Commit

Permalink
Add sound to the aniumated Depth Panda model
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasGorisse committed Jan 6, 2022
1 parent 394d072 commit 65d6e6e
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@

import android.net.Uri;
import android.os.Bundle;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.ar.core.exceptions.CameraNotAvailableException;
import com.google.ar.sceneform.Node;
import com.google.ar.sceneform.SceneView;
import com.google.ar.sceneform.math.Quaternion;
import com.google.ar.sceneform.math.Vector3;
import com.google.ar.sceneform.rendering.ModelRenderable;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.google.ar.sceneform.samples.depth;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.MotionEvent;
Expand All @@ -24,11 +27,13 @@
import com.google.ar.sceneform.rendering.CameraStream;
import com.google.ar.sceneform.rendering.ModelRenderable;
import com.google.ar.sceneform.rendering.Renderable;
import com.google.ar.sceneform.rendering.RenderableInstance;
import com.google.ar.sceneform.rendering.ViewRenderable;
import com.google.ar.sceneform.ux.ArFragment;
import com.google.ar.sceneform.ux.BaseArFragment;
import com.google.ar.sceneform.ux.TransformableNode;

import java.io.IOException;
import java.lang.ref.WeakReference;

public class MainActivity extends AppCompatActivity implements
Expand All @@ -39,6 +44,8 @@ public class MainActivity extends AppCompatActivity implements

private ArFragment arFragment;
private Renderable model;
private ObjectAnimator modelAnimator;
private MediaPlayer modelSound = new MediaPlayer();
private ViewRenderable viewRenderable;

@Override
Expand Down Expand Up @@ -78,6 +85,34 @@ public void onViewCreated(ArSceneView arSceneView) {
.setDepthOcclusionMode(CameraStream.DepthOcclusionMode.DEPTH_OCCLUSION_ENABLED);
}

@Override
protected void onStart() {
super.onStart();

if (modelAnimator != null && modelAnimator.isPaused()) {
modelAnimator.start();
modelSound.start();
}
}

@Override
protected void onStop() {
super.onStop();

if (modelAnimator != null && modelAnimator.isRunning()) {
modelAnimator.pause();
modelSound.pause();
}
}

@Override
protected void onDestroy() {
super.onDestroy();

stopSound();
modelSound.release();
}

public void loadModels() {
WeakReference<MainActivity> weakActivity = new WeakReference<>(this);
ModelRenderable.builder()
Expand Down Expand Up @@ -123,20 +158,59 @@ public void onTapPlane(HitResult hitResult, Plane plane, MotionEvent motionEvent
anchorNode.setParent(arFragment.getArSceneView().getScene());

// Create the transformable model and add it to the anchor.
TransformableNode model = new TransformableNode(arFragment.getTransformationSystem());
model.setParent(anchorNode);
model.setRenderable(this.model)
.animate(true).start();
model.select();
TransformableNode modelNode = new TransformableNode(arFragment.getTransformationSystem());
modelNode.setParent(anchorNode);
RenderableInstance modelInstance = modelNode.setRenderable(this.model);
modelAnimator = modelInstance.animate(true);
modelAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
playSound();
}

@Override
public void onAnimationEnd(Animator animation) {
stopSound();
}

@Override
public void onAnimationCancel(Animator animation) {
stopSound();
}

@Override
public void onAnimationRepeat(Animator animation) {
stopSound();
playSound();
}
});
modelAnimator.start();
modelNode.select();

Node titleNode = new Node();
titleNode.setParent(model);
titleNode.setParent(modelNode);
titleNode.setEnabled(false);
titleNode.setLocalPosition(new Vector3(0.0f, 1.0f, 0.0f));
titleNode.setRenderable(viewRenderable);
titleNode.setEnabled(true);
}

private void playSound() {
try {
// Can't figure out why does the repeat function doesn't for remote url
modelSound.setDataSource("https://storage.googleapis.com/ar-answers-in-search-models/static/GiantPanda/Bear_Panda_Giant_Unisex_Adult.ogg");
modelSound.prepare();
modelSound.start();
} catch (IOException e) {
e.printStackTrace();
}
}

private void stopSound() {
modelSound.stop();
modelSound.reset();
}

@Override
public void onSessionConfiguration(Session session, Config config) {
// Comment this in to feed the DepthTexture with Raw Depth Data.
Expand Down
2 changes: 1 addition & 1 deletion ux/src/main/java/com/google/ar/sceneform/ux/VideoNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private void init(Context context) {
*
* @param player the media player
* @param material the material to apply to your custom model
* @return the the renderable with the applied material.
* @return the renderable with the applied material.
*/
public Renderable createModel(MediaPlayer player, Material material) {
final int width = player.getVideoWidth();
Expand Down

0 comments on commit 65d6e6e

Please sign in to comment.