In the last issue, we introduced how to use the Huawei audio editing service achieve the separation of song accompaniment. In this issue, we will introduce how to implement the sound change effect in the social game App.
In the Werewolf game, if the user can select the voice they want to change the voice, it will not only add interest to the game speech, but also meet the needs of some users who do not want to expose their true voice.
Let's take a look at the voice change of the speech in the police session in the Werewolf game.
Speech line: I am a prophet. I checked the number 3 last night. TA is a werewolf. Give me the police badge. I will lead .
To view the demo, please visit the HMS Core forum on the Huawei Developer Alliance website:
https://developer.huawei.com/consumer/cn/forum/topic/0201682246924880578?fid=18
Huawei’s Video Editor Kit supports voice-changing audio materials according to the specified voice type (uncle, loli, female, male, monster, etc.).
Let's take a look at how to access Huawei's audio editing service to achieve the sound changing effect.
Development combat
Development preparation
1.1 Configure the Maven warehouse address
buildscript {
repositories {
google()
jcenter()
// 配置HMS Core SDK的Maven仓地址。
maven {url 'https://developer.huawei.com/repo/'}
}
dependencies {
...
// 增加agcp插件配置。
classpath 'com.huawei.agconnect:agcp:1.4.2.300'
}
}
allprojects {
repositories {
google()
jcenter()
// 配置HMS Core SDK的Maven仓地址。
maven {url 'https://developer.huawei.com/repo/'}
}
}
1.2 File header increased configuration
apply plugin: 'com.huawei.agconnect'
1.3 Application-level build.gradle configuration SDK dependency
dependencies{
implementation 'com.huawei.hms:audio-editor-ui:{version}'
}
Apply for the following permissions in the AndroidManifest.xml file
<!--震动权限-->
<uses-permission android:name="android.permission.VIBRATE" />
<!--麦克风权限-->
<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" />
<!--网络权限-->
<uses-permission android:name="android.permission.INTERNET" />
<!--网络状态权限-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!--网络状态变化权限-->
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
2. Code development
2.1 Create a custom activity interface of your application to select audio, and return the audio file path to the audio editing SDK in the following way.
// 将音频文件路径List返回给音频编辑页面
private void sendAudioToSdk() {
// 获取到的音频文件路径 filePath
String filePath = "/sdcard/AudioEdit/audio/music.aac";
ArrayList<String> audioList = new ArrayList<>();
audioList.add(filePath);
// 将音频文件路径返回给音频编辑页面
Intent intent = new Intent();
// 使用sdk提供的HAEConstant.AUDIO_PATH_LIST
intent.putExtra(HAEConstant.AUDIO_PATH_LIST, audioList);
// 使用sdk提供的HAEConstant.RESULT_CODE为结果CODE
this.setResult(HAEConstant.RESULT_CODE, intent);
finish();
}
2.2 When importing audio on the UI interface, the SDK will send an intent with an action value of com.huawei.hms.audioeditor.chooseaudio 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>
2.3 Start the audio editing page, click "Add Audio", the SDK will actively call the activity defined in 2.1. After adding audio, you can perform audio editing, special effects addition and other operations. After completion, export the edited audio .
HAEUIManager.getInstance().launchEditorActivity(this);
2.4 audio format conversion.
调用transformAudioUseDefaultPath接口进行音频格式转换,转换后的音频文件导出到默认路径。
// 音频格式转换接口
HAEAudioExpansion.getInstance().transformAudioUseDefaultPath(context,inAudioPath, audioFormat, new OnTransformCallBack() {
// 进度回调(0-100)
@Override
public void onProgress(int progress) {
}
// 转换失败
@Override
public void onFail(int errorCode) {
}
// 转换成功
@Override
public void onSuccess(String outPutPath) {
}
// 取消转换
@Override
public void onCancel() {
}
});
// 取消转换任务接口
HAEAudioExpansion.getInstance().cancelTransformAudio();
Call the transformAudio interface to convert the audio format, and export the converted audio file to the target path.
// 音频格式转换接口
HAEAudioExpansion.getInstance().transformAudio(context,inAudioPath, outAudioPath, new OnTransformCallBack(){
// 进度回调(0-100)
@Override
public void onProgress(int progress) {
}
// 转换失败
@Override
public void onFail(int errorCode) {
}
// 转换成功
@Override
public void onSuccess(String outPutPath) {
}
// 取消转换
@Override
public void onCancel() {
}
});
// 取消转换任务接口
HAEAudioExpansion.getInstance().cancelTransformAudio();
2.5 Call file interface to realize voice change function
private ChangeSoundCallback callBack = new ChangeSoundCallback() {
@Override
public void onSuccess(String outAudioPath) {
// 处理成功
}
@Override
public void onProgress(int progress) {
// 进度回调处理
}
@Override
public void onFail(int errorCode) {
// 处理失败
}
@Override
public void onCancel() {
// 取消处理
}
};
• Call the applyAudioFile interface to change the voice.
// 变声
HAEChangeVoiceFile haeChangeVoiceFile = new HAEChangeVoiceFile();
// 设置变声的类型
haeChangeVoiceFile.changeSoundTypeOfFile(SoundType.AUDIO_TYPE_SEASONED);
// 调用接口
haeChangeVoiceFile.applyAudioFile(inAudioPath, outAudioDir, outAudioName, callBack);
// 取消变声任务
haeChangeVoiceFile.cancel();
2.6 calls the streaming interface to realize the voice change processing of audio files. The final result requires the developer to set by himself.
// 变声
HAEChangeVoiceStream haeChangeVoiceStream = new HAEChangeVoiceStream();
// 设置音频的格式参数,返回设置结果res(注:当res为HAEErrorCode.SUCCESS时才可进行后续操作)
int res = haeChangeVoiceStream.setAudioFormat(BIT_DEPTH, CHANNEL_COUNT, SAMPLE_RATE);
//设置变声类型,返回设置结果changeRes(注:当res为HAEErrorCode.SUCCESS时才可进行后续操作)
int changeRes = haeChangeVoiceStream.changeSoundType(SoundType.AUDIO_TYPE_SEASONED);
// 对pcm数据(buffer)进行变声操作,返回变声后的pcm数据(resultByte)
while(buffer!=null){
byte[] resultByte = haeChangeVoiceStream.applyPcmData(buffer);
}
// 变声结束后释放资源
haeChangeVoiceStream.release();
Past review:
Huawei audio editing service brings you one-click accompaniment separation!
If you want to know more details, please refer to:
Huawei Developer Alliance Audio Editing Service Official Website
Obtain the development audio editing service guide document
Obtain development guidance documents:
Android SDK integration document
Quick Application SDK Integration Document
Visit Huawei Developer Alliance official website
Obtain the development guide document
Huawei mobile service open source warehouse address: GitHub 、 Gitee
and learn about the latest technical information of HMS Core for the first time~
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。