harmony 鸿蒙Using AudioHaptic for Audio-Haptic Playback
Using AudioHaptic for Audio-Haptic Playback
AudioHaptic11+ provides APIs for audio-haptic playback and management. It applies to scenarios where haptic feedback needs to be initiated synchronously during audio playback, for example, when there are incoming calls or messages or users are typing.
Development Guidelines
The entire process of audio-haptic development involves management of audio and haptic sources, configuration of an audio latency mode and audio stream usage, and creation and management of an audio-haptic player. This topic uses the process of one-time audio-haptic playback as an example to describe how to use AudioHaptic. Before the development, read AudioHaptic for better understanding.
Requesting Permissions
If the audio-haptic player needs to trigger vibration, check whether the application has the permission ohos.permission.VIBRATE.
How to Develop
- Obtain an AudioHapticManager instance, and register the audio and haptic sources. For details about the sources supported, see AudioHapticManager.
import { audio, audioHaptic } from '@kit.AudioKit';
import { BusinessError } from '@kit.BasicServicesKit';
let audioHapticManagerInstance: audioHaptic.AudioHapticManager = audioHaptic.getAudioHapticManager();
let audioUri = 'data/audioTest.wav'; // Change it to the URI of the target audio source.
let hapticUri = 'data/hapticTest.json'; // Change it to the URI of the target haptic source.
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}`);
});
- Set the parameters of an audio-haptic player. For details, see 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);
- Create an AudioHapticPlayer instance.
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.`);
- Call start() to start the audio-haptic player.
audioHapticPlayer.start().then(() => {
console.info(`Promise returned to indicate that start playing successfully.`);
}).catch ((err: BusinessError) => {
console.error(`Failed to start playing. ${err}`);
});
- Call stop() to stop the audio-haptic player.
audioHapticPlayer.stop().then(() => {
console.info(`Promise returned to indicate that stop playing successfully.`);
}).catch ((err: BusinessError) => {
console.error(`Failed to stop playing. ${err}`);
});
- Release the AudioHapticPlayer instance.
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}`);
});
- Unregister the audio and haptic sources.
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 鸿蒙Developing Audio Call
harmony 鸿蒙Audio Effect Management
harmony 鸿蒙Global Audio Input Device Management
harmony 鸿蒙Introduction to Audio Kit
harmony 鸿蒙Audio Latency Management
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