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 for the Audio SDK.
    The procedure for configuring the Maven repository address in Android Studio is different for Gradle plugin earlier than 7.0, Gradle plugin 7.0, and Gradle plugin 7.1 or later. Click a relevant link below to find the configuration procedure for the specific Gradle plugin version.
    Gradle plugin earlier than 7.0 Gradle plugin 7.0 Gradle plugin 7.1 or later
    • Gradle plugin earlier than 7.0
      i.  Open the project-level build.gradle file in your Android Studio project.


      ii.  Add the Maven repository.

        • Go to buildscript > repositories and configure the Maven repository address for the HMS Core Audio SDK
        • Go to allprojects > repositories and configure the Maven repository address for the HMS Core Audio SDK.
        buildscript { 
            repositories { 
                google() 
                jcenter() 
                // Configure the Maven repository address for the Audio SDK. 
                maven {url 'https://developer.huawei.com/repo/'} 
            } 
        } 
        allprojects { 
            repositories { 
                google() 
                jcenter() 
                // Configure the Maven repository address for the Audio SDK. 
                maven {url 'https://developer.huawei.com/repo/'} 
            } 
        }
        
    • Gradle plugin 7.0
      i.  Open the build.gradle file in the root directory of your Android Studio project.


      ii.  Go to buildscript > repositories and configure the Maven repository address for the Audio SDK.

      buildscript { 
          repositories { 
              google() 
              jcenter() 
              // Configure the Maven repository address for the Audio SDK. 
              maven {url 'https://developer.huawei.com/repo/'} 
          } 
      }
      

      iii. Open the project-level settings.gradle file and configure the Maven repository address for the Audio SDK.

      dependencyResolutionManagement { 
          ... 
          repositories { 
              google() 
              jcenter()  
              // Configure the Maven repository address for the Audio SDK. 
              maven {url 'https://developer.huawei.com/repo/'} 
          } 
      }
      
    • Gradle plugin 7.1 or later
      i.  Open the project-level settings.gradle file in your Android Studio project.


      ii.  Configure the Maven repository address for the Audio SDK.

      pluginManagement { 
          repositories { 
              gradlePluginPortal() 
              google() 
              mavenCentral() 
              // Configure the Maven repository address for the Audio SDK. 
              maven { url 'https://developer.huawei.com/repo/' } 
          } 
      } 
      dependencyResolutionManagement { 
          ... 
          repositories { 
              google() 
              mavenCentral() 
              // Configure the Maven repository address for the Audio SDK. 
              maven { url 'https://developer.huawei.com/repo/' } 
          } 
      }
      
  2. Add the following dependency to thebuild.gradlee file in theappp directory:

    implementation 'com.huawei.hms:audiokit-player:{version}'
  3. Click the synchronization icon on the toolbar to synchronize thebuild.gradlee 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://developer.huawei.com/config/file/HMSCore/AudioKit/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://developer.huawei.com/config/file/HMSCore/AudioKit/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://developer.huawei.com/config/file/HMSCore/AudioKit/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