HarmonyOS鸿蒙Next中audioHaptic (音振协同) 的使用
HarmonyOS鸿蒙Next中audioHaptic (音振协同) 的使用 注册audioHaptic的createPlayer时返回5400103 - I/O error.,是我的资源文件放的位置有问题吗,还是我自定义震动有问题,或者是音频有问题呢,如何使用?
audioUri和hapticUri需更改为目标音频资源的Uri,代码操作将音频和震动配置文件从rawfile送进沙箱路劲,然后再执行音频registerSource()操作,
demo:
async function resourceFile(resourcePath: string) {
let uint8Array: Uint8Array = getContext()
.createModuleContext("library")
.resourceManager
.getRawFileContentSync(resourcePath);
let fileName = resourcePath.substring(resourcePath.lastIndexOf('/') + 1);
console.log("${tag} fileName:${fileName}");
let filePath = getContext().createModuleContext("library").filesDir + '/' + fileName;
if (fileIo.accessSync(filePath)) {
fileIo.unlinkSync(filePath)
}
let file: fileIo.File = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE);
fileIo.writeSync(file.fd, uint8Array.buffer);
fileIo.closeSync(file);
console.log("${tag} filePath:${filePath}");
return filePath;
}
audioHaptic 振动可能只会有一种振动效果,声音可以并发,一次振动两次音效,如果是创建两个AudioHapticPlayer,应该是可以达到的
当前audiohaptic是振动对齐到音频的,所以是一对一的关系
这边audiohaptic是不支持并行播放音频,这边可以使用soundPool并行播放两个音频,再使用Vibrator进行振动,这样便可实现你要的效果,
soundPool:华为开发者官网
Vibrator:华为开发者官网
soundPool 并行播放请参考demo:
import audio from '@ohos.multimedia.audio';
import media from '@ohos.multimedia.media';
import fs from '@ohos.file.fs'
import { common } from '@kit.AbilityKit';
import fileIo from '@ohos.file.fs';
let soundPool: media.SoundPool;
let streamId: number = 0;
let soundId1: number = 0;
let soundId2: number = 0;
let context = getContext(this) as common.UIAbilityContext;
let resourceDir = context.resourceDir;
let audioRendererInfo: audio.AudioRendererInfo = {
usage: audio.StreamUsage.STREAM_USAGE_MUSIC,
rendererFlags: 1
}
let PlayParameters: media.PlayParameters = {
loop: 3, // 循环4次
rate: audio.AudioRendererRate.RENDER_RATE_NORMAL, // 正常倍速
leftVolume: 0.5, // range = 0.0-1.0
rightVolume: 0.5, // range = 0.0-1.0
priority: 0, // 最低优先级
}
let uri: string = "";
async function create() {
soundPool = await media.createSoundPool(2, audioRendererInfo);
loadCallback();
finishPlayCallback();
setErrorCallback();
let filePath = context.resourceDir + '/tap.mp3';
let filePath1 = context.resourceDir + '/push.mp3';
let file = await fileIo.open(filePath, fileIo.OpenMode.READ_ONLY);
let file1 = await fileIo.open(filePath1, fileIo.OpenMode.READ_ONLY);
uri = 'fd://' + (file.fd).toString()
let uri1 = 'fd://' + (file1.fd).toString()
soundId1 = await soundPool.load(uri);
soundId2 = await soundPool.load(uri1);
}
async function loadCallback() {
soundPool.on('loadComplete', (soundId_: number) => {
console.info('loadComplete, soundId: ' + soundId_);
})
}
async function finishPlayCallback() {
soundPool.on('playFinished', () => {
console.info("recive play finished message");
// 可进行下次播放
})
}
function setErrorCallback() {
soundPool.on('error', (error) => {
console.info('error happened,message is :' + error.message);
})
}
async function PlaySoundPool(index:number) {
streamId = await soundPool.play(index);
soundPool.setLoop(streamId, 2); // 播放3次
soundPool.setPriority(streamId, 1);
soundPool.setVolume(streamId, 0.5, 0.5);
}
async function release() {
soundPool.stop(streamId);
// 卸载音频资源
// await soundPool.unload(soundId);
setOffCallback();
await soundPool.release();
}
function setOffCallback() {
soundPool.off('loadComplete');
soundPool.off('playFinished');
soundPool.off('error');
}
@Entry
@Component
struct Index {
@State message: string = 'Hello World';
async aboutToAppear(): Promise<void> {
create();
}
build() {
RelativeContainer() {
Button("播放").width('33%').height('5%').onClick(async () => {
PlaySoundPool(soundId1)
PlaySoundPool(soundId2)
// setOffCallback()
// release()
})
}
.height('100%')
.width('100%')
}
}
更多关于HarmonyOS鸿蒙Next中audioHaptic (音振协同) 的使用的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中,audioHaptic
(音振协同)功能通过结合音频和振动反馈,提升用户体验。开发者可以使用AudioHapticManager
类来管理音振协同效果。首先,通过getAudioHapticManager()
获取实例,然后使用playEffect()
方法播放音振效果,传入音频文件和振动模式。振动模式可通过VibrationEffect
类定义,支持自定义频率、强度和时长。通过stopEffect()
可停止播放。此功能适用于游戏、通知等场景,增强交互沉浸感。