Making a multimodal presentation

Part 3: Making sound

Pepper’s speech can be completed by playing arbitrary sound files.

Part 3
Schema of Pepper performance - part 3

1. Adding a media player

Playing sound requires a MediaPlayer object.

Add one as a member of the activity:

private lateinit var mediaPlayer: MediaPlayer

You will need to initialize this one and destroy it in the android lifecycle functions:

////////////////////////////
// Application Life Cycle //
////////////////////////////

override fun onCreate(savedInstanceState: Bundle?) {
   super.onCreate(savedInstanceState)
   Log.i(TAG, "onCreate")
   QiSDK.register(this, this)
   setSpeechBarDisplayStrategy(SpeechBarDisplayStrategy.OVERLAY)
   mediaPlayer = MediaPlayer()
   setContentView(R.layout.main_activity)
}

override fun onPause() {
   super.onPause()
   mediaPlayer.stop()
}

override fun onDestroy() {
   super.onDestroy()
   QiSDK.unregister(this, this)
   mediaPlayer.release()
}

Note that you should stop it on activity pause so it doesn’t keep playing in the background.

And, as usual, add a helper function to use it:

private fun playMedia(mediaResource: Int) {
   mediaPlayer.reset()
   mediaPlayer = MediaPlayer.create(applicationContext, mediaResource)
   mediaPlayer.start()
}

2. Adding sound to the presentation

You can now continue the sequence:

////////////////////////
// Presentation logic //
////////////////////////

private fun runPresentation() {

   // ...

   // Part 3: "I can make sound"

   playMedia(R.raw.stone_breaks)
   setImage(R.drawable.scene5)
   makeSay("Like me, a rock can make sound.").run()
   setImage(R.drawable.scene6)
   Thread.sleep(1_000)
   setImage(R.drawable.scene7)
   makeSay("and light.").run()
   Thread.sleep(1_000)
   setImage(R.drawable.scene8)

   // Part 4: "And become beautiful"

}