openharmony 鸿蒙 using-soundplayer-for-playback

2026-08-25 浏览 (1)

Using SoundPlayer for System Sound Effect Playback

System sound effect playback is supported starting from API version 23.

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.

Supported Sound Effect Types

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 TypeValueDescription
PHOTO_SHUTTER0Camera shutter sound.
VIDEO_RECORDING_BEGIN1Video recording start sound.
VIDEO_RECORDING_END2Video recording end sound.

How to Develop

The examples in each of the following steps are code snippets. You can click the link at the bottom right of the sample code to obtain the complete sample codes.

  1. 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}`);
      });
    
  2. 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}`);
      });
    
  3. 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}`);
      });
    
  4. 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}`);
      });
    
  5. 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}`);
    });
    

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 public-audio-spatialization-management

openharmony 鸿蒙 audio-session-management

openharmony 鸿蒙 using-opensl-es-for-playback

openharmony 鸿蒙 using-ohaudio-for-recording

openharmony 鸿蒙 audio-kit-intro

openharmony 鸿蒙 audio-workgroup

openharmony 鸿蒙 audio-collaborative-management-sys

openharmony 鸿蒙 audio-recording-concurrency

openharmony 鸿蒙 audio-recording-overview

openharmony 鸿蒙 audio-suite-manual-rendering

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/80dZjY22