1

In the fields of music creation, audio and video editing, and games, it is more and more important to bring users an immersive audio experience. How do developers create 3D surround sound effects in the app? Huawei Audio Editing Service version 6.2.0 brings dynamic spatial rendering, which can render audio elements such as human voices and musical instruments to a specified three-dimensional spatial orientation. It supports both static and dynamic rendering modes to further enhance the application Sound experience. Developers can click to view the following Demo demonstration to understand the integration effect and get started with experimental features.

Development actual

1. Development Preparation

The developer prepares the music material in advance, and the MP3 format is the best. For other audio formats, please refer to the "2.4" step for conversion, and for video formats, please refer to the "2.5" step for audio extraction.

Configure Maven warehouse address 1.1 project-level build.gradle:

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 Increase the file header configuration :

apply plugin: 'com.huawei.agconnect'

1.3 Application-level build.gradle configuration SDK depends on :

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

1.4 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 an application-defined activity interface to select audio and return the audio file path to the audio editing SDK :

// 将音频文件路径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 step "2.1". After adding the audio, you can perform audio editing, special effects addition and other operations. After completion, export and edit audio .

HAEUIManager.getInstance().launchEditorActivity(this);

2.4. If the audio material is not in MP3 format, this step can complete the audio format conversion :

Call the transformAudioUseDefaultPath interface to convert the audio format, and export the converted audio file to the default path.

// 音频格式转换接口
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 If the material is in a video format, you can call the extractAudio interface to extract audio, extract the audio file from the video and export it to the specified directory :

// outAudioDir提取出的音频保存的文件夹路径,非必填
// outAudioName提取出的音频名称,不带后缀,非必填
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.");
    }
    });
// 取消音频提取任务接口
HAEAudioExpansion.getInstance().cancelExtractAudio();

2.6 calls getInstruments and startSeparationTasks interface to extract accompaniment .

// 获取提取伴奏类型ID,后面将此ID传给接口
HAEAudioSeparationFile haeAudioSeparationFile = new HAEAudioSeparationFile();
haeAudioSeparationFile.getInstruments(new SeparationCloudCallBack<List<SeparationBean>>() {
    @Override
public void onFinish(List<SeparationBean> response) {
// 返回的数据,包括伴奏的类型ID
}
    @Override
    public void onError(int errorCode) {
        // 失败返回
}
});
// 设置要提取的伴奏参数
List instruments = new ArrayList<>();
instruments.add(“伴奏id”);
haeAudioSeparationFile.setInstruments(instruments);
// 开始进行伴奏分离
haeAudioSeparationFile.startSeparationTasks(inAudioPath, outAudioDir, outAudioName, new AudioSeparationCallBack() {
    @Override
    public void onResult(SeparationBean separationBean) { }
    @Override
    public void onFinish(List<SeparationBean> separationBeans) {}
    @Override
    public void onFail(int errorCode) {}
    @Override
    public void onCancel() {}
});
// 取消分离任务
haeAudioSeparationFile.cancel();

2.7 Call the applyAudioFile interface to perform spatial orientation rendering .

// 空间方位渲染
// 固定摆位
HAESpaceRenderFile haeSpaceRenderFile = new HAESpaceRenderFile(SpaceRenderMode.POSITION);
haeSpaceRenderFile.setSpacePositionParams(
                            new SpaceRenderPositionParams(x, y, z));
// 动态渲染
HAESpaceRenderFile haeSpaceRenderFile = new HAESpaceRenderFile(SpaceRenderMode.ROTATION);
haeSpaceRenderFile.setRotationParams( new SpaceRenderRotationParams(
                                    x, y, z, surroundTime, surroundDirection));
// 扩展
HAESpaceRenderFile haeSpaceRenderFile = new HAESpaceRenderFile(SpaceRenderMode.EXTENSION);
haeSpaceRenderFile.setExtensionParams(new SpaceRenderExtensionParams(radiusVal, angledVal));
// 调用接口
haeSpaceRenderFile.applyAudioFile(inAudioPath, outAudioDir, outAudioName, callBack);
// 取消空间方位渲染
haeSpaceRenderFile.cancel();

After completing the above steps, you can get the corresponding spatial dynamic rendering effect, and easily realize the 2D to 3D sound effect in the application! This function can also be applied to corporate meetings and sports rehabilitation, such as immersive display of products at exhibitions, as a sense of direction for the visually impaired, and providing convenience for daily life. Developers can choose to use according to the actual needs of their applications. For more details, please refer to:
Huawei Developer Alliance Audio Editing Service Official Website ; Get the integrated audio editing service guide document .

for more details>>

Visit Huawei Developer Alliance official website
Obtain 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~


HarmonyOS_SDK
596 声望11.7k 粉丝

HarmonyOS SDK通过将HarmonyOS系统级能力对外开放,支撑开发者高效打造更纯净、更智能、更精致、更易用的鸿蒙原生应用,和开发者共同成长。