openharmony 鸿蒙 js-apis-inner-multimedia-soundPool

2025-06-12 浏览 (1)

SoundPool (Sound Pool)

The SoundPool module provides APIs for loading, unloading, playing, and stopping playing sounds, setting the volume, and setting the number of loops.

Before using these APIs, you must call media.createSoundPool to create a SoundPool instance.

NOTE

The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import { media } from '@kit.MediaKit';
import { audio } from '@kit.AudioKit';

PlayParameters

Describes the playback parameters of the sound pool.

These parameters are used to control the playback volume, number of loops, and priority.

System capability: SystemCapability.Multimedia.Media.SoundPool

NameTypeMandatoryDescription
loopnumberNoNumber of loops.
If this parameter is set to a value greater than or equal to 0, the number of times the content is actually played is the value of loop plus 1.
If this parameter is set to a value less than 0, the content is played repeatedly.
The default value is 0, indicating that the content is played only once.
ratenumberNoPlayback rate. For details, see AudioRendererRate. Default value: 0
leftVolumenumberNoVolume of the left channel. The value ranges from 0.0 to 1.0. Default value: 1.0
rightVolumenumberNoVolume of the right channel. The value ranges from 0.0 to 1.0. (Currently, the volume cannot be set separately for the left and right channels. The volume set for the left channel is used.) Default value: 1.0
prioritynumberNoPlayback priority. The value 0 means the lowest priority. A larger value indicates a higher priority. The value is an integer greater than or equal to 0. Default value: 0

ErrorType20+

Enumerates the error types (used to distinguish error stages).

System capability: SystemCapability.Multimedia.Media.SoundPool

NameValueDescription
LOAD_ERROR1An error occurred during resource loading.
PLAY_ERROR2An error occurred during resource playback.

ErrorInfo20+

Describes the error information.

System capability: SystemCapability.Multimedia.Media.SoundPool

NameTypeRead-OnlyOptionalDescription
errorCodeTNoNoError code. The type of errorCode is BusinessError.
errorTypeErrorTypeNoYesStage at which the error occurred.
soundIdnumberNoYesID of the resource where the error occurred. It can be obtained by calling load.
streamIdnumberNoYesID of the audio stream where the error occurred. It can be obtained by calling play.

SoundPool

Implements a sound pool that provides APIs for loading, unloading, playing, and stopping playing system sounds, setting the volume, and setting the number of loops. Before using these APIs, you must call createSoundPool to create a SoundPool instance.

NOTE

When using the SoundPool instance, you are advised to register the following callbacks to proactively obtain status changes:

load

load(uri: string, callback: AsyncCallback<number>): void

Loads a sound. This API uses an asynchronous callback to obtain the sound ID. The input parameter uri is a string starting with fd://, which is generated based on the file descriptor (FD) obtained. This API cannot be used to load resources in the rawfile directory. Instead, use load(fd: number, offset: number, length: number, callback: AsyncCallback<number>): void or load(fd: number, offset: number, length: number): Promise<number>.

NOTE

After the resource handle (in the form of an FD) or path description (in the form of a URI) is transferred to the AVPlayer, do not use the resource handle or path description in read or write operations, including but not limited to transferring it to multiple AVPlayers. Competition occurs when multiple AVPlayers use the same resource handle or path description to read and write files at the same time, resulting in playback errors.

System capability: SystemCapability.Multimedia.Media.SoundPool

Parameters

NameTypeMandatoryDescription
uristringYesURI of the audio file to load. Generally, the URI starts with fd://.
callbackAsyncCallback<number>YesCallback used to return the sound ID. A valid value must be greater than 0.

Error codes

For details about the error codes, see Media Error Codes.

IDError Message
5400102Operation not allowed. Return by callback.
5400103I/O error. Return by callback.
5400105Service died. Return by callback.

Example

import { fileIo } from '@kit.CoreFileKit';
import { BusinessError } from '@kit.BasicServicesKit';

// Create a SoundPool instance.
let soundPool: media.SoundPool;
let audioRendererInfo: audio.AudioRendererInfo = {
  usage: audio.StreamUsage.STREAM_USAGE_MUSIC,
  rendererFlags: 1
}
media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => {
  if (error) {
    console.error(`Failed to createSoundPool`);
    return;
  } else {
    soundPool = soundPool_;
    console.info(`Succeeded in createSoundPool`);
    let uri:string = "";
    let file: fileIo.File;
    // Obtain the URI starting with fd://.
    fileIo.open('/test_01.mp3', fileIo.OpenMode.READ_ONLY).then((file_: fileIo.File) => {
      file = file_;
      console.info("file fd: " + file.fd);
      uri = 'fd://' + (file.fd).toString();
      soundPool.load(uri, (error: BusinessError, soundId_: number) => {
        if (error) {
          console.error(`Failed to load soundPool: errCode is ${error.code}, errMessage is ${error.message}`);
        } else {
          console.info(`Succeeded in loading soundPool` + JSON.stringify(soundId_));
        }
      });
    }); // '/test_01.mp3' here is only an example. You need to pass in the actual URI.
  }
});

load

load(uri: string): Promise<number>

Loads a sound. This API uses a promise to obtain the sound ID. The input parameter uri is a starting with fd://, which is generated based on the FD obtained. This API cannot be used to load resources in the rawfile directory. Instead, use load(fd: number, offset: number, length: number, callback: AsyncCallback<number>): void or load(fd: number, offset: number, length: number): Promise<number>.

NOTE

After the resource handle (in the form of an FD) or path description (in the form of a URI) is transferred to the AVPlayer, do not use the resource handle or path description in read or write operations, including but not limited to transferring it to multiple AVPlayers. Competition occurs when multiple AVPlayers use the same resource handle or path description to read and write files at the same time, resulting in playback errors.

System capability: SystemCapability.Multimedia.Media.SoundPool

Parameters

NameTypeMandatoryDescription
uristringYesURI of the audio file to load. Generally, the URI starts with fd://.

Return value

TypeDescription
Promise<number>Promise used to return the sound ID. A valid value must be greater than 0.

Error codes

For details about the error codes, see Media Error Codes.

IDError Message
5400102Operation not allowed. Return by promise.
5400103I/O error. Return by promise.
5400105Service died. Return by promise.

Example

import { fileIo } from '@kit.CoreFileKit';
import { BusinessError } from '@kit.BasicServicesKit';

