文档管理中心

使用SoundPlayer开发系统音效播放功能

从API版本23开始,支持系统音效播放。

SoundPlayer提供系统音效播放功能,适用于拍照或录像提示音,比如在开始拍照、开始录像或结束录像时播放提示音。

支持的音效类型

支持的音效类型SystemSoundType信息如下表所示。可通过systemSoundManager.SystemSoundType.PHOTO_SHUTTER等具体类型,作为loadplayunload方法的入参。

展开
播放音效类型 说明
PHOTO_SHUTTER 0 拍照音效。
VIDEO_RECORDING_BEGIN 1 视频录制开始音效。
VIDEO_RECORDING_END 2 视频录制结束音效。

开发步骤

以下各步骤示例为片段代码,可通过点击示例代码右下方的链接获取完整示例

  1. 在调用SystemSoundPlayer的接口前,需要先通过createSystemSoundPlayer创建实例。

    收起
    自动换行
    深色代码主题
    复制
    1. import { systemSoundManager } from '@kit.AudioKit';
    2. // ...
    3. // SystemSoundPlayer对象。
    4. let systemSoundPlayer: systemSoundManager.SystemSoundPlayer | null = null;
    5. // ...
    6. systemSoundManager.createSystemSoundPlayer().then((systemSoundPlayerInstance) => {
    7. console.info('Succeeded in creating the system sound player.');
    8. systemSoundPlayer = systemSoundPlayerInstance;
    9. }).catch((err: BusinessError) => {
    10. console.error(`Failed to create the system sound player. Code: ${err.code}, message: ${err.message}`);
    11. });
  2. 调用load接口,加载指定类型音效资源。

    收起
    自动换行
    深色代码主题
    复制
    1. import { systemSoundManager } from '@kit.AudioKit';
    2. // ...
    3. // 音效类型。
    4. let systemSoundType: systemSoundManager.SystemSoundType = systemSoundManager.SystemSoundType.PHOTO_SHUTTER;
    5. // ...
    6. systemSoundPlayer?.load(systemSoundType).then(() => {
    7. console.info('Succeeded in calling the load method.');
    8. }).catch((err: BusinessError) => {
    9. console.error(`Failed to call the load method. Code: ${err.code}, message: ${err.message}`);
    10. });
  3. 调用play接口,播放已加载的音效资源。

    收起
    自动换行
    深色代码主题
    复制
    1. import { systemSoundManager } from '@kit.AudioKit';
    2. // ...
    3. // 音效类型。
    4. let systemSoundType: systemSoundManager.SystemSoundType = systemSoundManager.SystemSoundType.PHOTO_SHUTTER;
    5. // ...
    6. systemSoundPlayer?.play(systemSoundType).then(() => {
    7. console.info('Succeeded in calling the play method.');
    8. }).catch((err: BusinessError) => {
    9. console.error(`Failed to call the play method. Code: ${err.code}, message: ${err.message}`);
    10. });
  4. 调用unload接口,卸载之前已加载的音效资源。

    收起
    自动换行
    深色代码主题
    复制
    1. import { systemSoundManager } from '@kit.AudioKit';
    2. // ...
    3. // 音效类型。
    4. let systemSoundType: systemSoundManager.SystemSoundType = systemSoundManager.SystemSoundType.PHOTO_SHUTTER;
    5. // ...
    6. systemSoundPlayer?.unload(systemSoundType).then(() => {
    7. console.info('Succeeded in calling the unload method.');
    8. }).catch((err: BusinessError) => {
    9. console.error(`Failed to call the unload method. Code: ${err.code}, message: ${err.message}`);
    10. });
  5. 调用release接口,释放系统音效播放器。

    收起
    自动换行
    深色代码主题
    复制
    1. systemSoundPlayer?.release().then(() => {
    2. console.info('Succeeded in calling the release method.');
    3. }).catch((err: BusinessError) => {
    4. console.error(`Failed to call the release method. Code: ${err.code}, message: ${err.message}`);
    5. });
在 指南 中进行搜索
请输入您想要搜索的关键词