harmony 鸿蒙使用AudioHaptic开发音振协同播放功能

  • 2025-06-12
  • 浏览 (7)

使用AudioHaptic开发音振协同播放功能

AudioHaptic11+提供音频与振动协同播放及管理的方法,适用于需要在播放音频时同步发起振动的场景,如来电铃声随振、键盘按键反馈、消息通知反馈等。

开发指导

使用AudioHaptic播放音频并同步开启振动,涉及到音频及振动资源的管理、音频时延模式及音频流使用类型的配置、音振播放器的创建及管理等。本开发指导将以一次音振协同播放的过程为例,向开发者讲解如何使用AudioHaptic进行音振协同播放,建议配合AudioHaptic的API说明阅读。

权限申请

如果应用创建的AudioHapticPlayer需要触发振动,则需要校验应用是否拥有该权限:ohos.permission.VIBRATE

  1. 声明权限
  2. 向用户申请授权

开发步骤及注意事项

  1. 获取音振管理器实例,并注册音频及振动资源,资源支持情况可以查看AudioHapticManager
   import { audio, audioHaptic } from '@kit.AudioKit';
   import { BusinessError } from '@kit.BasicServicesKit';

   let audioHapticManagerInstance: audioHaptic.AudioHapticManager = audioHaptic.getAudioHapticManager();

   let audioUri = 'data/audioTest.wav'; // 需更改为目标音频资源的Uri。
   let hapticUri = 'data/hapticTest.json'; // 需更改为目标振动资源的Uri。
   let id = 0;

   audioHapticManagerInstance.registerSource(audioUri, hapticUri).then((value: number) => {
     console.info(`Promise returned to indicate that the source id of the registered source ${value}.`);
     id = value;
   }).catch ((err: BusinessError) => {
     console.error(`Failed to register source ${err}`);
   });
  1. 设置音振播放器参数,各参数作用可以查看AudioHapticManager
   let latencyMode: audioHaptic.AudioLatencyMode = audioHaptic.AudioLatencyMode.AUDIO_LATENCY_MODE_FAST;
   audioHapticManagerInstance.setAudioLatencyMode(id, latencyMode);

   let usage: audio.StreamUsage = audio.StreamUsage.STREAM_USAGE_NOTIFICATION;
   audioHapticManagerInstance.setStreamUsage(id, usage);
  1. 创建AudioHapticPlayer实例。
   let options: audioHaptic.AudioHapticPlayerOptions = {muteAudio: false, muteHaptics: false};
   let audioHapticPlayer: audioHaptic.AudioHapticPlayer|undefined = undefined;

   audioHapticManagerInstance.createPlayer(id, options).then((value: audioHaptic.AudioHapticPlayer) => {
     console.info(`Promise returned to indicate that the audio haptic player instance.`);
     audioHapticPlayer = value;
   }).catch ((err: BusinessError) => {
     console.error(`Failed to create player ${err}`);
   });
   console.info(`Create the audio haptic player successfully.`);
  1. 调用start()方法,开启音频播放并同步开启振动。
   audioHapticPlayer.start().then(() => {
     console.info(`Promise returned to indicate that start playing successfully.`);
   }).catch ((err: BusinessError) => {
     console.error(`Failed to start playing. ${err}`);
   });
  1. 调用stop()方法,停止音频播放并同步停止振动。
   audioHapticPlayer.stop().then(() => {
     console.info(`Promise returned to indicate that stop playing successfully.`);
   }).catch ((err: BusinessError) => {
     console.error(`Failed to stop playing. ${err}`);
   });
  1. 释放AudioHapticPlayer实例。
   audioHapticPlayer.release().then(() => {
     console.info(`Promise returned to indicate that release the audio haptic player successfully.`);
   }).catch ((err: BusinessError) => {
     console.error(`Failed to release the audio haptic player. ${err}`);
   });
  1. 将已注册的音频及振动资源移除注册。
   audioHapticManagerInstance.unregisterSource(id).then(() => {
     console.info(`Promise returned to indicate that unregister source successfully`);
   }).catch ((err: BusinessError) => {
     console.error(`Failed to unregister source ${err}`);
   });

相关实例

针对音振协同开发,有以下相关实例可供参考:

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Audio Kit(音频服务)

harmony 鸿蒙开发音频通话功能

harmony 鸿蒙音频通话开发概述

harmony 鸿蒙实现音频耳返

harmony 鸿蒙音效管理

harmony 鸿蒙管理全局音频输入设备

harmony 鸿蒙Audio Kit简介

harmony 鸿蒙音频时延管理

harmony 鸿蒙响应音频流输出设备变更

harmony 鸿蒙管理全局音频输出设备

0  赞