// Create a SoundPool instance.
let soundPool: media.SoundPool;
let audioRendererInfo: audio.AudioRendererInfo = {
  usage: audio.StreamUsage.STREAM_USAGE_MUSIC,
  rendererFlags: 1
}
media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => {
  if (error) {
    console.error(`Failed to createSoundPool`);
    return;
  } else {
    soundPool = soundPool_;
    console.info(`Succeeded in createSoundPool`);
    let uri:string = "";
    let soundID: number = 0;
    let file: fileIo.File;
    // Obtain the URI starting with fd://.
    fileIo.open('/test_01.mp3', fileIo.OpenMode.READ_ONLY).then((file_: fileIo.File) => {
      file = file_;
      console.info("file fd: " + file.fd);
      uri = 'fd://' + (file.fd).toString();
      soundPool.load(uri).then((soundId: number) => {
        console.info('Succeeded in loading uri');
        soundID = soundId;
      }, (err: BusinessError) => {
        console.error('Failed to load soundPool and catch error is ' + err.message);
      });
    }); // '/test_01.mp3' here is only an example. You need to pass in the actual URI.
  }
});

load

load(fd: number, offset: number, length: number, callback: AsyncCallback<number>): void

Loads a sound. This API uses an asynchronous callback to obtain the sound ID. The input parameter fd can be manually input or automatically obtained by reading the embedded resource of the application.

NOTE

After the resource handle (in the form of an FD) or path description (in the form of a URI) is transferred to the AVPlayer, do not use the resource handle or path description in read or write operations, including but not limited to transferring it to multiple AVPlayers. Competition occurs when multiple AVPlayers use the same resource handle or path description to read and write files at the same time, resulting in playback errors.

System capability: SystemCapability.Multimedia.Media.SoundPool

Parameters

NameTypeMandatoryDescription
fdnumberYesResource handle, which is obtained by calling resourceManager.getRawFd.
offsetnumberYesResource offset, which needs to be entered based on the preset resource information. An invalid value causes a failure to parse audio and video resources.
lengthnumberYesResource length, which needs to be entered based on the preset resource information. An invalid value causes a failure to parse audio and video resources.
callbackAsyncCallback<number>YesCallback used to return the sound ID. A valid value must be greater than 0.

Error codes

For details about the error codes, see Media Error Codes.

IDError Message
5400102Operation not allowed. Return by callback.
5400103I/O error. Return by callback.
5400105Service died. Return by callback.

Example 1:

import { fileIo } from '@kit.CoreFileKit';
import { BusinessError } from '@kit.BasicServicesKit';

// Create a SoundPool instance.
let soundPool: media.SoundPool;
let audioRendererInfo: audio.AudioRendererInfo = {
  usage: audio.StreamUsage.STREAM_USAGE_MUSIC,
  rendererFlags: 1
}
media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => {
  if (error) {
    console.error(`Failed to createSoundPool`);
    return;
  } else {
    soundPool = soundPool_;
    console.info(`Succeeded in createSoundPool`);
    let file: fileIo.File;
    let soundID: number = 0;
    let fileSize: number = 1; // Obtain the size through fileIo.stat().
    let uri: string = "";
    // Obtain the FD. The test_01.mp3 file is not an audio file in the rawfile directory.
    fileIo.open('/test_01.mp3', fileIo.OpenMode.READ_ONLY).then((file_: fileIo.File) => {
      file = file_;
      console.info("file fd: " + file.fd);
      uri = 'fd://' + (file.fd).toString();
      soundPool.load(file.fd, 0, fileSize, (error: BusinessError, soundId_: number) => {
        if (error) {
          console.error(`Failed to load soundPool: errCode is ${error.code}, errMessage is ${error.message}`);
        } else {
          soundID = soundId_;
          console.info('Succeeded in loading soundId:' + soundId_);
        }
      });
    }); // '/test_01.mp3' here is only an example. You need to pass in the actual URI.
  }
});

Example 2

import { media } from '@kit.MediaKit';
import { audio } from '@kit.AudioKit';
import { BusinessError } from '@kit.BasicServicesKit';

function create(context: Context) {
  // Create a SoundPool instance.
  let soundPool: media.SoundPool;
  let audioRendererInfo: audio.AudioRendererInfo = {
    usage: audio.StreamUsage.STREAM_USAGE_MUSIC,
    rendererFlags: 1
  }
  let soundID: number = 0;
  media.createSoundPool(5, audioRendererInfo, async (error: BusinessError, soundPool_: media.SoundPool) => {
    if (error) {
      console.error(`Failed to createSoundPool`);
      return;
    } else {
      soundPool = soundPool_;
      console.info(`Succeeded in createSoundPool`);
      // The test_01.mp3 file is an audio file in the rawfile directory.
      let fileDescriptor = await context.resourceManager.getRawFd('test_01.mp3');
      soundPool.load(fileDescriptor.fd, fileDescriptor.offset, fileDescriptor.length, (error: BusinessError, soundId_: number) => {
        if (error) {
          console.error(`Failed to load soundPool: errCode is ${error.code}, errMessage is ${error.message}`);
        } else {
          soundID = soundId_;
          console.info('Succeeded in loading soundId:' + soundId_);
        }
      });
    }
  });
}

load

load(fd: number, offset: number, length: number): Promise<number>

Loads a sound. This API uses a promise to obtain the sound ID. The input parameter fd can be manually input or automatically obtained by reading the embedded resource of the application.

NOTE

After the resource handle (in the form of an FD) or path description (in the form of a URI) is transferred to the AVPlayer, do not use the resource handle or path description in read or write operations, including but not limited to transferring it to multiple AVPlayers. Competition occurs when multiple AVPlayers use the same resource handle or path description to read and write files at the same time, resulting in playback errors.

System capability: SystemCapability.Multimedia.Media.SoundPool

Parameters

NameTypeMandatoryDescription
fdnumberYesResource handle, which is obtained by calling resourceManager.getRawFd.
offsetnumberYesResource offset, which needs to be entered based on the preset resource information. An invalid value causes a failure to parse audio and video resources.
lengthnumberYesResource length, which needs to be entered based on the preset resource information. An invalid value causes a failure to parse audio and video resources.

Return value

TypeDescription
Promise<number>Promise used to return the sound ID. A valid value must be greater than 0.

Error codes

