Short videos are the rage, BGM is everywhere, and users want personalized audio wherever they can show it. As a developer, we need to constantly explore and cater to users’ preferences, and audio editing function becomes a necessity for users to edit personal information, create content, share life and other scenarios.

Huawei Audio editing services (Audio Editor kit) is a set of huawei to developers worldwide Audio processing ability, has based edit Audio, Audio extraction, add Audio effect, current dual mic noise reduction, and other functions, and support for multiple voice and four kinds of format conversion, can be widely used in the creation of music, podcasts, games, etc. Let’s see how to integrate with Huawei Audio Editor Kit.

Effect of the Demo

The development of preparation

Set Maven location in build. Gradle

Buildscript {repositories {Google () jCenter () // Set Maven repository address for HMS Core SDK. maven {url 'https://developer.huawei.com/repo/'} } dependencies { ... // Add agcp plug-in configuration. Classpath 'com. Huawei. Agconnect: agcp: 1.4.2.300'}} allprojects {repositories {Google jcenter () () / / configure HMS Core The Maven repository address of the SDK. maven {url 'https://developer.huawei.com/repo/'} } }Copy the code

2. Add configuration to file header

apply plugin: 'com.huawei.agconnect'
Copy the code

3. Configure SDK dependencies in application-level build.gradle

dependencies{
    implementation 'com.huawei.hms:audio-editor-ui:{version}'
}
Copy the code

4. Apply for the following permissions in the AndroidManifest.xml file

<! Shaking -- permissions - > < USES - permission android: name = "android. Permission. VIBRATE" / > <! - the microphone permissions - > < USES - permission android: name = "android. Permission. RECORD_AUDIO" / > <! <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <! <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <! - INTERNET access - > < USES - permission android: name = "android. Permission. INTERNET" / > <! - network state authority - > < USES - permission android: name = ". Android. Permission ACCESS_NETWORK_STATE "/ >Copy the code

Code development

1, create your application custom activity interface, used to select the audio, and return the audio file path to the audio editing SDK as follows:

Private void sendAudioToSdk() {// obtained audio filePath filePath String filePath = "/sdcard/AudioEdit/audio/music.aac"; ArrayList<String> audioList = new ArrayList<>(); audioList.add(filePath); // Return the audio file path to the audio edit page. Intent Intent = new Intent(); // Use HAEConstant.AUDIO_PATH_LIST Intent. putExtra(HAEConstant.AUDIO_PATH_LIST, audioList); RESULT_CODE this. SetResult (HAEConstant.RESULT_CODE, intent); // Use the SDK's HAEConstant. finish(); }Copy the code

2, the UI interface to import audio, the SDK will send a action value of com. Huawei. HMS. Audioeditor. Chooseaudio intent to jump to the activity. Therefore, the registration form in the activity “AndroidManifest.xml” is as follows.

<activity android:name="Activity ">
<intent-filter>
<action android:name="com.huawei.hms.audioeditor.chooseaudio"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
Copy the code

3. Start the audio editing page, click “Add Audio”, SDK will actively call the activity defined in 2.1. After adding the audio, you can edit the audio, add special effects and other operations, and export the edited audio after completion.

HAEUIManager.getInstance().launchEditorActivity(this);
Copy the code

4. Audio format conversion. Call transformAudioUseDefaultPath interface in audio format conversion, the converted audio file to the default path is deduced.

/ / audio format conversion interface HAEAudioExpansion getInstance () transformAudioUseDefaultPath (context, inAudioPath audioFormat, New OnTransformCallBack() {// Progress callback (0-100) @override public void onProgress(int progress) {} // Transformation failed @override public Void onFail(int errorCode) {} // Conversion succeeded @override public void onSuccess(String outPutPath) {} // Unconversion @override public void onCancel() { } }); / / cancel the transformation interface HAEAudioExpansion getInstance () cancelTransformAudio ();Copy the code

The transformAudio interface is called to convert the audio format, and the converted audio file is exported to the target path.

/ / audio format conversion interface HAEAudioExpansion getInstance () transformAudio (context, inAudioPath outAudioPath, New OnTransformCallBack(){// Progress callback (0-100) @override public void onProgress(int progress) {} // Transformation failed @override public Void onFail(int errorCode) {} // Conversion succeeded @override public void onSuccess(String outPutPath) {} // Unconversion @override public void onCancel() { } }); / / cancel the transformation interface HAEAudioExpansion getInstance () cancelTransformAudio ();Copy the code

5. Call extractAudio interface for audio extraction, extractAudio files from the video and export them to the specified directory.

// outAudioName Specifies the name of the audio file to be saved. // outAudioName specifies the name of the audio file to be saved. The mandatory HAEAudioExpansion. GetInstance (). ExtractAudio (context, inVideoPath outAudioDir, outAudioName,new AudioExtractCallBack() { @Override public void onSuccess(String audioPath) { Log.d(TAG, "ExtractAudio onSuccess : " + audioPath); } @Override public void onProgress(int progress) { Log.d(TAG, "ExtractAudio onProgress : " + progress); } @Override public void onFail(int errCode) { Log.i(TAG, "ExtractAudio onFail : " + errCode); } @Override public void onCancel() { Log.d(TAG, "ExtractAudio onCancel."); }}); / / cancel audio extraction task interface HAEAudioExpansion. GetInstance () cancelExtractAudio ();Copy the code

Learn more

Visit the official website of Huawei Audio Editing Service

Obtain the Huawei audio editing service development guide document

Huawei audio editing service open source warehouse address: GitHub, Gitee

Huawei HMS Core official forum

To solve integration problems, go to Stack Overflow

Click the attention on the right side of the picture in the upper right corner to learn about the latest HMS Core technology