/* * Copyright (c) 2021-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * @file * @kit MediaKit */ import type { ErrorCallback, AsyncCallback, Callback } from '../@ohos.base'; import type audio from '../@ohos.multimedia.audio'; /** * Interface for play parameters. * @typedef PlayParameters * @syscap SystemCapability.Multimedia.Media.SoundPool * @since 10 */ export interface PlayParameters { /** * loop mode (0 = no loop, -1 = loop forever) * * @type { ?number } * @syscap SystemCapability.Multimedia.Media.SoundPool * @since 10 */ loop?: number; /** * playback rate * * @type { ?number } * @syscap SystemCapability.Multimedia.Media.SoundPool * @since 10 */ rate?: number; /** * left volume value(range = 0.0 to 1.0),current leftVolume = rightVolume * * @type { ?number } * @syscap SystemCapability.Multimedia.Media.SoundPool * @since 10 */ leftVolume?: number; /** * right volume value(range = 0.0 to 1.0),current leftVolume = rightVolume * * @type { ?number } * @syscap SystemCapability.Multimedia.Media.SoundPool * @since 10 */ rightVolume?: number; /** * stream priority (0 = lowest priority) * * @type { ?number } * @syscap SystemCapability.Multimedia.Media.SoundPool * @since 10 */ priority?: number; /** * Flag indicating that the sound effect and audio can be played in parallel. * * @type { ?boolean } * @syscap SystemCapability.Multimedia.Media.SoundPool * @systemapi * @since 10 */ parallelPlayFlag?: boolean; } /** * Interface for soundPool instance. Manages and plays sound. Before calling an SoundPool method, you must use createSoundPool() * to create an SoundPool instance. * @typedef SoundPool * @syscap SystemCapability.Multimedia.Media.SoundPool * @since 10 */ export interface SoundPool { /** * Load the sound from the specified path. * * @param {string} uri The path to the audio file * @param {AsyncCallback} callback Callback a sound ID. This value can be used to play or unload the sound. * @throws { BusinessError } 5400102 - Operation not allowed. Return by callback. * @throws { BusinessError } 5400103 - I/O error. Return by callback. * @throws { BusinessError } 5400105 - Service died. Return by callback. * @syscap SystemCapability.Multimedia.Media.SoundPool * @since 10 */ load(uri: string, callback: AsyncCallback): void; /** * Load the sound from the specified path. * * @param {string} uri The path to the audio file * @returns {Promise} Promise a sound ID. This value can be used to play or unload the sound. * @throws { BusinessError } 5400102 - Operation not allowed. Return by promise. * @throws { BusinessError } 5400103 - I/O error. Return by promise. * @throws { BusinessError } 5400105 - Service died. Return by promise. * @syscap SystemCapability.Multimedia.Media.SoundPool * @since 10 */ load(uri: string): Promise; /** * Load the sound from a FileDescriptor. * * @param {number} fd A FileDescriptor object * @param {number} offset Offset to the start of the sound * @param {number} length Length of the sound * @param {AsyncCallback} callback Callback a sound ID. This value can be used to play or unload the sound. * @throws { BusinessError } 5400102 - Operation not allowed. Return by callback. * @throws { BusinessError } 5400103 - I/O error. Return by callback. * @throws { BusinessError } 5400105 - Service died. Return by callback. * @syscap SystemCapability.Multimedia.Media.SoundPool * @since 10 */ load(fd: number, offset: number, length: number, callback: AsyncCallback): void; /** * Load the sound from a FileDescriptor. * * @param {number} fd A FileDescriptor object * @param {number} offset Offset to the start of the sound * @param {number} length Length of the sound * @returns {Promise} Promise a sound ID. This value can be used to play or unload the sound. * @throws { BusinessError } 5400102 - Operation not allowed. Return by promise. * @throws { BusinessError } 5400103 - I/O error. Return by promise. * @throws { BusinessError } 5400105 - Service died. Return by promise. * @syscap SystemCapability.Multimedia.Media.SoundPool * @since 10 */ load(fd: number, offset: number, length: number): Promise; /** * Play a sound from a sound ID. * * @param {number} soundID Returned by the load() * @param {PlayParameters} params Player parameters * @param {AsyncCallback} callback Callback used to return a non-zero streamID if successful, zero if it fails. * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. *
2.Incorrect parameter types. 3.Parameter verification failed. Return by callback. * @throws { BusinessError } 5400102 - Operation not allowed. Return by callback. * @throws { BusinessError } 5400105 - Service died. Return by callback. * @syscap SystemCapability.Multimedia.Media.SoundPool * @since 10 */ play(soundID: number, params: PlayParameters, callback: AsyncCallback): void; /** * Play a sound from a sound ID. * * @param {number} soundID Returned by the load() * @param {AsyncCallback} callback Callback used to return a non-zero streamID if successful, zero if it fails. * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. *
2.Incorrect parameter types. 3.Parameter verification failed. Return by callback. * @throws { BusinessError } 5400102 - Operation not allowed. Return by callback. * @throws { BusinessError } 5400105 - Service died. Return by callback. * @syscap SystemCapability.Multimedia.Media.SoundPool * @since 10 */ play(soundID: number, callback: AsyncCallback): void; /** * Play a sound from a sound ID. * * @param {number} soundID Returned by the load() * @param {PlayParameters} [params] Player parameters * @returns {Promise} Promise used to return a non-zero streamID if successful, zero if it fails. * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. *
2.Incorrect parameter types. 3.Parameter verification failed. Return by promise. * @throws { BusinessError } 5400102 - Operation not allowed. Return by promise. * @throws { BusinessError } 5400105 - Service died. Return by promise. * @syscap SystemCapability.Multimedia.Media.SoundPool * @since 10 */ play(soundID: number, params?: PlayParameters): Promise; /** * Stop a stream which is playing. * * @param {number} streamID Returned by the play() * @param {AsyncCallback} callback Callback used to return the result. * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. *
2.Incorrect parameter types. 3.Parameter verification failed. Return by callback. * @throws { BusinessError } 5400102 - Operation not allowed. Return by callback. * @throws { BusinessError } 5400105 - Service died. Return by callback. * @syscap SystemCapability.Multimedia.Media.SoundPool * @since 10 */ stop(streamID: number, callback: AsyncCallback): void; /** * Stop a stream which is playing. * * @param {number} streamID Returned by the play() * @returns {Promise} Promise used to return the result. * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. *
2.Incorrect parameter types. 3.Parameter verification failed. Return by promise. * @throws { BusinessError } 5400102 - Operation not allowed. Return by promise. * @throws { BusinessError } 5400105 - Service died. Return by promise. * @syscap SystemCapability.Multimedia.Media.SoundPool * @since 10 */ stop(streamID: number): Promise; /** * Set loop mode. * * @param {number} streamID Returned by the play() * @param {number} loop Loop mode (0 = no loop, -1 = loop forever) * @param {AsyncCallback} callback Callback used to return the result. * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. *
2.Incorrect parameter types. 3.Parameter verification failed. Return by callback. * @throws { BusinessError } 5400102 - Operation not allowed. Return by callback. * @throws { BusinessError } 5400105 - Service died. Return by callback. * @syscap SystemCapability.Multimedia.Media.SoundPool * @since 10 */ setLoop(streamID: number, loop: number, callback: AsyncCallback): void; /** * Set loop mode. * * @param {number} streamID Returned by the play() * @param {number} loop Loop mode (0 = no loop, -1 = loop forever) * @returns {Promise} Promise used to return the result. * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. *
2.Incorrect parameter types. 3.Parameter verification failed. Return by promise. * @throws { BusinessError } 5400102 - Operation not allowed. Return by promise. * @throws { BusinessError } 5400105 - Service died. Return by promise. * @syscap SystemCapability.Multimedia.Media.SoundPool * @since 10 */ setLoop(streamID: number, loop: number): Promise; /** * Set stream priority. * * @param {number} streamID Returned by the play() * @param {number} priority Stream priority (0 = lowest priority) * @param {AsyncCallback} callback Callback used to return the result. * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. *
2.Incorrect parameter types. 3.Parameter verification failed. Return by callback. * @throws { BusinessError } 5400102 - Operation not allowed. Return by callback. * @throws { BusinessError } 5400105 - Service died. Return by callback. * @syscap SystemCapability.Multimedia.Media.SoundPool * @since 10 */ setPriority(streamID: number, priority: number, callback: AsyncCallback): void; /** * Set stream priority. * * @param {number} streamID Returned by the play() * @param {number} priority Stream priority (0 = lowest priority) * @returns {Promise} Promise used to return the result. * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. *
2.Incorrect parameter types. 3.Parameter verification failed. Return by promise. * @throws { BusinessError } 5400102 - Operation not allowed. Return by promise. * @throws { BusinessError } 5400105 - Service died. Return by promise. * @syscap SystemCapability.Multimedia.Media.SoundPool * @since 10 */ setPriority(streamID: number, priority: number): Promise; /** * Set playback rate. * * @param {number} streamID Returned by the play() * @param {audio.AudioRendererRate} rate Playback rate * @param {AsyncCallback} callback Callback used to return the result. * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. *
2.Incorrect parameter types. 3.Parameter verification failed. Return by callback. * @throws { BusinessError } 5400102 - Operation not allowed. Return by callback. * @throws { BusinessError } 5400105 - Service died. Return by callback. * @syscap SystemCapability.Multimedia.Media.SoundPool * @since 10 */ setRate(streamID: number, rate: audio.AudioRendererRate, callback: AsyncCallback): void; /** * Set playback rate. * * @param {number} streamID Returned by the play() * @param {audio.AudioRendererRate} rate Playback rate * @returns {Promise} Promise used to return the result. * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. *
2.Incorrect parameter types. 3.Parameter verification failed. Return by promise. * @throws { BusinessError } 5400102 - Operation not allowed. Return by promise. * @throws { BusinessError } 5400105 - Service died. Return by promise. * @syscap SystemCapability.Multimedia.Media.SoundPool * @since 10 */ setRate(streamID: number, rate: audio.AudioRendererRate): Promise; /** * Set stream volume. * * @param {number} streamID Returned by the play() * @param {number} leftVolume Volume value(range = 0.0 to 1.0),current leftVolume = rightVolume * @param {number} rightVolume Volume value(range = 0.0 to 1.0),current leftVolume = rightVolume * @param {AsyncCallback} callback Callback used to return the result. * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. *
2.Incorrect parameter types. 3.Parameter verification failed. Return by callback. * @throws { BusinessError } 5400102 - Operation not allowed. Return by callback. * @throws { BusinessError } 5400105 - Service died. Return by callback. * @syscap SystemCapability.Multimedia.Media.SoundPool * @since 10 */ setVolume(streamID: number, leftVolume: number, rightVolume: number, callback: AsyncCallback): void; /** * Set stream volume. * * @param {number} streamID Returned by the play() * @param {number} leftVolume Volume value(range = 0.0 to 1.0),current leftVolume = rightVolume * @param {number} rightVolume Volume value(range = 0.0 to 1.0),current leftVolume = rightVolume * @returns {Promise} Promise used to return the result. * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. *
2.Incorrect parameter types. 3.Parameter verification failed. Return by promise. * @throws { BusinessError } 5400102 - Operation not allowed. Return by promise. * @throws { BusinessError } 5400105 - Service died. Return by promise. * @syscap SystemCapability.Multimedia.Media.SoundPool * @since 10 */ setVolume(streamID: number, leftVolume: number, rightVolume: number): Promise; /** * Unload a sound from a sound ID. * * @param {number} soundID Returned by the load() * @param {AsyncCallback} callback Callback used to return the result. * @throws { BusinessError } 5400102 - Operation not allowed. Return by callback. * @throws { BusinessError } 5400103 - I/O error. Return by callback. * @throws { BusinessError } 5400105 - Service died. Return by callback. * @syscap SystemCapability.Multimedia.Media.SoundPool * @since 10 */ unload(soundID: number, callback: AsyncCallback): void; /** * Unload a sound from a sound ID. * * @param {number} soundID Returned by the load() * @returns {Promise} Promise used to return the result. * @throws { BusinessError } 5400102 - Operation not allowed. Return by promise. * @throws { BusinessError } 5400103 - I/O error. Return by promise. * @throws { BusinessError } 5400105 - Service died. Return by promise. * @syscap SystemCapability.Multimedia.Media.SoundPool * @since 10 */ unload(soundID: number): Promise; /** * Releases the soundPool. This method uses an asynchronous callback to return the result. * * @param {AsyncCallback} callback Callback used to return the result. * @throws { BusinessError } 5400105 - Service died. Return by callback. * @syscap SystemCapability.Multimedia.Media.SoundPool * @since 10 */ release(callback: AsyncCallback): void; /** * Releases the soundPool. This method uses a promise to return the result. * * @returns {Promise} Promise used to return the result. * @throws { BusinessError } 5400105 - Service died. Return by promise. * @syscap SystemCapability.Multimedia.Media.SoundPool * @since 10 */ release(): Promise; /** * Register listens for load result event. * * @param {'loadComplete'} type Type of the play finish event to listen for. * @param {Callback} callback Callback used to listen for load result event * @syscap SystemCapability.Multimedia.Media.SoundPool * @since 10 */ on(type: 'loadComplete', callback: Callback): void; /** * Cancel Listens for load result event. * * @param {'loadComplete'} type Type of the play finish event to listen for. * @syscap SystemCapability.Multimedia.Media.SoundPool * @since 10 */ off(type: 'loadComplete'): void; /** * Register listens for play finish event. * * @param {'playFinished'} type Type of the play finish event to listen for. * @param {Callback} callback Callback used to listen for the play finish * @syscap SystemCapability.Multimedia.Media.SoundPool * @since 10 */ on(type: 'playFinished', callback: Callback): void; /** * Cancel Listens for play finish event. * * @param {'playFinished'} type of the play finish event to listen for. * @syscap SystemCapability.Multimedia.Media.SoundPool * @since 10 */ off(type: 'playFinished'): void; /** * Register listens for sound play error events. * * @param {'error'} type Type of the sound play error event to listen for. * @param {ErrorCallback} callback Callback used to listen for sound play error events. * @syscap SystemCapability.Multimedia.Media.SoundPool * @since 10 */ on(type: 'error', callback: ErrorCallback): void; /** * Cancel Listens for sound play error events. * * @param {'error'} type Type of the sound play error event to listen for. * @syscap SystemCapability.Multimedia.Media.SoundPool * @since 10 */ off(type: 'error'): void; }