Overview

HUAWEI Audio Kit is a set of audio capabilities developed by Huawei. It provides you with audio playback capabilities based on the HMS ecosystem, including audio encoding and decoding capabilities at the hardware level and system bottom layer.
You can quickly build your own audio playback capabilities and audio apps based on Audio Kit.

What You Will Create

In this codelab, you will create a demo project to call Audio Kit APIs. Through the demo project, you will experience how to:

What You Will Learn

Hardware Requirements

Software Requirements

Required Knowledge

To integrate the Audio SDK, you must complete the following preparations:

  1. Configure app information in AppGallery Connect.
  2. Create an Android Studio project.
  3. Add the app package name and save the configuration file.
For details, please refer to the Audio Kit development guide.

Adding Build Dependencies and Permissions Required

  1. Configure the Maven repository address and add the gradle configuration.
    1. Go to buildscript > repositories and configure the Maven repository address for the HMS Core Audio SDK.
    2. Go to allprojects > repositories and configure the Maven repository address for the HMS Core Audio SDK.
    3. Go to buildscript > dependencies and add the gradle configuration.
    buildscript { repositories { google() jcenter() maven {url 'https://developer.huawei.com/repo/'} } dependencies { classpath 'com.android.tools.build:gradle:3.4.2' // Note: Do not place app dependencies here. // Place them in independent build.gradle files. } } allprojects { repositories { google() jcenter() maven {url 'https://developer.huawei.com/repo/'} } }
  2. Add the following dependency to the build.gradle file in the app directory:

    implementation 'com.huawei.hms:audiokit-player:{version}'
  3. Click the synchronization icon on the toolbar to synchronize the build.gradle file.

  4. Add permissions required to the app\src\main\AndroidManifest.xml file as follows:
    <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.huawei.hms.audiokitdemotest"> <!-- Need to access the network and obtain network status information--> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!-- android4.4 To operate SD card, you need to apply for the following permissions --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_MEDIA_STORAGE" /> <!-- 9.0 Adaptation --> <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <!-- Play songs to prevent CPU from sleeping. --> <uses-permission android:name="android.permission.WAKE_LOCK" /> <application android:allowBackup="false" android:debuggable="false" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>

Configuring Obfuscation Scripts

Before building the APK, configure obfuscation scripts to prevent the HMS Core SDK from being obfuscated.
The obfuscation configuration file is proguard-rules.pro for Android Studio.
Open the obfuscation configuration file of your Android Studio project and add configurations to exclude the HMS Core SDK from obfuscation.


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

Writing the Code

  1. Write code in the MainActivity.java file in ...\app\src\main\java\com\huawei\hms\audiokitdemotest to create a playback management object and play audio.
    // Initialize the SDK. public void init(final Context context) { Log.i(TAG, "init start"); new AsyncTask<Void, Void, Void>() { @Override protected Void doInBackground(Void... voids) { // Create a configuration instance, which contains playback configurations. HwAudioPlayerConfig hwAudioPlayerConfig = new HwAudioPlayerConfig(context); // Create a control instance. HwAudioManagerFactory.createHwAudioManager(hwAudioPlayerConfig, new HwAudioConfigCallBack() { // Return the control instance through callback. @Override public void onSuccess(HwAudioManager hwAudioManager) { try { Log.i(TAG, "createHwAudioManager onSuccess"); mHwAudioManager = hwAudioManager; // Obtain the playback control instance. mHwAudioPlayerManager = hwAudioManager.getPlayerManager(); mHwAudioPlayerManager.playList(getOnlinePlaylist(), 0, 0); } catch (Exception e) { Log.e(TAG, "player init fail", e); } } @Override public void onError(int errorCode) { Log.e(TAG, "init err:" + errorCode); } }); return null; } }.execute(); }
  2. Create an audio object and write necessary information.
    public List<HwAudioPlayItem> getOnlinePlaylist() { List<HwAudioPlayItem> playItemList = new ArrayList<>(); // Create an audio object audioPlayItem1 and write information about the song "chengshilvren" into the object. HwAudioPlayItem audioPlayItem1 = new HwAudioPlayItem(); audioPlayItem1.setAudioId("1000"); audioPlayItem1.setSinger("Taoge"); audioPlayItem1.setOnlinePath("https://lfmusicservice.hwcloudtest.cn:18084/HMS/audio/Taoge-chengshilvren.mp3"); audioPlayItem1.setOnline(1); audioPlayItem1.setAudioTitle("chengshilvren"); playItemList.add(audioPlayItem1); // Create an audio object audioPlayItem2 and write information about the song "dayu" into the object. HwAudioPlayItem audioPlayItem2 = new HwAudioPlayItem(); audioPlayItem2.setAudioId("1001"); audioPlayItem2.setSinger("Taoge"); audioPlayItem2.setOnlinePath("https://lfmusicservice.hwcloudtest.cn:18084/HMS/audio/Taoge-dayu.mp3"); audioPlayItem2.setOnline(1); audioPlayItem2.setAudioTitle("dayu"); playItemList.add(audioPlayItem2); // Create an audio object audioPlayItem3 and write information about the song "wangge" into the object. HwAudioPlayItem audioPlayItem3 = new HwAudioPlayItem(); audioPlayItem3.setAudioId("1002"); audioPlayItem3.setSinger("Taoge"); audioPlayItem3.setOnlinePath("https://lfmusicservice.hwcloudtest.cn:18084/HMS/audio/Taoge-wangge.mp3"); audioPlayItem3.setOnline(1); audioPlayItem3.setAudioTitle("wangge"); playItemList.add(audioPlayItem3); return playItemList; }
  3. Execute audio playback in the onCreate function in the MainActivity.java file.

    public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private HwAudioManager mHwAudioManager; private HwAudioPlayerManager mHwAudioPlayerManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(MainActivity.this); }
  4. Install the app. After the app is launched, the configured song can be played.

Download and decompress the sample code.

Running the Audio Kit Sample Code

  1. Open Android Studio, go to File > Open, and select the directory where the sample code has been decompressed.
  2. Select the AudioKitDemo project and click OK.
  3. Double-click installDebug on the Gradle page to run the sample code.

Verifying the Integration

After the sample code is packaged and the app is installed, launch the app to play audio. The app will automatically scan the local audio list.

Well done. You have successfully completed this codelab and learned how to:

For more information, please read related documents.
You can download the source code used in this codelab.

Code copied