Starting from API version 23, system sound effect playback is supported.
SoundPlayer provides system sound effect playback functionality, which is suitable for camera prompt sounds, such as playing prompt sounds when starting to take a photo, starting to record a video, or ending a video recording.
The following table describes the supported sound effect types. Specific types like systemSoundManager.SystemSoundType.PHOTO_SHUTTER can be used as input parameters for the load, play, or unload methods.
| Sound Effect Type | Value | Description |
|---|---|---|
| PHOTO_SHUTTER | 0 | Camera shutter sound. |
| VIDEO_RECORDING_BEGIN | 1 | Video recording start sound. |
| VIDEO_RECORDING_END | 2 | Video recording end sound. |
The examples in the following steps are code snippets. You can click the link at the bottom right of each to get the complete sample.
Before calling any of the SystemSoundPlayer APIs, you need to create an instance through createSystemSoundPlayer.
import { systemSoundManager } from '@kit.AudioKit';
// ...
// SystemSoundPlayer object
let systemSoundPlayer: systemSoundManager.SystemSoundPlayer | null = null;
// ...
systemSoundManager.createSystemSoundPlayer().then((systemSoundPlayerInstance) => {
console.info('Succeeded in creating the system sound player.');
systemSoundPlayer = systemSoundPlayerInstance;
}).catch((err: BusinessError) => {
console.error(`Failed to create the system sound player. Code: ${err.code}, message: ${err.message}`);
}); Call the load API to load the sound effect resource of a specified type.
import { systemSoundManager } from '@kit.AudioKit';
// ...
// Sound effect type
let systemSoundType: systemSoundManager.SystemSoundType = systemSoundManager.SystemSoundType.PHOTO_SHUTTER;
// ...
systemSoundPlayer?.load(systemSoundType).then(() => {
console.info('Succeeded in calling the load method.');
}).catch((err: BusinessError) => {
console.error(`Failed to call the load method. Code: ${err.code}, message: ${err.message}`);
}); Call the play API to play the loaded sound effect resource.
import { systemSoundManager } from '@kit.AudioKit';
// ...
// Sound effect type
let systemSoundType: systemSoundManager.SystemSoundType = systemSoundManager.SystemSoundType.PHOTO_SHUTTER;
// ...
systemSoundPlayer?.play(systemSoundType).then(() => {
console.info('Succeeded in calling the play method.');
}).catch((err: BusinessError) => {
console.error(`Failed to call the play method. Code: ${err.code}, message: ${err.message}`);
}); Call the unload API to unload the sound effect resource.
import { systemSoundManager } from '@kit.AudioKit';
// ...
// Sound effect type
let systemSoundType: systemSoundManager.SystemSoundType = systemSoundManager.SystemSoundType.PHOTO_SHUTTER;
// ...
systemSoundPlayer?.unload(systemSoundType).then(() => {
console.info('Succeeded in calling the unload method.');
}).catch((err: BusinessError) => {
console.error(`Failed to call the unload method. Code: ${err.code}, message: ${err.message}`);
}); Call the release API to release the system sound effect player.
systemSoundPlayer?.release().then(() => {
console.info('Succeeded in calling the release method.');
}).catch((err: BusinessError) => {
console.error(`Failed to call the release method. Code: ${err.code}, message: ${err.message}`);
});