Mimic the elephant

In this example, we use the Actuation service to set up an Animate action and we use the mediaPlayer to play a sound that will be triggered on the robot’s speakers.

elephant.anim

elephant_sound.wav

package com.aldebaran.qi.sdksample;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.aldebaran.qi.sdk.object.actuation.Animate;
import com.aldebaran.qi.sdk.object.actuation.Animation;


public class ElephantActivity extends AppCompatActivity {

    private MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_elephant);
    }

    @Override
    protected void onStart() {
        super.onStart();
        mediaPlayer = MediaPlayer.create(this, R.raw.elephant_sound);
        if (mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = MediaPlayer.create(this, R.raw.elephant_sound);
        }
        Animation animation = Animation.fromResources(this, R.raw.elephant);
        Animate animate = new Animate(this);
        animate.setOnStartedListener(new Animate.OnStartedListener() {
            @Override
            public void onStarted() {
                mediaPlayer.start();
            }
        });
        animate.run(animation);
    }

    @Override
    protected void onStop() {
        super.onStop();
        mediaPlayer.stop();
    }
}