public class SoundPlayer extends Object
This class provides applications with the createSound and play interfaces at the kit layer to preload
and play audio resources, including sounds and tones.
SystemCapability.Multimedia#AUDIO| 限定符和类型 | 类 | 说明 |
|---|---|---|
static class |
SoundPlayer.AudioVolumes |
Sets the volume of audio channels.
|
static interface |
SoundPlayer.OnCreateCompleteListener |
Listener used to monitor the successful creation of sound resources,
using in
setOnCreateCompleteListener. |
static class |
SoundPlayer.SoundPlayerParameters |
Provides
SoundPlayer parameters for a playback task. |
static class |
SoundPlayer.SoundType |
Indicates the system sound type.
|
| 构造器 | 说明 |
|---|---|
SoundPlayer() |
Default constructor used for tone playback.
|
SoundPlayer(int taskType) |
A constructor used to create a
SoundPlayer instance. |
SoundPlayer(String packageName) |
A constructor used to create a
SoundPlayer instance. |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
int |
createSound(FileDescriptor fd,
long offset,
long length) |
Creates and loads sound data from a file based on its descriptor.
|
int |
createSound(String path) |
Creates and loads sound data from a specified path.
|
int |
createSound(String path,
AudioRendererInfo rendererInfo) |
Creates a sound based on the audio data and playback information loaded from a specified path.
|
int |
createSound(Context context,
int resourceId) |
Creates and loads sound data based on the application context and audio resource ID.
|
int |
createSound(BaseFileDescriptor baseFileDescriptor) |
Creates and loads sound data from the specified
BaseFileDescriptor instance. |
boolean |
createSound(AudioStreamInfo.StreamType streamType,
float volume) |
Creates a dual-tone multiple-frequency tone with specified stream type and tone volume.
|
boolean |
createSound(ToneDescriptor.ToneType type,
int durationMs) |
Creates a dual-tone multiple-frequency (DTMF) tone with specified tone frequency description and duration (ms).
|
boolean |
deleteSound(int soundId) |
Deletes a sound based on its ID and releases the resources occupied.
|
boolean |
pause() |
Pauses the playback of a tone.
|
boolean |
pause(int taskId) |
Pauses the playback of a sound based on the task ID.
|
boolean |
pauseAll() |
Pauses all ongoing playback tasks.
|
boolean |
play() |
Plays a created tone.
|
int |
play(int soundId) |
Plays a sound using the default parameters.
|
int |
play(int soundId,
SoundPlayer.SoundPlayerParameters parameters) |
Plays a sound using specified parameters.
|
boolean |
play(ToneDescriptor.ToneType toneType,
int durationMs) |
Plays a created tone with toneType and tone duration millisecond.
|
boolean |
playSound(SoundPlayer.SoundType type) |
Plays a sound in a special effect.
|
boolean |
playSound(SoundPlayer.SoundType type,
float volume) |
Plays the sound in a special effect at a specified volume.
|
boolean |
release() |
Releases resources used for tone playback.
|
boolean |
resume(int taskId) |
Resumes a sound playback task.
|
boolean |
resumeAll() |
Resumes all paused tasks.
|
boolean |
setLoop(int taskId,
int loopNum) |
Sets the loop number of a sound playback task.
|
boolean |
setOnCreateCompleteListener(SoundPlayer.OnCreateCompleteListener listener) |
Sets the callback that listens for the completion of sound creation.
|
boolean |
setOnCreateCompleteListener(SoundPlayer.OnCreateCompleteListener listener,
boolean isDiscarded) |
Sets the callback that listens for the completion of sound creation, and determines whether to
discard original messages in the queue based on the specified
isDiscarded. |
boolean |
setPlaySpeedRate(int taskId,
float speedRate) |
Sets the playback speed of a sound playback task.
|
boolean |
setPriority(int taskId,
int priority) |
Sets the priority of a sound playback task.
|
boolean |
setVolume(int taskId,
float volume) |
Sets the volume of all audio channels for a sound playback task.
|
boolean |
setVolume(int taskId,
SoundPlayer.AudioVolumes audioVolumes) |
Sets the volume of an audio channel for a sound playback task.
|
boolean |
stop(int taskId) |
Stops a sound playback task.
|
public SoundPlayer()
It is a no-argument constructor and is used only for playing tones.
public SoundPlayer(String packageName)
SoundPlayer instance.
An instance of this class is created based on the name of an application installation package.
packageName - Indicates the name of an application installation package.public SoundPlayer(int taskType)
SoundPlayer instance.
An instance of this class is created based on the type of an audio task.
taskType - Indicates the audio task type, which is specified by AudioManager.AudioVolumeType.public boolean playSound(SoundPlayer.SoundType type)
type - Indicates the sound effect type, which is specified by SoundType.true if the sound is played in a special effect; returns false otherwise.public boolean playSound(SoundPlayer.SoundType type, float volume)
type - Indicates the sound effect type, which is specified by SoundType.volume - Indicates the volume of the sound effect.true if the sound is played in a special effect at a specified volume;
returns false otherwise.public final boolean createSound(ToneDescriptor.ToneType type, int durationMs)
You can use this method to create a tone, for example, a dial tone.
type - Indicates the tone type, which is specified by ToneDescriptor.ToneType.durationMs - Indicates the duration of the tone, in ms.true if the DTMF tone is created; returns false otherwise.public final boolean createSound(AudioStreamInfo.StreamType streamType, float volume)
streamType - Indicates the stream type for tone playing, defined by AudioStreamInfo.StreamType.volume - Indicates the volume of tone, the value ranges from 0.0 to 1.0.true if the tone is created; returns false otherwise.public final int createSound(String path)
You can use this method to create a sound, for example, knocking or footsteps.
path - Indicates the path for creating a sound.public final int createSound(BaseFileDescriptor baseFileDescriptor)
BaseFileDescriptor instance.
You can use this method to create a sound, for example, of knocking or footsteps.
baseFileDescriptor - Indicates the BaseFileDescriptor instance.public final int createSound(Context context, int resourceId)
You can use this method to create a sound, for example, of knocking or footsteps.
context - Indicates the application context.resourceId - Indicates the audio resource ID, for example, Resource.Raw.knocking.public final int createSound(FileDescriptor fd, long offset, long length)
You can use this method to create a sound, for example, of knocking or footsteps.
fd - Indicates the file descriptor.offset - Indicates the offset form which the data will be loaded.length - Indicates the size of the file.public final int createSound(String path, AudioRendererInfo rendererInfo)
You can set only the AudioStreamInfo#streamUsage, AudioStreamInfo#contentType, and
AudioStreamInfo#streamFlag parameters in the AudioRendererInfo object. Other information,
such as AudioRendererInfo#bufferSizeInBytes and
AudioStreamInfo#sampleRate, loaded from the specified file cannot be configured.
path - Indicates the path from which the data is loaded.rendererInfo - Indicates the playback parameters. You can set some of them to implement specific functions,
for example, setting the sound usage type.0 otherwise.public final boolean deleteSound(int soundId)
You can use this method to release resources occupied by the sound in the case of insufficient resources.
soundId - Indicates the sound ID.true if the resources are released; returns false otherwise.public final boolean play(ToneDescriptor.ToneType toneType, int durationMs)
You can use SoundPlayer#createSound(AudioStreamInfo.StreamType, float) to create a tone,
and then invoke this method to play it.
toneType - Indicates the tone type, which is specified by ToneDescriptor.ToneType.durationMs - Indicates the duration millisecond of the tone (infinite duration time = -1).true if the tone is played; returns false otherwise.public final boolean play()
You can use SoundPlayer#createSound(ToneDescriptor.ToneType, int) to create a tone,
and then invoke this method to play it.
true if the tone is played; returns false otherwise.public final int play(int soundId)
soundId - Indicates the ID of the sound to be played.true if the sound is played; returns false otherwise.public final int play(int soundId,
SoundPlayer.SoundPlayerParameters parameters)
soundId - Indicates the ID of the sound to be played.parameters - Indicates the playback parameters, such as the playback priority, playback speed,
and loop number.true if the sound is played; returns false otherwise.public final boolean pause()
true if the tone playback is paused; returns false otherwise.public final boolean pause(int taskId)
taskId - Indicates the ID of a playback task.true if the sound playback is paused; returns false otherwise.public final boolean resume(int taskId)
taskId - Indicates the ID of a playback task, which is the return value of SoundPlayer#play(int).true if the playback task is resumed; returns false otherwise.public final boolean stop(int taskId)
taskId - Indicates the ID of a playback task, which is the return value of SoundPlayer#play(int).true if the setting is successful; returns false otherwise.public final boolean release()
You can use this method to release resources occupied by the tone in the case of insufficient resources.
true if the tone resources are released; returns false otherwise.public final boolean setVolume(int taskId,
SoundPlayer.AudioVolumes audioVolumes)
taskId - Indicates the ID of a playback task, which is the return value of SoundPlayer#play(int).audioVolumes - Indicates the volume of an audio channel. The value ranges from 0.0 to 1.0.
The value 0.0 indicates that the audio channel is muted, and the value 1.0 indicates
the maximum volume of the audio channel. The audio channels include the left, right, and surround channels.true if the setting is successful; returns false otherwise.public final boolean setVolume(int taskId,
float volume)
taskId - Indicates the ID of a playback task, which is the return value of SoundPlayer#play(int).volume - Indicates the volume of a playback task. The value ranges from 0.0 to 1.0.
The value 0.0 means that all the audio channels are muted, and the value 1.0 means
the maximum volume of all the audio channels.true if the setting is successful; returns false otherwise.public final boolean setPriority(int taskId,
int priority)
taskId - Indicates the ID of a playback task, which is the return value of SoundPlayer#play(int).priority - Indicates the priority. The value 0 means that the sound will be played immediately,
and the value 1 means that the sound will be played at a later time.true if the setting is successful; returns false otherwise.public final boolean setPlaySpeedRate(int taskId,
float speedRate)
taskId - Indicates the ID of a playback task, which is the return value of SoundPlayer#play(int).speedRate - Indicates the playback speed. The value is a floating-point number ranging from 0.5 to 2.0.
The value 1.0 indicates normal playback speed.true if the setting is successful; returns false otherwise.public final boolean setLoop(int taskId,
int loopNum)
You can use this method to set the number of times that a sound will be played.
taskId - Indicates the ID of a playback task, which is the return value of SoundPlayer#play(int).loopNum - Indicates the loop number. The value is an integer greater than or equal to -1.
The value 0 indicates that the sound does not loop, and the value -1 indicates that
the sound loops without an end.true if the setting is successful; returns false otherwise.public final boolean pauseAll()
true if all tasks are paused; returns false otherwise.public final boolean resumeAll()
true if all paused tasks are resumed; returns false otherwise.public boolean setOnCreateCompleteListener(SoundPlayer.OnCreateCompleteListener listener)
createSound() is an asynchronous method, which immediately returns upon calling. You can
set the OnCreateCompleteListener to receive the notification, and then specify subsequent
operations in onCreateComplete provided by OnCreateCompleteListener.
listener - Indicates the OnCreateCompleteListener instance set for the completion of
sound creation.true if the setting is successful; returns false otherwise.public boolean setOnCreateCompleteListener(SoundPlayer.OnCreateCompleteListener listener, boolean isDiscarded)
isDiscarded.listener - Indicates the OnCreateCompleteListener instance set for the completion of
sound creation.isDiscarded - Specifies whether the original messages in the queue will be discarded. value
true indicates the messages will be discarded, and false indicates the opposite.true if the setting is successful; returns false otherwise.