[Huawei Developers] Freemind R:
More articles like this, you can visit HUAWEI Developer Forum and Medium.​

Introduction

In this article I would like to address the integration of the Audio Kit in an Android project to create a simple music player. The new Audio Kit is a set of audio capabilities developed by Huawei. It provides us with audio playback capabilities based on the HMS Core ecosystem, including audio encoding and decoding capabilities at the hardware level and lower layer of the system.

Steps

1.-Create App in AGC

2.-Integrate the SDK in our new Android project

3.-Integrate the dependencies of Audio Kit

4.- Update the HMS Core to the latest version.

5.- Create the user interface for our Player.

6.-Add our audio file to the raw folder

7.- Program our MainActivity

1.-Create App in AGC

Well, as in most HMS Kits we must create an App in AGC.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/429/0890052100000000429.20200702161803.05841468060200600028508710465175:50510708020831:2800:91C79CF47CF5428A0BF6B4A2F3A001FE8A047EF981D0433379E907C640187A85.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/429/0890052100000000429.20200702161803.05841468060200600028508710465175:50510708020831:2800:91C79CF47CF5428A0BF6B4A2F3A001FE8A047EF981D0433379E907C640187A85.png[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/429/0890052100000000429.20200702161832.50454282638946050675622712804214:50510708020831:2800:9CDD23F8DBE17CC21D61ADA8854321B63FFC261ABF1630951C5CC7F7849F2030.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/429/0890052100000000429.20200702161832.50454282638946050675622712804214:50510708020831:2800:9CDD23F8DBE17CC21D61ADA8854321B63FFC261ABF1630951C5CC7F7849F2030.png[/IMG]​

2.-Add the SDK in our new Android project

​

Place SHA 256 and download our Json Services, to place it at the project level in Android Studio.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/429/0890052100000000429.20200702162040.88443570193718550354836568552859:50510708020831:2800:2B5F3349296F1BCC65F393F06B27F69028F993624B00D3737B838D0F2EB441BD.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/429/0890052100000000429.20200702162040.88443570193718550354836568552859:50510708020831:2800:2B5F3349296F1BCC65F393F06B27F69028F993624B00D3737B838D0F2EB441BD.png[/IMG]​

3.-Integrate the dependencies of Audio Kit

Build.gradle(project)

Build.gradle(project)
buildscript {
ext.kotlin_version = "1.3.72"
repositories {
google()
jcenter()
maven { url 'https://developer.huawei.com/repo/' }
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.0"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.android.tools.build:gradle:3.4.2'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

allprojects {
repositories {
google()
jcenter()
maven { url 'https://developer.huawei.com/repo/' }
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}

build.gradle(app)

//Add the following dependency
implementation 'com.huawei.hms:audiokit-player:1.0.0.302'

proguard-rules.pro obfuscating the code

-ignorewarnings
-keepattributes *Annotation*
-keepattributes Exceptions
-keepattributes InnerClasses
-keepattributes Signature
-keepattributes SourceFile,LineNumberTable
-keep class com.hianalytics.android.**{*;}
-keep class com.huawei.updatesdk.**{*;}
-keep class com.huawei.hms.**{*;}

4.- Update the HMS Core to the latest version.

Depending on the Huawei device you have for testing, sometimes we do not have the latest version of the HMS core. In my case I have a Huawei Y9 EMUI 9.1.0, Android version 9. And I must download the apk with the latest version of the HMS core

I regularly download my updates from this link

https://www.******************/download-the-latest-hms-core-apk/

I have tested some versions of HMS Core and the only one that worked for me was [5.0.0.304].

5.-Create the user interface of our player

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:gravity="center">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/music"/>

<SeekBar
android:id="@+id/positionBar"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/elapsedTimeLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0:11"
android:layout_marginLeft="40dp"/>
<TextView
android:id="@+id/remainingTimeLabelTimeLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-1:11"
android:layout_marginLeft="240dp"/>

</LinearLayout>
<Button
android:id="@+id/playBtn"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/play"
android:layout_marginTop="40dp"
android:onClick="playBtnClick"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="40dp"
android:gravity="center">
<ImageView
android:layout_width="18dp"
android:layout_height="18dp"
android:src="@drawable/mute"/>
<SeekBar
android:id="@+id/volumeBar"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:progress="5"
android:max="100"/>
<ImageView
android:layout_width="26dp"
android:layout_height="26dp"
android:src="@drawable/volup"/>

</LinearLayout>

</LinearLayout>

6.-Add our audio to the RAW folder

Android Studio allows us to create a Raw Folder in a very simple way, we just have to right click on app > New > Folder > RAW, with this Android Studio will create a Folder where we will add the Audio to play.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/429/0890052100000000429.20200702162721.61349037541473810193280098730304:50510708020831:2800:C7FB06BF1F91536F05CC67E80F4AA166E3AEA3FD2443CEE8E1158091E1CD225C.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/429/0890052100000000429.20200702162721.61349037541473810193280098730304:50510708020831:2800:C7FB06BF1F91536F05CC67E80F4AA166E3AEA3FD2443CEE8E1158091E1CD225C.png[/IMG]​

drag the audio to the RAW Folder

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/429/0890052100000000429.20200702162814.84676457072631055136131421504089:50510708020831:2800:611CCC6208670746A644F7520E68747444A7DC7CEC48AB9A017CF3879B43F63D.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/429/0890052100000000429.20200702162814.84676457072631055136131421504089:50510708020831:2800:611CCC6208670746A644F7520E68747444A7DC7CEC48AB9A017CF3879B43F63D.png[/IMG]​

7.- Program our MainActivity

In this final step we will add our necessary variables, we will create the Player and we will also create a PlayList with a single song which is stored in the RAW folder.

Create our instance variables

private lateinit var mHwAudioManager : HwAudioManager
private lateinit var mHwAudioPlayerManager: HwAudioPlayerManager

We create our Player and the PlayList

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var hwAudioPlayerConfig = HwAudioPlayerConfig(applicationContext)
HwAudioManagerFactory.createHwAudioManager(hwAudioPlayerConfig, object : HwAudioConfigCallBack {

override fun onSuccess(audiomanager: HwAudioManager?) {

if (audiomanager != null) {
try {
mHwAudioManager = audiomanager

mHwAudioPlayerManager = audiomanager.playerManager

var cancion:HwAudioPlayItem = HwAudioPlayItem()
val song = Uri.parse("android.resource://com.huawei.simplemusicplayer/raw/jimihendrix").toString()
cancion.filePath = song
cancion.audioTitle = "Jimi Hendrix"
cancion.audioId = "123"
var canciones:List<HwAudioPlayItem> = listOf(cancion)

mHwAudioPlayerManager.playList(canciones,0,0)

mHwAudioPlayerManager.setVolume(100)
totalTime = mHwAudioPlayerManager.duration.toInt()

}catch (ex: Exception){

}

}
}

override fun onError(p0: Int) {
Log.d("TAG1", p0.toString())
}

})
}

Add the click behavior on the button

fun playBtnClick(v: View){
if(mHwAudioPlayerManager.isPlaying){
mHwAudioPlayerManager.pause()
playBtn.setBackgroundResource(R.drawable.play)
}else{
mHwAudioPlayerManager.play()
playBtn.setBackgroundResource(R.drawable.stop)
}
}

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/429/0890052100000000429.20200702163121.45753568780390286693280291868898:50510708020831:2800:9BC51E10FAFC0B87CD3435274B53173847D343E469ABC7B5C94BC82B28476907.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/429/0890052100000000429.20200702163121.45753568780390286693280291868898:50510708020831:2800:9BC51E10FAFC0B87CD3435274B53173847D343E469ABC7B5C94BC82B28476907.png[/IMG]​

Conclusion

With this little exercise we have managed to integrate the new Huawei Audio Kit to play a song stored locally. I hope this article is very helpful to you and see you next time