鸿蒙5.0版开发:音频播放-使用AudioHaptic开发音振协同播放功能

 往期鸿蒙全套实战文章必看:


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

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

开发指导

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

权限申请

如果应用创建的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 registerd source ${value}.`);
      id = value;
    }).catch ((err: BusinessError) => {
      console.error(`Failed to register source ${err}`);
    });
  2. 设置音振播放器参数,各参数作用可以查看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);
  3. 创建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.`);
  4. 调用start()方法,开启音频播放并同步开启振动。

    audioHapticPlayer.start().then(() => {
      console.info(`Promise returned to indicate that start playing successfully.`);
    }).catch ((err: BusinessError) => {
      console.error(`Failed to start playing. ${err}`);
    });
  5. 调用stop()方法,停止音频播放并同步停止振动。

    audioHapticPlayer.stop().then(() => {
      console.info(`Promise returned to indicate that stop playing successfully.`);
    }).catch ((err: BusinessError) => {
      console.error(`Failed to stop playing. ${err}`);
    });
  6. 释放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}`);
    });
  7. 将已注册的音频及振动资源移除注册

    audioHapticManagerInstance.unregisterSource(id).then(() => {
      console.info(`Promise returned to indicate that unregister source successfully`);
    }).catch ((err: BusinessError) => {
      console.error(`Failed to unregistere source ${err}`);
    });

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值