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.
In this codelab, you will create a demo project to call Audio Kit APIs. Through the demo project, you will experience how to:
To integrate the Audio SDK, you must complete the following preparations:
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.
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/' } } }
Add the following dependency to thebuild.gradlee file in theappp directory:
implementation 'com.huawei.hms:audiokit-player:{version}'
Click the synchronization icon on the toolbar to synchronize thebuild.gradlee file.
<?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>
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.**{*;}
// 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();
}
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;
}
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);
}
Download and decompress the sample code.
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.