【Harmony】@ohos.multimedia.audioHaptic (音振协同)填坑版~7

音振协同,表示在播放声音时,可同步发起振动。可用于来电通知、消息提醒等场景

导入模块

typescript 复制代码
import { audioHaptic } from '@kit.AudioKit';audioHaptic.getAudioHapticManagergetAudioHapticManager():

AudioHapticManager获取音振管理器。系统能力: SystemCapability.Multimedia.AudioHaptic.Core

typescript 复制代码
 let audioHapticManagerInstance: audioHaptic.AudioHapticManager = audioHaptic.getAudioHapticManager();

AudioHapticManager管理音振协同功能。

在调用AudioHapticManager的接口前registerSourceregisterSource(audioUri: string, hapticUri: string): Promise注册音频和振动资源的Uri,使用Promise方式异步返回结果。

typescript 复制代码
import { BusinessError } from '@kit.BasicServicesKit';
let audioUri = 'data/audioTest.wav'; // 需更改为目标音频资源的Urilet hapticUri = 'data/hapticTest.json'; // 需更改为目标振动资源的Urilet id = 0;
audioHapticManagerInstance.registerSource(audioUri, hapticUri).then((value: number) => {  console.info(`Promise returned to indicate that the source id of the registerd source ${value}.`);  id = value;}).catch ((err: BusinessError) => {  console.error(`Failed to register source ${err}`);});

以上摘抄官网部分使用说明

官网 demo 代码如下:

typescript 复制代码
import { BusinessError } from '@kit.BasicServicesKit';
let audioUri = 'data/audioTest.wav'; // 需更改为目标音频资源的Urilet hapticUri = 'data/hapticTest.json'; // 需更改为目标振动资源的Urilet id = 0;
audioHapticManagerInstance.registerSource(audioUri, hapticUri).then((value: number) => {  console.info(`Promise returned to indicate that the source id of the registerd source ${value}.`);  id = value;}).catch ((err: BusinessError) => {  console.error(`Failed to register source ${err}`);});
let options: audioHaptic.AudioHapticPlayerOptions = {muteAudio: false, muteHaptics: false};let audioHapticPlayerInstance: audioHaptic.AudioHapticPlayer | undefined = undefined;
audioHapticManagerInstance.createPlayer(id, options).then((value: audioHaptic.AudioHapticPlayer) => {  audioHapticPlayerInstance = value;  console.info(`Create the audio haptic player successfully.`);}).catch ((err: BusinessError) => {  console.error(`Failed to create the audio haptic player. ${err}`);});

看上去也没有啥困难的。事实上也的确不难

坑位

但坑就出在那个文件路径上'data/audioTest.wav';这个是 string 类型的路径而项目一般都会把文件放到 rawfile 资源目录里,然后问题来了,怎么获取到 rawfile 里文件的路径地址了,我找了半天也没有找到,然后找了几个同事也没有发现。结果是花了两天也没有找到好的办法

突破点:

registerSource需要的是 string 类型的路径地址;那给它一个路径文件地址就可以了。于是开始了骚操作如下:

1、使用沙箱定义路径地址:

typescript 复制代码
let audioUriPath = getContext().cacheDir + '/audioTest.mp3';let hapticUriPath = getContext().cacheDir + '/hapticTest.json';

这样就有了地址了

2、使用resourceManager 调用getRawFileContentSync读取内容

typescript 复制代码
let audioUriConte = getContext().resourceManager.getRawFileContentSync('audioTest.mp3');let hapticUriConte = getContext().resourceManager.getRawFileContentSync('hapticTest.json'); 

3、然后使用文件写入到定义的文件路径地址里

typescript 复制代码
FileUtils.writeFileOnlyOne(audioUriPath, audioUriConte);FileUtils.writeFileOnlyOne(hapticUriPath, hapticUriConte);

附一份

typescript 复制代码
FileUlteimport { fileIo as fs } from '@kit.CoreFileKit';

const TAG='[FileUtils]'

export namespace FileUtils{  

export function writeFileOnlyOne(filePath: string, content: Uint8Array) { 

try {      
let res=fs.accessSync(filePath)      

if(res){        
console.debug(TAG,'已存在 audioUriPath='+filePath)        
return          }       
 } catch (e) {          
console.debug(TAG,JSON.stringify(e))              
}             

 console.debug(TAG,'writeFile called!-start writing')                  
      writeFile(filePath,content)  }             
export function writeFile(filePath: string, content: Uint8Array){        
       
       const file: fs.File = fs.openSync(filePath                 , fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);               
       fs.writeSync(file.fd, content.buffer);              
        fs.closeSync(file);             
        }
    }

其他就回到官方使用说明里走逻辑流程即可===================END

如果对老师您有点用,可否用发财的金手指点个赞👍

相关推荐
海兰29 分钟前
【 Python 量化交易】第3章:金融基础概念
开发语言·python·金融
喵本喵叁肆30 分钟前
06-M6-部门过滤与综合研判-从问答机到研判助手
前端·javascript·jquery
一木 之林38 分钟前
三.C++ 内存管理(进阶)(二)
开发语言·arm开发·c++
吴声子夜歌40 分钟前
Java——类、对象及方法(一)
java·开发语言
阿里嘎多学长1 小时前
2026-08-29 GitHub 热点项目精选
开发语言·程序员·github·代码托管
এ慕ོ冬℘゜1 小时前
JavaScript学习心得:从只会语法,到真正会写业务逻辑
开发语言·javascript·ecmascript
2601_966949651 小时前
五档盘口数据对量化交易有什么价值?从信号判断到策略风险的完整分析
开发语言·人工智能·python·数据分析·量化·股票数据·quantdash
小吴学不废Java2 小时前
Python 基础语法
开发语言·python
এ慕ོ冬℘゜2 小时前
jQuery 核心方法
前端·javascript·jquery