For details about the error codes, see Media Error Codes.

IDError Message
5400102Operation not allowed. Return by promise.
5400103I/O error. Return by promise.
5400105Service died. Return by promise.

Example 1:

import { fileIo } from '@kit.CoreFileKit';
import { BusinessError } from '@kit.BasicServicesKit';

// Create a SoundPool instance.
let soundPool: media.SoundPool;
let audioRendererInfo: audio.AudioRendererInfo = {
  usage: audio.StreamUsage.STREAM_USAGE_MUSIC,
  rendererFlags: 1
}
media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => {
  if (error) {
    console.error(`Failed to createSoundPool`);
    return;
  } else {
    soundPool = soundPool_;
    console.info(`Succeeded in createSoundPool`);
    let file: fileIo.File;
    let soundID: number = 0;
    let fileSize: number = 1; // Obtain the size through fileIo.stat().
    let uri: string = "";
    // Obtain the FD. The test_01.mp3 file is not an audio file in the rawfile directory.
    fileIo.open('/test_01.mp3', fileIo.OpenMode.READ_ONLY).then((file_: fileIo.File) => {
      file = file_;
      console.info("file fd: " + file.fd);
      uri = 'fd://' + (file.fd).toString();
      soundPool.load(file.fd, 0, fileSize).then((soundId: number) => {
        console.info('Succeeded in loading soundpool');
        soundID = soundId;
      }, (err: BusinessError) => {
        console.error('Failed to load soundpool and catch error is ' + err.message);
      });
    });
  }
});

Example 2

import { media } from '@kit.MediaKit';
import { audio } from '@kit.AudioKit';
import { BusinessError } from '@kit.BasicServicesKit';

function create(context: Context) {
  // Create a SoundPool instance.
  let soundPool: media.SoundPool;
  let audioRendererInfo: audio.AudioRendererInfo = {
    usage: audio.StreamUsage.STREAM_USAGE_MUSIC,
    rendererFlags: 1
  }
  let soundID: number = 0;
  media.createSoundPool(5, audioRendererInfo, async (error: BusinessError, soundPool_: media.SoundPool) => {
    if (error) {
      console.error(`Failed to createSoundPool`);
      return;
    } else {
      soundPool = soundPool_;
      console.info(`Succeeded in createSoundPool`);
      // The test_01.mp3 file is an audio file in the rawfile directory.
      let fileDescriptor = await context.resourceManager.getRawFd('test_01.mp3');
      soundPool.load(fileDescriptor.fd, fileDescriptor.offset, fileDescriptor.length).then((soundId: number) => {
        console.info('Succeeded in loading soundpool');
        soundID = soundId;
      }, (err: BusinessError) => {
        console.error('Failed to load soundpool and catch error is ' + err.message);
      });
    }
  });
}

play

play(soundID: number, params: PlayParameters, callback: AsyncCallback<number>): void

Plays a sound. This API uses an asynchronous callback to obtain the audio stream ID.

System capability: SystemCapability.Multimedia.Media.SoundPool

Parameters

NameTypeMandatoryDescription
soundIDnumberYesSound ID, which is obtained by calling load().
paramsPlayParametersYesPlayback parameters.
callbackAsyncCallback<number>YesCallback used to return the audio stream ID. A valid value must be greater than 0.

Error codes

For details about the error codes, see Media Error Codes.

IDError Message
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. Return by callback.
5400102Operation not allowed. Return by callback.
5400105Service died. Return by callback.

Example

import { BusinessError } from '@kit.BasicServicesKit';

// Create a SoundPool instance.
let soundPool: media.SoundPool;
let audioRendererInfo: audio.AudioRendererInfo = {
  usage: audio.StreamUsage.STREAM_USAGE_MUSIC,
  rendererFlags: 1
}
media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => {
  if (error) {
    console.error(`Failed to createSoundPool`);
    return;
  } else {
    soundPool = soundPool_;
    console.info(`Succeeded in createSoundPool`);
    let soundID: number = 0;
    let streamID: number = 0;
    let playParameters: media.PlayParameters = {
      loop: 3, // The sound is played four times (three loops).
      rate: audio.AudioRendererRate.RENDER_RATE_NORMAL, // The sound is played at the original frequency.
      leftVolume: 0.5, // range = 0.0-1.0
      rightVolume: 0.5, // range = 0.0-1.0
      priority: 0, // The sound playback has the lowest priority.
    }
    soundPool.play(soundID, playParameters, (error: BusinessError, streamId: number) => {
      if (error) {
        console.error(`Failed to play soundpool: errCode is ${error.code}, errMessage is ${error.message}`);
      } else {
        streamID = streamId;
        console.info('Succeeded in playing soundpool, streamId:' + streamId);
      }
    });
  }
});

play

play(soundID: number, callback: AsyncCallback<number>): void

Plays a sound. This API uses an asynchronous callback to obtain the audio stream ID.

System capability: SystemCapability.Multimedia.Media.SoundPool

Parameters

NameTypeMandatoryDescription
soundIDnumberYesSound ID, which is obtained by calling load().
callbackAsyncCallback<number>YesCallback used to return the audio stream ID. A valid value must be greater than 0.

Error codes

For details about the error codes, see Media Error Codes.

IDError Message
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. Return by callback.
5400102Operation not allowed. Return by callback.
5400105Service died. Return by callback.

Example

import { BusinessError } from '@kit.BasicServicesKit';

// Create a SoundPool instance.
let soundPool: media.SoundPool;
let audioRendererInfo: audio.AudioRendererInfo = {
  usage: audio.StreamUsage.STREAM_USAGE_MUSIC,
  rendererFlags: 1
}
media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => {
  if (error) {
    console.error(`Failed to createSoundPool`);
    return;
  } else {
    soundPool = soundPool_;
    console.info(`Succeeded in createSoundPool`);
    let soundID: number = 0;
    let streamID: number = 0;
    soundPool.play(soundID,  (error: BusinessError, streamId: number) => {
      if (error) {
        console.error(`Failed to play soundpool: errCode is ${error.code}, errMessage is ${error.message}`);
      } else {
        streamID = streamId;
        console.info('Succeeded in playing soundpool, streamId:' + streamId);
      }
    });
  }
});

play

play(soundID: number, params?: PlayParameters): Promise<number>

Plays a sound. This API uses a promise to obtain the audio stream ID.

