public class AudioRenderer extends Object
This class manages and plays an audio resource for an application. Audio buffers are transferred to the audio receiver for playback.
SystemCapability.Multimedia#AUDIO| 限定符和类型 | 类 | 说明 |
|---|---|---|
static class |
AudioRenderer.ChannelVolume |
Specifies the volume of audio channels.
|
static class |
AudioRenderer.PlayMode |
Describes playback modes.
|
static class |
AudioRenderer.SpeedPara |
Provides parameters related to the playback speed and pitch.
|
static class |
AudioRenderer.State |
Describes player status.
|
| 限定符和类型 | 字段 | 说明 |
|---|---|---|
static int |
ERROR |
Indicates that an operation failed.
|
static int |
ERROR_INVALID_OPERATION |
Indicates a failure caused by improper use of a method.
|
static float |
MAX_PLAYBACK_SPEED |
Indicates the maximum playback speed.
|
static float |
MIN_PLAYBACK_SPEED |
Indicates the minimum playback speed.
|
static int |
RENDERER_SESSION_ID_NONE |
Indicates the default ID of an audio playback session.
|
static int |
SUCCESS |
Indicates that an operation is successful.
|
| 构造器 | 说明 |
|---|---|
AudioRenderer(AudioRendererInfo audioRendererInfo,
AudioRenderer.PlayMode pm) |
Creates an
AudioRenderer instance based on a specified AudioRendererInfo instance and
playback mode. |
AudioRenderer(AudioRendererInfo audioRendererInfo,
AudioRenderer.PlayMode pm,
AudioDeviceDescriptor outputDevice) |
Creates an
AudioRenderer instance based on a specified AudioRendererInfo instance,
playback mode, and playback device. |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
boolean |
duckVolume() |
Decreases the volume and mixes the audio with another application that has the audio focus.
|
boolean |
flush() |
Refreshes the stream data queue of the current playback.
|
AudioStreamInfo.ChannelMask |
getAudioChannel() |
Obtains channel mask information for audio playback.
|
Timestamp |
getAudioTime() |
Obtains the playback timestamp.
|
int |
getBufferFrameSize() |
Obtains the size of frames in the audio buffer for audio playback.
|
int |
getBufferUnderflowTimes() |
Obtains the number of times that the data in the audio buffer is insufficient.
|
AudioDeviceDescriptor |
getCurrentDevice() |
Obtains information about an audio playback device.
|
static float |
getMaxVolume() |
Obtains the maximum volume allowed for the stream playback.
|
static int |
getMinBufferSize(int sampleRate,
AudioStreamInfo.EncodingFormat format,
AudioStreamInfo.ChannelMask channelMask) |
Obtains the minimum buffer size required to ensure normal audio stream playback in
PlayMode#MODE_STREAM
mode. |
static float |
getMinVolume() |
Obtains the minimum volume allowed for the stream playback.
|
AudioRenderer.SpeedPara |
getPlaybackSpeed() |
Obtains the playback speed and pitch parameters.
|
int |
getPosition() |
Obtains the playback position, which is represented by the number of audio frames played.
|
AudioRendererInfo |
getRendererInfo() |
Gets the audio renderer info.
|
int |
getRendererSessionId() |
Obtains the audio rendering session ID.
|
int |
getSampleRate() |
Obtains the sampling rate.
|
AudioRenderer.State |
getState() |
Obtains audio playback status.
|
AudioStreamInfo.StreamType |
getStreamType() |
Obtains the audio stream type for the playback.
|
boolean |
pause() |
Pauses audio stream playback.
|
boolean |
release() |
Releases playback resources.
|
void |
setFrameIntervalObserver(FrameIntervalObserver observer,
int interval,
EventHandler handler) |
Sets an observer to listen for the event that the playback progress reaches a multiple of
interval. |
void |
setFramePositionObserver(FramePositionObserver observer,
int position,
EventHandler handler) |
Sets an observer to listen for the event that the playback progress reaches a specified position.
|
boolean |
setOutputDevice(AudioDeviceDescriptor info) |
Sets the audio stream playback device.
|
boolean |
setPlaybackSpeed(AudioRenderer.SpeedPara speedPara) |
Sets playback speed and pitch parameters.
|
boolean |
setPosition(int position) |
Sets the position to start audio stream playback, which is represented by frames.
|
boolean |
setSampleRate(int sampleRateInHz) |
Sets the audio stream sampling rate.
|
boolean |
setSendEffect(int effectId) |
Sets a send effect for audio playback.
|
void |
setSendEffectLevel(float level) |
Sets the level of the send effect for audio playback.
|
boolean |
setSpeed(float speed) |
Sets the playback speed.
|
boolean |
setVolume(float vol) |
Sets output volume for all audio channels.
|
boolean |
setVolume(AudioRenderer.ChannelVolume channelVolume) |
Sets output volume for an audio channel.
|
boolean |
start() |
Plays audio streams.
|
boolean |
stop() |
Stops playing audio streams.
|
boolean |
unduckVolume() |
Restores the volume.
|
boolean |
write(byte[] data,
int offset,
int size) |
Writes audio data to an audio receiver for playback in
MODE_STREAM mode, or copies the audio data for
later playback in MODE_STATIC mode. |
boolean |
write(float[] data,
int offset,
int size) |
Writes the specified type of audio data into an audio receiver for playback in
MODE_STREAM
mode, or copies the audio data for later playback in MODE_STATIC mode. |
boolean |
write(short[] data,
int offset,
int size) |
Writes audio data to an audio receiver for playback in
MODE_STREAM mode, or copies the audio data for
later playback in MODE_STATIC mode. |
boolean |
write(ByteBuffer data,
int size) |
Writes
ByteBuffer audio data into an audio receiver for playback in MODE_STREAM
mode, or copies the audio data for later playback in MODE_STATIC mode. |
public static final int ERROR
public static final int ERROR_INVALID_OPERATION
public static final float MAX_PLAYBACK_SPEED
public static final float MIN_PLAYBACK_SPEED
public static final int RENDERER_SESSION_ID_NONE
public static final int SUCCESS
public AudioRenderer(AudioRendererInfo audioRendererInfo, AudioRenderer.PlayMode pm) throws IllegalArgumentException
AudioRenderer instance based on a specified AudioRendererInfo instance and
playback mode. The playback device is the default one.audioRendererInfo - Indicates information about the audio streams to play.pm - Indicates the stream playback mode.IllegalArgumentException - Throws this exception if a parameter is invalid.public AudioRenderer(AudioRendererInfo audioRendererInfo, AudioRenderer.PlayMode pm, AudioDeviceDescriptor outputDevice) throws IllegalArgumentException
AudioRenderer instance based on a specified AudioRendererInfo instance,
playback mode, and playback device.audioRendererInfo - Indicates information about the audio streams to play.pm - Indicates the stream playback mode.outputDevice - Indicates information about the preference output device of this AudioRenderer.IllegalArgumentException - Throws this exception if a parameter is invalid.public AudioRenderer.State getState()
State.public boolean start()
To play audio streams in MODE_STATIC mode, you need to call the
write method before this method.
true if the audio streams are played; returns false otherwise.public boolean pause()
true if the playback is paused; returns false otherwise.public boolean stop()
true if the playback is stopped; returns false otherwise.public boolean release()
true if the resources are released; returns false otherwise.public static int getMinBufferSize(int sampleRate,
AudioStreamInfo.EncodingFormat format,
AudioStreamInfo.ChannelMask channelMask)
PlayMode#MODE_STREAM
mode.sampleRate - Indicates the sampling rate of the audio stream.format - Indicates the encoding format of the audio stream.channelMask - Indicates the audio channel mask.public boolean write(short[] data,
int offset,
int size)
MODE_STREAM mode, or copies the audio data for
later playback in MODE_STATIC mode.
This is a synchronous method that returns the result until all the data is written. You can set
a short integer array to write audio data for both MODE_STREAM and MODE_STATIC playback.
data - Indicates data to be written.offset - Indicates the offset (in shorts), which defines the beginning placement to write the data.size - Indicates the shorts of data to be written.true if the data is written; returns false otherwise.public boolean write(byte[] data,
int offset,
int size)
MODE_STREAM mode, or copies the audio data for
later playback in MODE_STATIC mode.
You can set a byte array to write audio data for both MODE_STREAM and MODE_STATIC playback.
data - Indicates data to be written.offset - Indicates the offset (in bytes), which defines the beginning placement to write the data.size - Indicates the bytes of data to be written.true if the data is written; returns false otherwise.public boolean write(float[] data,
int offset,
int size)
MODE_STREAM
mode, or copies the audio data for later playback in MODE_STATIC mode.
This is a synchronous method that returns the result until all the data is written. You can set
a float array to write audio data for both MODE_STREAM and MODE_STATIC playback.
data - Indicates the data to be written.offset - Indicates the offset into which the data will be written.size - Indicates the size of the float data to be written.true if the data is written; returns false otherwise.public boolean write(ByteBuffer data, int size)
ByteBuffer audio data into an audio receiver for playback in MODE_STREAM
mode, or copies the audio data for later playback in MODE_STATIC mode.
You can set a ByteBuffer instance to write audio data for both MODE_STREAM and
MODE_STATIC playback.
data - Indicates the data to be written.size - Indicates the size of the ByteBuffer data to be written.true if the data is written; returns false otherwise.public boolean setVolume(float vol)
You can use vol to set the same volume for both left and right channels.
vol - Indicates the output volume.true if the volume is set; returns false otherwise.public boolean setVolume(AudioRenderer.ChannelVolume channelVolume)
This method sets left and right channel volume based on the value defined in ChannelVolume.
channelVolume - Indicates the channel volume defined in ChannelVolume.true if the volume is set; returns false otherwise.public boolean setPosition(int position)
PlayMode#MODE_STATIC mode when the playback is stopped or paused.position - Indicates the position to start audio stream playback, which is represented by frames.true if the setting is successful; returns false otherwise.public boolean setSpeed(float speed)
speed - Indicates the playback speed, which must be greater than 0.0f.true if the setting is successful; returns false otherwise.public boolean setPlaybackSpeed(AudioRenderer.SpeedPara speedPara)
speedPara - Indicates the speed and pitch parameters defined in the SpeedPara instance.true if the setting is successful; returns false otherwise.public boolean setSampleRate(int sampleRateInHz)
sampleRateInHz - Indicates the sampling rate, which must be an integer greater than 0.true if the setting is successful; returns false otherwise.public boolean setOutputDevice(AudioDeviceDescriptor info)
info - Indicates the AudioDeviceDescriptor instance describing the device information to be set.
.true if the setting is successful; returns false otherwise.public AudioDeviceDescriptor getCurrentDevice()
The audio playback device is set using the
AudioRenderer#AudioRenderer(AudioRendererInfo, PlayMode, AudioDeviceDescriptor) method.
null otherwise.public int getRendererSessionId()
AudioRenderer belongs to.public int getSampleRate()
public int getPosition()
The number of frames is a non-negative int value that continuously increases.
It will be wrapped periodically, for example, once every 27 hours around for playback
of 44.1 kHz audio streams. After the number of frames exceeds the value range of an int,
the value will be reset to 0.
public Timestamp getAudioTime()
You need to ensure that the timestamp is obtained during the audio stream playback. You can call
getState() to check the playback state. In non-playback state, you should obtain the
new timestamp periodically until you get the playback timestamp.
Frequent calling of this method will cause low efficiency. The recommended frequency is no more than once every 15 seconds.
Timestamp if obtained; returns a Timestamp
with its member variables set to 0 otherwise.public boolean flush()
Any data that has been written but not yet consumed will be discarded. This method is valid only
for the playback in PlayMode#MODE_STREAM mode and when the playback is stopped or paused.
true if the operation is successful; returns false otherwise.public AudioRenderer.SpeedPara getPlaybackSpeed()
SpeedPara instance containing the speed and pitch parameters.public static float getMaxVolume()
public static float getMinVolume()
public AudioStreamInfo.StreamType getStreamType()
StreamType.public AudioRendererInfo getRendererInfo()
AudioRendererInfo.public int getBufferFrameSize()
public boolean setSendEffect(int effectId)
effectId - Identifies the send effect to set.true if the setting is successful; returns false otherwise.public AudioStreamInfo.ChannelMask getAudioChannel()
AudioStreamInfo.ChannelMask object.public int getBufferUnderflowTimes()
public void setSendEffectLevel(float level)
level - Indicates the effect level to set. The value ranges from 0.0 to 1.0.public void setFramePositionObserver(FramePositionObserver observer, int position, EventHandler handler)
observer - Indicates the observer to set.position - Indicates the playback position.handler - Indicates the event handler to invoke the observer. If you do not specify a handler, the
default one will be used.public void setFrameIntervalObserver(FrameIntervalObserver observer, int interval, EventHandler handler)
interval.observer - Indicates the observer to set.interval - Indicates the audio frame interval.handler - Indicates the event handler to invoke the observer. If you do not specify a handler, the
default one will be used.public boolean duckVolume()
unduckVolume() to restore the volume.
This method is called when your application is notified by audio focus callback
AudioInterrupt.InterruptListener.onInterrupt(int, int),
with input parameters type and hint set to AudioInterrupt.INTERRUPT_TYPE_BEGIN
and AudioInterrupt.INTERRUPT_HINT_NONE, respectively.
true if the volume is decreased; returns false otherwise.public boolean unduckVolume()
duckVolume().
This method is called when your application is notified by audio focus callback
AudioInterrupt.InterruptListener.onInterrupt(int, int),
with input parameters type and hint set to AudioInterrupt.INTERRUPT_TYPE_END
and AudioInterrupt.INTERRUPT_HINT_RESUME, respectively.
true if the volume is restored; returns false otherwise.