Creating a 3D Scene with Sounds using Huawei’s Scene Kit and Audio Kit with Kotlin
Hi everyone!
Today I will be briefing through how to implement a 3D Scene to display objects and play sounds in your Andorid Kotlin projects. All we need is Android Studio with version 3.5 or higher and a smartphone running Android 4.4 or later. The kits we need requires these specifications at minimum:
For Scene Kit alone:
- JDK version: 1.7 or later
- minSdkVersion: 19 or later
- targetSdkVersion: 19 or later
- compileSdkVersion: 19 or later
- Gradle version: 3.5 or later
For Audio Kit alone:
- JDK version: 1.8.211 or later
- minSdkVersion: 21
- targetSdkVersion: 29
- compileSdkVersion: 29
- Gradle version: 4.6 or later
That brings us to use Audio Kit’s minimum requirements as Scene Kit requirements are lower. So we should keep those in our minds while configuring our project. Let’s begin with implementing Scene Kit.
First of all, our aim with this Scene Kit implementation is to achieve a view of 3D object that we can interact with like this:
We will also add multiple objects and be able to cycle through. In order to use Scene Kit in your project, start by adding these implementations to build.gradle files.
Note that in the project I have used viewBinding feature of Kotlin in order to skip boilerplate view initialization codes. If you want to use viewBinding, you should add this little code in your app-level build.gradle.
After syncing gradle files, we are ready to use Scene Kit in our project. Keep in mind that our purpose is solely to display 3D objects that user can interact with. But Scene Kit has much more deeper abilities that is provided for us. If you are actually looking for something different or want to discover all abilities, follow the link. Else, let’s continue with a custom scene view.
Simple purpose of this custom view is just to load automatically our first object into the view when it is done initializing. Of course you can skip this part if you don’t need this purpose. Bear in mind that you should use default SceneView and load manually instead. You can still find the code for loading objects in this code snippet.
Well we cannot display anything actually before adding our object files in our projects. You will need to obtain object files elsewhere as object models I have, are not my creation. You can find public objects with ‘gltf object’ queries in search engines. Once you have your object, head to your project file and create ‘assets’ file under ‘../src/main/’ and place your object file here. In my case:
In the surfaceCreated method, loadScene(), loadSpecularEnvTexture() and loadDiffuseEnvTexture() methods are used to load our object. Once the view surface is created, our first object will be loaded into it. Now head to the xml for your 3D objects to display, in this guide case, activity_main.xml. In activity_main.xml, create the view we just created. I have also added simple arrows to navigate between models.
Now we are all set for our object to be displayed once our app is launched. Let’s add a few other objects and navigate between. In MainActivity:
In onCreate(), we are making a simple next/previous logic to change our objects. And we are storing our objects’ file paths as strings in separate lists that we created hardcoded. You may want to tinker to make it dynamic but I wanted to keep it simple for the guide. We used ‘selectedId’ to keep track of current object being displayed.
And that’s all for SceneView implementation for 3D object views!
Now no time to waste, let’s continue with adding Audio Kit.
Head back to the app-level build.gradle and add Audio Kit implementation.
As we already added necessary repository while implementing Scene Kit, we won’t need to make any changes in the project-level build.gradle. So let’s go back and complete Audio Kit.
I added a simple play button to activity_main.xml.
I will use this button to play sound for current object. Afterwards, all we need to do is make these changes in our MainActivity.
After making the changes, we will be able to play sounds in our projects. I had to add online sounds available, if you want to use sounds added in your project, then you should use ‘sound’ variable I have given example of, and change ‘audioPlayItem.setOnline(1)’ to ‘audioPlayItem.setOnline(0)’. Also change ‘audioPlayItem.onlinePath’ to ‘audioPlayItem.filePath’. Then you should be able to play imported sound files too. By the way, that’s all for Audio Kit as well! We didn’t need to implement any play/pause or seekbar features as we just want to hear the sound and get done with it.
So we completed our guide for how to implement a Scene Kit 3D Scene View and Audio Kit to play sounds in our projects in Kotlin. If you have any questions or suggestions, feel free to contact me. Thanks for reading this far and I hope it was useful for you!