System capability: SystemCapability.Multimedia.Media.SoundPool

Parameters

NameTypeMandatoryDescription
soundIDnumberYesSound ID, which is obtained by calling load().
paramsPlayParametersNoPlayback parameters.

Return value

TypeDescription
Promise<number>Promise used to return the audio stream ID. A valid value must be greater than 0.

Error codes

For details about the error codes, see Media Error Codes.

IDError Message
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. Return by promise.
5400102Operation not allowed. Return by promise.
5400105Service died. Return by promise.

Example

import { BusinessError } from '@kit.BasicServicesKit';

// Create a SoundPool instance.
let soundPool: media.SoundPool;
let audioRendererInfo: audio.AudioRendererInfo = {
  usage: audio.StreamUsage.STREAM_USAGE_MUSIC,
  rendererFlags: 1
}
media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => {
  if (error) {
    console.error(`Failed to createSoundPool`);
    return;
  } else {
    soundPool = soundPool_;
    console.info(`Succeeded in createSoundPool`);
    let soundID: number = 0;
    let streamID: number = 0;
    let playParameters: media.PlayParameters = {
      loop: 3, // The sound is played four times (three loops).
      rate: audio.AudioRendererRate.RENDER_RATE_NORMAL, // The sound is played at the original frequency.
      leftVolume: 0.5, // range = 0.0-1.0.
      rightVolume: 0.5, // range = 0.0-1.0.
      priority: 0, // The sound playback has the lowest priority.
    }

    soundPool.play(soundID, playParameters).then((streamId: number) => {
      console.info('Succeeded in playing soundpool');
      streamID = streamId;
    },(err: BusinessError) => {
      console.error('Failed to play soundpool and catch error is ' + err.message);
    });
  }
});

stop

stop(streamID: number, callback: AsyncCallback<void>): void

Stops playing a sound. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Media.SoundPool

Parameters

NameTypeMandatoryDescription
streamIDnumberYesAudio stream ID, which is obtained by calling play().
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

For details about the error codes, see Media Error Codes.

IDError Message
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. Return by callback.
5400102Operation not allowed. Return by callback.
5400105Service died. Return by callback.

Example

import { BusinessError } from '@kit.BasicServicesKit';

// Create a SoundPool instance.
let soundPool: media.SoundPool;
let audioRendererInfo: audio.AudioRendererInfo = {
  usage: audio.StreamUsage.STREAM_USAGE_MUSIC,
  rendererFlags: 1
}
media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => {
  if (error) {
    console.error(`Failed to createSoundPool`);
    return;
  } else {
    soundPool = soundPool_;
    console.info(`Succeeded in createSoundPool`);
    let streamID: number = 0;
    // Call play() to obtain the stream ID.
    soundPool.stop(streamID, (error: BusinessError) => {
      if (error) {
        console.error(`Failed to stop soundpool: errCode is ${error.code}, errMessage is ${error.message}`);
      } else {
        console.info('Succeeded in stopping soundpool');
      }
    })
  }
});

stop

stop(streamID: number): Promise<void>

Stops playing a sound. This API uses a promise to return the result.

System capability: SystemCapability.Multimedia.Media.SoundPool

Parameters

NameTypeMandatoryDescription
streamIDnumberYesAudio stream ID, which is obtained by calling play().

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Media Error Codes.

IDError Message
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. Return by promise.
5400102Operation not allowed. Return by promise.
5400105Service died. Return by promise.

Example

import { BusinessError } from '@kit.BasicServicesKit';

// Create a SoundPool instance.
let soundPool: media.SoundPool;
let audioRendererInfo: audio.AudioRendererInfo = {
  usage: audio.StreamUsage.STREAM_USAGE_MUSIC,
  rendererFlags: 1
}
media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => {
  if (error) {
    console.error(`Failed to createSoundPool`);
    return;
  } else {
    soundPool = soundPool_;
    console.info(`Succeeded in createSoundPool`);
    let streamID: number = 0;
    // Call play() to obtain the stream ID.
    soundPool.stop(streamID).then(() => {
      console.info('Succeeded in stopping soundpool');
    }, (err: BusinessError) => {
      console.error('Failed to stop soundpool and catch error is ' + err.message);
    });
  }
});

setLoop

setLoop(streamID: number, loop: number, callback: AsyncCallback<void>): void;

Sets the loop mode for an audio stream. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Media.SoundPool

Parameters

NameTypeMandatoryDescription
streamIDnumberYesAudio stream ID, which is obtained by calling play().
loopnumberYesNumber of loops.
If this parameter is set to a value greater than or equal to 0, the number of times the content is actually played is the value of loop plus 1.
If this parameter is set to a value less than 0, the content is played repeatedly.
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

For details about the error codes, see Media Error Codes.

IDError Message
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. Return by callback.
5400102Operation not allowed. Return by callback.
5400105Service died. Return by callback.

Example

import { BusinessError } from '@kit.BasicServicesKit';

// Create a SoundPool instance.
let soundPool: media.SoundPool;
let audioRendererInfo: audio.AudioRendererInfo = {
  usage: audio.StreamUsage.STREAM_USAGE_MUSIC,
  rendererFlags: 1
}
media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => {
  if (error) {
    console.error(`Failed to createSoundPool`);
    return;
  } else {
    soundPool = soundPool_;
    console.info(`Succeeded in createSoundPool`);
    let streamID: number = 0;
    // Call play() to obtain the stream ID.
    // Set the number of loops to 2.
    soundPool.setLoop(streamID, 2, (error: BusinessError) => {
      if (error) {
        console.error(`Failed to setLoop soundPool: errCode is ${error.code}, errMessage is ${error.message}`);
      } else {
        console.info('Succeeded in setLoopping soundpool, streamID:' + streamID);
      }
    });
  }
});

setLoop

setLoop(streamID: number, loop: number): Promise<void>

Sets the loop mode for an audio stream. This API uses a promise to return the result.

System capability: SystemCapability.Multimedia.Media.SoundPool

Parameters

NameTypeMandatoryDescription
streamIDnumberYesAudio stream ID, which is obtained by calling play().
loopnumberYesNumber of loops.
If this parameter is set to a value greater than or equal to 0, the number of times the content is actually played is the value of loop plus 1.
If this parameter is set to a value less than 0, the content is played repeatedly.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Media Error Codes.

