[Huawei Developers] John Wo:
Unlike text or video, when users consume audio content, they can also do something else while they're listening. This is why users tend to choose audio, rather than text or video, when commuting or doing housework.

This makes audio playback a valuable addition for many apps. For example, fitness and health apps are more engaging when they have the ability to play music or audiobooks, while education apps are more effective when they provide useful audio courses, and ringtone apps need to be able to play a variety of ringtones.

So then, how do you build audio capabilities for your app?

The answer is, by using HUAWEI Audio Kit.

It provides you with a range of capabilities, including audio encoding and decoding at both the hardware level and system bottom layer.

Audio Kit provides the following functions:

l Play audio: apps can decode and play high-resolution audio files of up to 384 kHz/24 bit.

l Control playback: users can play, pause, play previous, play next, stop, and drag the progress bar.

l Adjust volume: users can increase or decrease the volume.

l Manage playlists: gives users the ability to view, save, and delete playlists, as well as add songs to a playlist.

l Manage play modes: apps can provide sequential playback, repeat a playlist, repeat a song, and shuffle songs).

l Users can save their playback progress and start from where they left off.

l Apps can cache and encrypt audio content.

Demo:

You can find the demo source code on GitHub.

Development Practice

1. Integrate the HMS Core Audio SDK

1.1 Configure the Maven Repository Address for the Audio SDK

Step 1 Open the build.gradle file in the root directory of your Android Studio project.

Step 2 Configure the Maven repository address and add the gradle configuration.

l Go to allprojects > repositories and configure the Maven repository address for the Audio SDK.

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

l Go to buildscript > dependencies and add the gradle configuration.

<p style="line-height: 1.5em;">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 your app dependencies here; you need to put them
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven {url 'https://developer.huawei.com/repo/'}
}
}

</p>

1.2 Add Build Dependencies

Step 1 Open the build.gradle file in the app directory.

Step 2 Add build dependencies in the dependencies section.

<p style="line-height: 1.5em;">dependencies {
implementation 'com.huawei.hms:audiokit-player:{version}'
}
</p>

1.3 Synchronize the Project

Once you have completed the configuration above, click the synchronization icon on the toolbar to synchronize the build.gradle file.

2 Configure Obfuscation Scripts

Before you build the APK, configure the obfuscation file to prevent the HMS Core SDK from being obfuscated.

The obfuscation configuration file is proguard-rules.pro for Android Studio.

Step 1 Open the obfuscation configuration file proguard-rules.pro in the app directory.

Step 2 Remove the HMS Core SDK from obfuscation.

<p style="line-height: 1.5em;">-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.**{*;}
</p>

Step 3 If you are using AndResGuard, add its trustlist to the obfuscation configuration file.

<p style="line-height: 1.5em;">"R.string.hms*",
"R.string.connect_server_fail_prompt_toast",
"R.string.getting_message_fail_prompt_toast",
"R.string.no_available_network_prompt_toast",
"R.string.third_app_*",
"R.string.upsdk_*",
"R.layout.hms*",
"R.layout.upsdk_*",
"R.drawable.upsdk*",
"R.color.upsdk*",
"R.dimen.upsdk*",
"R.style.upsdk*",
"R.string.agc*"
</p>

3 Adding Permissions

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

<p style="line-height: 1.5em;">// 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" />
</p>

4 Developing Your App

Step 1 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.

<p style="line-height: 1.5em;">private HwAudioPlayerManager mHwAudioPlayerManager;
private HwAudioConfigManager mHwAudioConfigManager;
private HwAudioQueueManager mHwAudioQueueManager;
public void createHwAudioManager() {
// Create a configuration instance, including various playback-related configurations.
HwAudioPlayerConfig hwAudioPlayerConfig = new HwAudioPlayerConfig(MainActivity.this);
// 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");
// Obtain the playback control instance.
mHwAudioPlayerManager = hwAudioManager.getPlayerManager();
// Obtain the configuration control instance.
mHwAudioConfigManager = hwAudioManager.getConfigManager();
// Obtain the queue control instance.
mHwAudioQueueManager = hwAudioManager.getQueueManager();
} catch (Exception e) {
Log.i(TAG, "player init fail");
}
}
@Override
public void onError(int errorCode) {
Log.w(TAG, "init err:" + errorCode);
}
});
}
</p>

Step 2 Create a playlist and play songs.

<p style="line-height: 1.5em;">public void play() {
if (mHwAudioPlayerManager != null) {
// Create a playlist.
String path = "https://lfmusicservice.hwcloudtest.cn:18084/HMS/audio/Taoge-chengshilvren.mp3";
// Create an audio object and write audio information into the object.
HwAudioPlayItem item = new HwAudioPlayItem();
// Set the audio title.
item.setAudioTitle("Playing input song");
// Set the audio ID.
item.setAudioId(String.valueOf(path.hashCode()));
// Set whether audio is online.
item.setOnline(1);
// Set the online audio URL.
item.setOnlinePath(path);
List<HwAudioPlayItem> playItemList = new ArrayList<>();
playItemList.add(item);
// Play songs.
mHwAudioPlayerManager.playList(playItemList, 0, 0);
}
}
</p>

Step 3 Use instances. The following are examples. For more details, see Audio Kit's Management APIs.

l Clear the playback cache.

<p style="line-height: 1.5em;">public void clearPlayCache() {
if (mHwAudioConfigManager != null) {
mHwAudioConfigManager.clearPlayCache();
}
}

l Check whether the current playback queue is empty.

public boolean isQueueEmpty() {
if (mHwAudioQueueManager != null) {
return mHwAudioQueueManager.isQueueEmpty();
}
return false;
}
</p>

And that's it! You've equipped your app with audio playback capabilities.