[Huawei Developers] John Wo:
HUAWEI Audio Kit

Article Introduction

The following article describes the easiest way to use the playback capability of the HUAWEI Audio Kit and how to read audio online, locally, and from the resources folder as well.

HUAWEI Audio Kit Introduction

HUAWEI Audio Kit is a set of audio capabilities that 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.

Supported by Android phones and tablets running Android 6.0 (EMUI 4.0) or later with HMS core vers. 5.0.0.300 or later.

Integrating the HUAWEI Audio Kit SDK

Before start using the AppGallery Auth Service, we must first integrate the HMS Core Audio SDK by following the steps below :

Step 1:

- Configure the Maven Repository Address for the HMS Core Audio SDK in the Gradle file at the project-level:

buildscript {
repositories {
google()
jcenter()
maven {url 'https://developer.huawei.com/repo/'} // to be added
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.3'
}
}
allprojects {
repositories {
google()
jcenter()
maven {url 'https://developer.huawei.com/repo/'} // to be added
}
}

- Add the build Dependencies in the Gradle file at the app-level:

implementation 'com.huawei.hms:audiokit-player:1.1.0.300'

Step 2:

- The HMS Core Audio SDK requires permissions to access the network, obtain the network status, operate SD cards, and read data from the Android media library. Declare the permissions in the Manifest file:

// Permission to access the network.
<uses-permission android:name="android.permission.INTERNET" />
// Permission to obtain the network status.
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
// Permission to write data into the SD card.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
// Permission to read data from the SD card.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
// Permission to read data from the Android media library.
<uses-permission android:name="android.permission.READ_MEDIA_STORAGE" />

Coding

The Audio Kit allow us to:

- Create an audio management instance by calling HwAudioManager;

- Manage audio playback by calling HwAudioPlayerManager;

- Manage audio queues by calling HwAudioQueueManager;

- Manage audio configurations by calling HwAudioConfigManager.

- Manage audio data by calling HwAudioPlayItem.

In the following, we'll create a HwAudioManager to manage the manage playback, and later we'll create a playlist to play online audio, local audio, and from the resources folder audio.

// Create a configuration instance, including various playback-related 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();
// get the play list of audio using and play the play list of audios.
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);
}
});

In the following, we'll see how to read audio files from different sources, and later we'll implement the previously used method getOnlinePlaylist() that returns the playlist.

The audio data class HwAudioPlayItem includes the album name, artist, whether audio is online, etc., and provides as well two methods to set the path of the audio file; setFilePath(java.lang.String filePath) to set the local path of the audio. and setOnlinePath(java.lang.String onlinePath) to set the online URL of the audio.

In order to have a 3rd method to set the audio path from the resources folder let's create our own class HwAudioPlayItemBeta that extends HwAudioPlayItem, in which we will add a method that copies the audio file from the resources folder to a cashed local folder to be able to read that file as a local file using setFilePath.

More details, you can visit https://forums.developer.huawei.com/forumPortal/en/topic/0204412320648460216