IDError Message
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. Return by promise.
5400102Operation not allowed. Return by promise.
5400105Service died. Return by promise.

Example

import { BusinessError } from '@kit.BasicServicesKit';

// Create a SoundPool instance.
let soundPool: media.SoundPool;
let audioRendererInfo: audio.AudioRendererInfo = {
  usage: audio.StreamUsage.STREAM_USAGE_MUSIC,
  rendererFlags: 1
}
media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => {
  if (error) {
    console.error(`Failed to createSoundPool`);
    return;
  } else {
    soundPool = soundPool_;
    console.info(`Succeeded in createSoundPool`);
    let streamID: number = 0;
    // Call play() to obtain the stream ID.
    // Set the number of loops to 1.
    soundPool.setLoop(streamID, 1).then(() => {
      console.info('Succeeded in setLoopping soundpool, streamID:' + streamID);
    }).catch((err: BusinessError) => {
      console.error('Failed to setLoop soundPool and catch error is ' + err.message);
    });
  }
});

setPriority

setPriority(streamID: number, priority: number, callback: AsyncCallback<void>): void

Sets the priority for an audio stream. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Media.SoundPool

Parameters

NameTypeMandatoryDescription
streamIDnumberYesAudio stream ID, which is obtained by calling play().
prioritynumberYesPriority. The value 0 means the lowest priority. The value is an integer greater than or equal to 0.
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

For details about the error codes, see Media Error Codes.

IDError Message
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. Return by callback.
5400102Operation not allowed. Return by callback.
5400105Service died. Return by callback.

Example

import { BusinessError } from '@kit.BasicServicesKit';

// Create a SoundPool instance.
let soundPool: media.SoundPool;
let audioRendererInfo: audio.AudioRendererInfo = {
  usage: audio.StreamUsage.STREAM_USAGE_MUSIC,
  rendererFlags: 1
}
media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => {
  if (error) {
    console.error(`Failed to createSoundPool`);
    return;
  } else {
    soundPool = soundPool_;
    console.info(`Succeeded in createSoundPool`);
    let streamID: number = 0;
    // Call play() to obtain the stream ID.
    // Set the priority to 1.
    soundPool.setPriority(streamID, 1, (error: BusinessError) => {
      if (error) {
        console.error(`Failed to setPriority soundPool: errCode is ${error.code}, errMessage is ${error.message}`);
      } else {
        console.info('Succeeded in setPriority soundpool, streamID:' + streamID);
      }
    });
  }
});

setPriority

setPriority(streamID: number, priority: number): Promise<void>

Sets the priority for an audio stream. This API uses a promise to return the result.

System capability: SystemCapability.Multimedia.Media.SoundPool

Parameters

NameTypeMandatoryDescription
streamIDnumberYesAudio stream ID, which is obtained by calling play().
prioritynumberYesPriority. The value 0 means the lowest priority. The value is an integer greater than or equal to 0.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Media Error Codes.

IDError Message
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. Return by promise.
5400102Operation not allowed. Return by promise.
5400105Service died. Return by promise.

Example

import { BusinessError } from '@kit.BasicServicesKit';

// Create a SoundPool instance.
let soundPool: media.SoundPool;
let audioRendererInfo: audio.AudioRendererInfo = {
  usage: audio.StreamUsage.STREAM_USAGE_MUSIC,
  rendererFlags: 1
}
media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => {
  if (error) {
    console.error(`Failed to createSoundPool`);
    return;
  } else {
    soundPool = soundPool_;
    console.info(`Succeeded in createSoundPool`);
    let streamID: number = 0;
    // Call play() to obtain the stream ID.
    // Set the priority to 1.

    soundPool.setPriority(streamID, 1).then(() => {
      console.info('Succeeded in setPriority soundpool');
    }, (err: BusinessError) => {
      console.error('Failed to setPriority soundPool and catch error is ' + err.message);
    });
  }
});

setRate

setRate(streamID: number, rate: audio.AudioRendererRate, callback: AsyncCallback<void>): void

Sets the playback rate for an audio stream. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Media.SoundPool

Parameters

NameTypeMandatoryDescription
streamIDnumberYesAudio stream ID, which is obtained by calling play().
rateaudio.AudioRendererRateYesPlayback rate.
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

For details about the error codes, see Media Error Codes.

IDError Message
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. Return by callback.
5400102Operation not allowed. Return by callback.
5400105Service died. Return by callback.

Example

import { BusinessError } from '@kit.BasicServicesKit';

// Create a SoundPool instance.
let soundPool: media.SoundPool;
let audioRendererInfo: audio.AudioRendererInfo = {
  usage: audio.StreamUsage.STREAM_USAGE_MUSIC,
  rendererFlags: 1
}
media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => {
  if (error) {
    console.error(`Failed to createSoundPool`);
    return;
  } else {
    soundPool = soundPool_;
    console.info(`Succeeded in createSoundPool`);
    let streamID: number = 0;
    let selectedAudioRendererRate: audio.AudioRendererRate = audio.AudioRendererRate.RENDER_RATE_NORMAL; // The sound is played at the original frequency.
    // Call play() to obtain the stream ID.
    soundPool.setRate(streamID, selectedAudioRendererRate, (error: BusinessError) => {
      if (error) {
        console.error(`Failed to setRate soundPool: errCode is ${error.code}, errMessage is ${error.message}`);
      } else {
        console.info('Succeeded in setRate success, streamID:' + streamID);
      }
    })
  }
});

setRate

setRate(streamID: number, rate: audio.AudioRendererRate): Promise<void>

Sets the playback rate for an audio stream. This API uses a promise to return the result.

System capability: SystemCapability.Multimedia.Media.SoundPool

Parameters

NameTypeMandatoryDescription
streamIDnumberYesAudio stream ID, which is obtained by calling play().
rateaudio.AudioRendererRateYesPlayback rate.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Media Error Codes.

IDError Message
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. Return by promise.
5400102Operation not allowed. Return by promise.
5400105Service died. Return by promise.

Example

import { BusinessError } from '@kit.BasicServicesKit';

