Last time we talked about how to use Huawei audio editing service to separate song accompaniment, this time we will show you how to implement sound change effect in social game App.

In werewolf killing games, if users can choose the voice they want to change the voice processing, not only to add fun to the game speech, but also to meet the needs of some users who do not want to expose their real voice. Let’s take a look at the voice change of the police officer in the werewolf killing game.

Speech lines: I am a prophet, last night tested 3, TA is a werewolf, police badge to me, I will lead the team.

To view the Demo, please visit the HMS Core forum on huawei Developer Alliance website:

Developer.huawei.com/consumer/cn…

Huawei Video Editor Kit supports audio editing based on the specified voice type (uncle, girl, female, male, monster).

Here’s how to access Huawei audio editing service to achieve the sound change effect.

The development of actual combat

The development of preparation

Configure Maven storehouse addresses in build.gradle at project level

Buildscript {repositories {Google () jCenter () // Configure Maven repository 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 Maven repository address for SDK. maven {url 'https://developer.huawei.com/repo/'} } }Copy the code

1.2 Added configuration of file headers

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

1.3 Configure SDK dependencies in build.gradle

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

1. 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 "/ > <! - network status change permissions - > < USES - permission android: name = "android. Permission. CHANGE_NETWORK_STATE" / >Copy the code

2. Code development

2.1 Create your application’s custom activity interface to select audio and return the audio file path to the audio editing SDK in the following way.

Private void sendAudioToSdk() {// obtained audio filePath filePath String filePath = "/sdcard/AudioEdit/audio/music.aac"; ArrayList<String> audioList = new ArrayList<>(); audioList.add(filePath); Intent Intent = new Intent(); PutExtra (HAEConstant.AUDIO_PATH_LIST, audioList); // Use HAEConstant.AUDIO_PATH_LIST intent. // Use the SDK HAEConstant.RESULT_CODE as the result of the CODE this.setResult(HAEConstant. finish(); }Copy the code

2.2 audio UI interface to import, 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

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

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

2.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) { Void onFail(int errorCode) {} @override public void onSuccess(String outPutPath) {} @override public void onSuccess(String outPutPath) { void onCancel() { } }); / / cancel the transformation interface HAEAudioExpansion getInstance () cancelTransformAudio ();Copy the code

The transformAudio interface is called for audio format conversion, and the converted audio file is exported to the destination path.

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

2.5 Invoke file interface to realize sound change function

Create a file interface callback. 1. private ChangeSoundCallback callBack = new ChangeSoundCallback() { 2. @Override 3. public void onSuccess(String Public void onProgress(int progress) {8. // Progress callback 9.} 10 Public void onFail(int errorCode) {12. // Process failed 13.} 14. 18.}; • Call applyAudioFile interface for voice change. HAEChangeVoiceFile = new HAEChangeVoiceFile(); 3. / / set voice changed type 4. HaeChangeVoiceFile. ChangeSoundTypeOfFile (SoundType. AUDIO_TYPE_SEASONED); 5. / / call interface 6. HaeChangeVoiceFile. ApplyAudioFile (inAudioPath outAudioDir, outAudioName, callBack); 7. 8. / / cancel his task haeChangeVoiceFile. Cancel ();Copy the code

2.6 Call the streaming interface to realize the sound change processing of audio files, and the final result needs to be set by the developer.

HAEChangeVoiceStream = new HAEChangeVoiceStream(); 3. // Set the audio format parameters, return the setting result res (note: When res HAEErrorCode. SUCCESS can be only subsequent operations) 4. Int res. = haeChangeVoiceStream setAudioFormat (BIT_DEPTH CHANNEL_COUNT, SAMPLE_RATE); 5. // Set the sound type and return the result changeRes (note: Perform subsequent operations only when res is haeErrorCode.success.) 6. Int changeRes = haeChangeVoiceStream.changeSoundType(SoundType.AUDIO_TYPE_SEASONED); 7. // Change PCM data (buffer), return PCM data after the change (resultByte) 8. =null){ 9. byte[] resultByte = haeChangeVoiceStream.applyPcmData(buffer); 10.} 11. / / his release resources after 12. HaeChangeVoiceStream. Release ();Copy the code

Previous review: Huawei Audio Editing service brings you one-click accompaniment separation!

For more details, please refer to:

Huawei’s website developer alliance audio editing service: developer.huawei.com/consumer/cn…

To obtain development audio editing services guide: developer.huawei.com/consumer/cn…

Get the development guidance document:

Android SDK integration documentation

IOS SDK integration documentation

Web SDK integration documentation

Quick application SDK integration document

Visit the official website of Huawei Developer Alliance to obtain the development guide. The open-source repositories of Huawei mobile services are GitHub and Gitee

Follow us for updates on HMS Core