// Create a SoundPool instance.
let soundPool: media.SoundPool;
let audioRendererInfo: audio.AudioRendererInfo = {
  usage: audio.StreamUsage.STREAM_USAGE_MUSIC,
  rendererFlags: 1
}
media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => {
  if (error) {
    console.error(`Failed to createSoundPool`);
    return;
  } else {
    soundPool = soundPool_;
    console.info(`Succeeded in createSoundPool`);
    let streamID: number = 0;
    let selectedAudioRendererRate: audio.AudioRendererRate = audio.AudioRendererRate.RENDER_RATE_NORMAL; // The sound is played at the original frequency.
    // Call play() to obtain the stream ID.
    soundPool.setRate(streamID, selectedAudioRendererRate).then(() => {
      console.info('Succeeded in setRate soundpool');
    }, (err: BusinessError) => {
      console.error('Failed to setRate soundpool and catch error is ' + err.message);
    });
  }
});

setVolume

setVolume(streamID: number, leftVolume: number, rightVolume: number, callback: AsyncCallback<void>): void

Sets the volume for an audio stream. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Media.SoundPool

Parameters

NameTypeMandatoryDescription
streamIDnumberYesAudio stream ID, which is obtained by calling play().
leftVolumenumberYesVolume of the left channel. The value ranges from 0.0 to 1.0.
rightVolumenumberYesVolume of the right channel. The value ranges from 0.0 to 1.0. Currently, setting the volume for the right channel does not take effect. The volume set for the left channel is used.
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

For details about the error codes, see Media Error Codes.

IDError Message
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. Return by callback.
5400102Operation not allowed. Return by callback.
5400105Service died. Return by callback.

Example

import { BusinessError } from '@kit.BasicServicesKit';

// Create a SoundPool instance.
let soundPool: media.SoundPool;
let audioRendererInfo: audio.AudioRendererInfo = {
  usage: audio.StreamUsage.STREAM_USAGE_MUSIC,
  rendererFlags: 1
}
media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => {
  if (error) {
    console.error(`Failed to createSoundPool`);
    return;
  } else {
    soundPool = soundPool_;
    console.info(`Succeeded in createSoundPool`);
    let streamID: number = 0;
    // Call play() to obtain the stream ID.
    // Set the volume to 0.5.
    soundPool.setVolume(streamID, 0.5, 0.5, (error: BusinessError) => {
      if (error) {
        console.error(`Failed to setVolume soundPool: errCode is ${error.code}, errMessage is ${error.message}`);
      } else {
        console.info('Succeeded in setVolume soundpool, streamID:' + streamID);
      }
    })
  }
});

setVolume

setVolume(streamID: number, leftVolume: number, rightVolume: number): Promise<void>

Sets the volume for an audio stream. This API uses a promise to return the result.

System capability: SystemCapability.Multimedia.Media.SoundPool

Parameters

NameTypeMandatoryDescription
streamIDnumberYesAudio stream ID, which is obtained by calling play().
leftVolumenumberYesVolume of the left channel. The value ranges from 0.0 to 1.0.
rightVolumenumberYesVolume of the right channel. The value ranges from 0.0 to 1.0. Currently, setting the volume for the right channel does not take effect. The volume set for the left channel is used.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Media Error Codes.

IDError Message
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. Return by promise.
5400102Operation not allowed. Return by promise.
5400105Service died. Return by promise.

Example

import { BusinessError } from '@kit.BasicServicesKit';

// Create a SoundPool instance.
let soundPool: media.SoundPool;
let audioRendererInfo: audio.AudioRendererInfo = {
  usage: audio.StreamUsage.STREAM_USAGE_MUSIC,
  rendererFlags: 1
}
media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => {
  if (error) {
    console.error(`Failed to createSoundPool`);
    return;
  } else {
    soundPool = soundPool_;
    console.info(`Succeeded in createSoundPool`);
    let streamID: number = 0;
    // Call play() to obtain the stream ID.

    soundPool.setVolume(streamID, 0.5, 0.5).then(() => {
      console.info('Succeeded in setVolume soundpool');
    }, (err: BusinessError) => {
      console.error('Failed to setVolume soundPool and catch error is ' + err.message);
    });
  }
});

unload

unload(soundID: number, callback: AsyncCallback<void>): void

Unloads a sound. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Media.SoundPool

Parameters

NameTypeMandatoryDescription
soundIDnumberYesSound ID, which is obtained by calling load().
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

For details about the error codes, see Media Error Codes.

IDError Message
5400102Operation not allowed. Return by callback.
5400103I/O error. Return by callback.
5400105Service died. Return by callback.

Example

import { BusinessError } from '@kit.BasicServicesKit';

// Create a SoundPool instance.
let soundPool: media.SoundPool;
let audioRendererInfo: audio.AudioRendererInfo = {
  usage: audio.StreamUsage.STREAM_USAGE_MUSIC,
  rendererFlags: 1
}
media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => {
  if (error) {
    console.error(`Failed to createSoundPool`);
    return;
  } else {
    soundPool = soundPool_;
    console.info(`Succeeded in createSoundPool`);
    let soundID: number = 0;
    // Call load() to obtain the sound ID.
    soundPool.unload(soundID, (error: BusinessError) => {
      if (error) {
        console.error(`Failed to unload soundPool: errCode is ${error.code}, errMessage is ${error.message}`);
      } else {
        console.info('Succceeded in unload soundPool');
      }
    })
  }
});

unload

unload(soundID: number): Promise<void>

Unloads a sound. This API uses a promise to return the result.

System capability: SystemCapability.Multimedia.Media.SoundPool

Parameters

NameTypeMandatoryDescription
soundIDnumberYesSound ID, which is obtained by calling load().

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Media Error Codes.

IDError Message
5400102Operation not allowed. Return by promise.
5400103I/O error. Return by promise.
5400105Service died. Return by promise.

Example

import { BusinessError } from '@kit.BasicServicesKit';

// Create a SoundPool instance.
let soundPool: media.SoundPool;
let audioRendererInfo: audio.AudioRendererInfo = {
  usage: audio.StreamUsage.STREAM_USAGE_MUSIC,
  rendererFlags: 1
}
media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => {
  if (error) {
    console.error(`Failed to createSoundPool`);
    return;
  } else {
    soundPool = soundPool_;
    console.info(`Succeeded in createSoundPool`);
    let soundID: number = 0;
    // Call load() to obtain the sound ID.

    soundPool.unload(soundID).then(() => {
      console.info('Succceeded in unload soundPool');
    }, (err: BusinessError) => {
      console.error('Failed to unload soundPool and catch error is ' + err.message);
    });
  }
});

release

release(callback: AsyncCallback<void>): void

Releases this SoundPool instance. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Media.SoundPool

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

For details about the error codes, see Media Error Codes.

IDError Message
5400105Service died. Return by callback.

Example

import { BusinessError } from '@kit.BasicServicesKit';

// Create a SoundPool instance.
let soundPool: media.SoundPool;
let audioRendererInfo: audio.AudioRendererInfo = {
  usage: audio.StreamUsage.STREAM_USAGE_MUSIC,
  rendererFlags: 1
}
media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => {
  if (error) {
    console.error(`Failed to createSoundPool`);
    return;
  } else {
    soundPool = soundPool_;
    console.info(`Succeeded in createSoundPool`);
    soundPool.release((error: BusinessError) => {
      if (error) {
        console.error(`Failed to release soundPool: errCode is ${error.code}, errMessage is ${error.message}`);
      } else {
        console.info('Succeeded in releasing soundPool');
      }
    })
  }
});


release

release(): Promise<void>

Releases this SoundPool instance. This API uses a promise to return the result.

System capability: SystemCapability.Multimedia.Media.SoundPool

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Media Error Codes.

IDError Message
5400105Service died. Return by promise.

Example

import { BusinessError } from '@kit.BasicServicesKit';

// Create a SoundPool instance.
let soundPool: media.SoundPool;
let audioRendererInfo: audio.AudioRendererInfo = {
  usage: audio.StreamUsage.STREAM_USAGE_MUSIC,
  rendererFlags: 1
}
media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => {
  if (error) {
    console.error(`Failed to createSoundPool`);
    return;
  } else {
    soundPool = soundPool_;
    console.info(`Succeeded in createSoundPool`);
    soundPool.release().then(() => {
      console.info('Succeeded in releasing soundPool');
    }, (err: BusinessError) => {
      console.error('Failed to release soundPool and catch error is ' + err.message);
    });
  }
});

on('loadComplete')

on(type: 'loadComplete', callback: Callback<number>): void

Subscribes to events indicating that a sound finishes loading.

System capability: SystemCapability.Multimedia.Media.SoundPool

Parameters

NameTypeMandatoryDescription
typestringYesEvent type, which is 'loadComplete' in this case. This event is triggered when a sound is loaded.
callbackCallback<number>YesID of the sound that has been loaded.

Example

import { BusinessError } from '@kit.BasicServicesKit';

// Create a SoundPool instance.
let soundPool: media.SoundPool;
let audioRendererInfo: audio.AudioRendererInfo = {
  usage: audio.StreamUsage.STREAM_USAGE_MUSIC,
  rendererFlags: 1
}
media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => {
  if (error) {
    console.error(`Failed to createSoundPool`);
    return;
  } else {
    soundPool = soundPool_;
    console.info(`Succeeded in createSoundPool`);
    soundPool.on('loadComplete', (soundId: number) => {
      console.info('Succeeded in loadComplete, soundId: ' + soundId);
    })
  }
});

off('loadComplete')

off(type: 'loadComplete'): void

Unsubscribes from events indicating that a sound finishes loading.

System capability: SystemCapability.Multimedia.Media.SoundPool

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'loadComplete'.

Example

import { BusinessError } from '@kit.BasicServicesKit';

// Create a SoundPool instance.
let soundPool: media.SoundPool;
let audioRendererInfo: audio.AudioRendererInfo = {
  usage: audio.StreamUsage.STREAM_USAGE_MUSIC,
  rendererFlags: 1
}
media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => {
  if (error) {
    console.error(`Failed to createSoundPool`);
    return;
  } else {
    soundPool = soundPool_;
    console.info(`Succeeded in createSoundPool`);
    soundPool.off('loadComplete');
  }
});

on('playFinishedWithStreamId')18+

on(type: 'playFinishedWithStreamId', callback: Callback<number>): void

Subscribes to events indicating the completion of audio playback and returns the stream ID of the audio that finishes playing.

When only on('playFinished') or on('playFinishedWithStreamId') is subscribed to, the registered callback is triggered when the audio playback is complete.

When both on('playFinished') and on('playFinishedWithStreamId') are subscribed to, the 'playFinishedWithStreamId' callback is triggered, but the 'playFinished' callback is not triggered, when the audio playback is complete.

System capability: SystemCapability.Multimedia.Media.SoundPool

Parameters

NameTypeMandatoryDescription
typestringYesEvent type, which is 'playFinishedWithStreamId' in this case. This event is triggered when an audio stream finishes playing, and the stream ID is returned.
callbackCallback<number>YesCallback used to return the result. Stream ID of the audio that finishes playing.

Example

import { BusinessError } from '@kit.BasicServicesKit';

// Create a SoundPool instance.
let soundPool_: media.SoundPool;
let audioRendererInfo: audio.AudioRendererInfo = {
  usage: audio.StreamUsage.STREAM_USAGE_MUSIC,
  rendererFlags: 1
}
media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool: media.SoundPool) => {
  if (error) {
    console.error(`Failed to createSoundPool`);
  } else {
    soundPool_ = soundPool;
    console.info(`Succeeded in createSoundPool`);
    soundPool_.on('playFinishedWithStreamId', (streamId) => {
      console.info('The stream with streamId: ' + streamId + ' has finished playing.');
    });
  }
});

off('playFinishedWithStreamId')18+

off(type: 'playFinishedWithStreamId'): void

Unsubscribes from events indicating that a sound finishes playing.

System capability: SystemCapability.Multimedia.Media.SoundPool

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'playFinishedWithStreamId'.

Example

import { BusinessError } from '@kit.BasicServicesKit';

// Create a SoundPool instance.
let soundPool_: media.SoundPool;
let audioRendererInfo: audio.AudioRendererInfo = {
  usage: audio.StreamUsage.STREAM_USAGE_MUSIC,
  rendererFlags: 1
}
media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool: media.SoundPool) => {
  if (error) {
    console.error(`Failed to createSoundPool`);
  } else {
    soundPool_ = soundPool;
    console.info(`Succeeded in createSoundPool`);
    soundPool_.off('playFinishedWithStreamId');
  }
});

on('playFinished')

on(type: 'playFinished', callback: Callback<void>): void

Subscribes to events indicating that a sound finishes playing.

System capability: SystemCapability.Multimedia.Media.SoundPool

Parameters

NameTypeMandatoryDescription
typestringYesEvent type, which is 'playFinished' in this case. This event is triggered when a sound finishes playing.
callbackCallback<void>YesCallback used to return the result.

Example

import { BusinessError } from '@kit.BasicServicesKit';

// Create a SoundPool instance.
let soundPool: media.SoundPool;
let audioRendererInfo: audio.AudioRendererInfo = {
  usage: audio.StreamUsage.STREAM_USAGE_MUSIC,
  rendererFlags: 1
}
media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => {
  if (error) {
    console.error(`Failed to createSoundPool`);
    return;
  } else {
    soundPool = soundPool_;
    console.info(`Succeeded in createSoundPool`);
    soundPool.on('playFinished', () => {
      console.info('Succeeded in playFinished');
    });
  }
});

off('playFinished')

off(type: 'playFinished'): void

Unsubscribes from events indicating that a sound finishes playing.

System capability: SystemCapability.Multimedia.Media.SoundPool

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is fixed at 'playFinished'.

Example

import { BusinessError } from '@kit.BasicServicesKit';

// Create a SoundPool instance.
let soundPool: media.SoundPool;
let audioRendererInfo: audio.AudioRendererInfo = {
  usage: audio.StreamUsage.STREAM_USAGE_MUSIC,
  rendererFlags: 1
}
media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => {
  if (error) {
    console.error(`Failed to createSoundPool`);
    return;
  } else {
    soundPool = soundPool_;
    console.info(`Succeeded in createSoundPool`);
    soundPool.off('playFinished');
  }
});

on('error')

on(type: 'error', callback: ErrorCallback): void

Subscribes to error events of this SoundPool instance. This event is used only for error prompt.

System capability: SystemCapability.Multimedia.Media.SoundPool

Parameters

NameTypeMandatoryDescription
typestringYesEvent type, which is 'error' in this case. This event can be triggered by both user operations and the system.
callbackErrorCallbackYesCallback used to return the error code ID and error message.

Example

import { BusinessError } from '@kit.BasicServicesKit';

// Create a SoundPool instance.
let soundPool: media.SoundPool;
let audioRendererInfo: audio.AudioRendererInfo = {
  usage: audio.StreamUsage.STREAM_USAGE_MUSIC,
  rendererFlags: 1
}
media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => {
  if (error) {
    console.error(`Failed to createSoundPool`);
    return;
  } else {
    soundPool = soundPool_;
    console.info(`Succeeded in createSoundPool`);
    soundPool.on('error', (error: BusinessError) => {
      console.error('error happened,and error message is :' + error.message);
      console.error('error happened,and error code is :' + error.code);
    })
  }
});

off('error')

off(type: 'error'): void

Unsubscribes from error events of this SoundPool instance.

System capability: SystemCapability.Multimedia.Media.SoundPool

Parameters

NameTypeMandatoryDescription
typestringYesEvent type, which is 'error' in this case.

Example

import { BusinessError } from '@kit.BasicServicesKit';

// Create a SoundPool instance.
let soundPool: media.SoundPool;
let audioRendererInfo: audio.AudioRendererInfo = {
  usage: audio.StreamUsage.STREAM_USAGE_MUSIC,
  rendererFlags: 1
}
media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => {
  if (error) {
    console.error(`Failed to createSoundPool`);
    return;
  } else {
    soundPool = soundPool_;
    console.info(`Succeeded in createSoundPool`);
    soundPool.off('error');
  }
});

on('errorOccurred')20+

on(type: 'errorOccurred', callback: Callback<ErrorInfo>): void

Subscribes to error events of this SoundPool instance and returns ErrorInfo that contains the error code, error stage, resource ID, and audio stream ID.

System capability: SystemCapability.Multimedia.Media.SoundPool

Parameters

NameTypeMandatoryDescription
typestringYesEvent type, which is 'errorOccurred' in this case. This event can be triggered by both user operations and the system.
callbackCallback<ErrorInfo>YesCallback used to return ErrorInfo.

Example

import { BusinessError } from '@kit.BasicServicesKit';

// Create a SoundPool instance.
let soundPool: media.SoundPool;
let audioRendererInfo: audio.AudioRendererInfo = {
  usage: audio.StreamUsage.STREAM_USAGE_MUSIC,
  rendererFlags: 1
}
media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => {
  if (error) {
    console.error(`Failed to createSoundPool`);
    return;
  } else {
    soundPool = soundPool_;
    console.info(`Succeeded in createSoundPool`);
    soundPool.on('errorOccurred', (errorInfo) => {
      console.error('error happened,and error message is :' + errorInfo.errorCode.message);
      console.error('error happened,and error code is :' + errorInfo.errorCode.code);
      console.error('error happened,and errorType is :' + errorInfo.errorType);
      console.error('error happened,and soundId is :' + errorInfo.soundId);
      console.error('error happened,and streamId is :' + errorInfo.streamId);
    })
  }
});

off('errorOccurred')20+

off(type: 'errorOccurred', callback?: Callback<ErrorInfo>): void

Unsubscribes from error events of this SoundPool instance.

System capability: SystemCapability.Multimedia.Media.SoundPool

Parameters

NameTypeMandatoryDescription
typestringYesEvent type, which is 'errorOccurred' in this case.
callbackCallback<ErrorInfo>NoCallback used to return ErrorInfo.

Example

import { BusinessError } from '@kit.BasicServicesKit';

// Create a SoundPool instance.
let soundPool: media.SoundPool;
let audioRendererInfo: audio.AudioRendererInfo = {
  usage: audio.StreamUsage.STREAM_USAGE_MUSIC,
  rendererFlags: 1
}
media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => {
  if (error) {
    console.error(`Failed to createSoundPool`);
    return;
  } else {
    soundPool = soundPool_;
    console.info(`Succeeded in createSoundPool`);
    soundPool.off('errorOccurred');
  }
});

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Media Kit

harmony 鸿蒙AVImageGenerator

harmony 鸿蒙AVMetadataExtractor

harmony 鸿蒙AVPlayer

harmony 鸿蒙AVPlayerCallback

harmony 鸿蒙AVRecorder

harmony 鸿蒙AVScreenCapture

harmony 鸿蒙OH_AVRecorder_Config

harmony 鸿蒙OH_AVRecorder_EncoderInfo

harmony 鸿蒙OH_AVRecorder_Location

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