Package org.robolectric.shadows
Class ShadowAudioTrack
java.lang.Object
org.robolectric.shadows.ShadowAudioTrack
Implementation of a couple methods in
AudioTrack. Only a couple methods are supported,
other methods are expected run through the real class. The two AudioTrack.WriteMode are treated the
same.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceListeners to be notified when data is written to anAudioTrackviaAudioTrack.write(ByteBuffer, int, int) -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddAllowedNonPcmEncoding(int encoding) Add a non-PCM encoding for whichAudioTrackinstances are allowed to be created.static voidRegisters anShadowAudioTrack.OnAudioDataWrittenListenerto theShadowAudioTrack.static voidaddDirectPlaybackSupport(AudioFormat format, AudioAttributes attr) Adds support for direct playback for the pair ofAudioFormatandAudioAttributeswhere the format encoding must be non-PCM.protected voidaddOnRoutingChangedListener(AudioRouting.OnRoutingChangedListener listener, Handler handler) static voidClears all encodings that have been added withaddAllowedNonPcmEncoding(int).static voidClears all encodings that have been added for direct playback support withaddDirectPlaybackSupport(android.media.AudioFormat, android.media.AudioAttributes).static voidenableIllegalStateOnPlay(boolean enabled) Simulates anAudioTrackIllegalStateExceptionwhile playing.protected voidflush()protected intprotected PlaybackParamsprotected AudioDeviceInfoprotected intprotected static intprotected intReturns the estimated latency of thisAudioTrack, in milliseconds.protected static intnative_get_min_buff_size(int sampleRateInHz, int channelConfig, int audioFormat) Returns a predefined or default minimum buffer size.protected intprotected static booleannative_is_direct_output_supported(int encoding, int sampleRate, int channelMask, int channelIndexMask, int contentType, int usage, int flags) protected intnative_setup(Object audioTrack, Object attributes, int[] sampleRate, int channelMask, int channelIndexMask, int audioFormat, int buffSizeInBytes, int mode, int[] sessionId, long nativeAudioTrack) protected intnative_setup(Object audioTrack, Object attributes, int[] sampleRate, int channelMask, int channelIndexMask, int audioFormat, int buffSizeInBytes, int mode, int[] sessionId, long nativeAudioTrack, boolean offload) protected intnative_setup(Object audioTrack, Object attributes, int[] sampleRate, int channelMask, int channelIndexMask, int audioFormat, int buffSizeInBytes, int mode, int[] sessionId, long nativeAudioTrack, boolean offload, int encapsulationMode, Object tunerConfiguration) protected intnative_setup(Object audioTrack, Object attributes, int[] sampleRate, int channelMask, int channelIndexMask, int audioFormat, int buffSizeInBytes, int mode, int[] sessionId, long nativeAudioTrack, boolean offload, int encapsulationMode, Object tunerConfiguration, String opPackageName) protected intnative_setup(Object audioTrack, Object attributes, int[] sampleRate, int channelMask, int channelIndexMask, int audioFormat, int buffSizeInBytes, int mode, int[] sessionId, Parcel attributionSource, long nativeAudioTrack, boolean offload, int encapsulationMode, Object tunerConfiguration, String opPackageName) protected intnative_setup(Object audioTrack, Object attributes, int sampleRate, int channelMask, int channelIndexMask, int audioFormat, int buffSizeInBytes, int mode, int[] sessionId) protected intnative_write_byte(byte[] audioData, int offsetInBytes, int sizeInBytes, int format, boolean isBlocking) Returns the number of bytes to write.protected intnative_write_native_bytes(Object audioData, int positionInBytes, int sizeInBytes, int format, boolean blocking) protected intnative_write_native_bytes(ByteBuffer audioData, int positionInBytes, int sizeInBytes, int format, boolean blocking) protected voidplay()static voidRemoves anShadowAudioTrack.OnAudioDataWrittenListenerfrom theShadowAudioTrack.protected voidstatic voidvoidsetLatency(int latencyMs) Sets the estimated latency of thisAudioTrackthat will be returned byAudioTrack.getLatency(), in milliseconds.static voidsetMinBufferSize(int bufferSize) In the real class, the minimum buffer size is estimated from audio sample rate and other factors.voidsetPlaybackParams(PlaybackParams params) static voidsetRoutedDevice(AudioDeviceInfo routedDevice) Sets the routed device returned fromAudioTrack.getRoutedDevice()and informs all registeredAudioRouting.OnRoutingChangedListener.
-
Field Details
-
DEFAULT_MIN_BUFFER_SIZE
protected static final int DEFAULT_MIN_BUFFER_SIZE- See Also:
-
-
Constructor Details
-
ShadowAudioTrack
public ShadowAudioTrack()
-
-
Method Details
-
setMinBufferSize
public static void setMinBufferSize(int bufferSize) In the real class, the minimum buffer size is estimated from audio sample rate and other factors. We do not provide such estimation innative_get_min_buff_size(int, int, int), instead letting users set the minimum for the expected audio sample. Usually higher sample rate requires bigger buffer size. -
addDirectPlaybackSupport
public static void addDirectPlaybackSupport(@Nonnull AudioFormat format, @Nonnull AudioAttributes attr) Adds support for direct playback for the pair ofAudioFormatandAudioAttributeswhere the format encoding must be non-PCM. CallingAudioTrack.isDirectPlaybackSupported(AudioFormat, AudioAttributes)will returntruefor matchingAudioFormatandAudioAttributes. The matching is performed against the format's encoding, sample rate, channel mask and channel index mask, and the attribute's content type, usage and flags.- Parameters:
format- TheAudioFormat, which must be of a non-PCM encoding. If the encoding is PCM, the method will throw anIllegalArgumentException.attr- TheAudioAttributes.
-
clearDirectPlaybackSupportedFormats
public static void clearDirectPlaybackSupportedFormats()Clears all encodings that have been added for direct playback support withaddDirectPlaybackSupport(android.media.AudioFormat, android.media.AudioAttributes). -
addAllowedNonPcmEncoding
public static void addAllowedNonPcmEncoding(int encoding) Add a non-PCM encoding for whichAudioTrackinstances are allowed to be created.- Parameters:
encoding- One ofAudioFormatENCODING_constants that represents a non-PCM encoding. Ifencodingis PCM, this method throws anIllegalArgumentException.
-
clearAllowedNonPcmEncodings
public static void clearAllowedNonPcmEncodings()Clears all encodings that have been added withaddAllowedNonPcmEncoding(int). -
setRoutedDevice
Sets the routed device returned fromAudioTrack.getRoutedDevice()and informs all registeredAudioRouting.OnRoutingChangedListener.Note that this affects the routed device for all
AudioTrackinstances.- Parameters:
routedDevice- The route device, or null to reset it to unknown.
-
native_get_FCC_8
-
native_is_direct_output_supported
@Implementation(minSdk=29) protected static boolean native_is_direct_output_supported(int encoding, int sampleRate, int channelMask, int channelIndexMask, int contentType, int usage, int flags) -
native_get_min_buff_size
@Implementation protected static int native_get_min_buff_size(int sampleRateInHz, int channelConfig, int audioFormat) Returns a predefined or default minimum buffer size. Audio format and config are neglected. -
native_get_native_frame_count
-
native_get_buffer_size_frames
-
native_setup
@Implementation(minSdk=23, maxSdk=23) protected int native_setup(Object audioTrack, Object attributes, int sampleRate, int channelMask, int channelIndexMask, int audioFormat, int buffSizeInBytes, int mode, int[] sessionId) -
native_setup
@Implementation(minSdk=24, maxSdk=27) protected int native_setup(Object audioTrack, Object attributes, int[] sampleRate, int channelMask, int channelIndexMask, int audioFormat, int buffSizeInBytes, int mode, int[] sessionId, long nativeAudioTrack) -
native_setup
@Implementation(minSdk=28, maxSdk=29) protected int native_setup(Object audioTrack, Object attributes, int[] sampleRate, int channelMask, int channelIndexMask, int audioFormat, int buffSizeInBytes, int mode, int[] sessionId, long nativeAudioTrack, boolean offload) -
native_setup
@Implementation(minSdk=30, maxSdk=30) protected int native_setup(Object audioTrack, Object attributes, int[] sampleRate, int channelMask, int channelIndexMask, int audioFormat, int buffSizeInBytes, int mode, int[] sessionId, long nativeAudioTrack, boolean offload, int encapsulationMode, Object tunerConfiguration) -
native_setup
@Implementation(minSdk=31, maxSdk=33) protected int native_setup(Object audioTrack, Object attributes, int[] sampleRate, int channelMask, int channelIndexMask, int audioFormat, int buffSizeInBytes, int mode, int[] sessionId, long nativeAudioTrack, boolean offload, int encapsulationMode, Object tunerConfiguration, String opPackageName) -
native_setup
@Implementation(minSdk=34) protected int native_setup(Object audioTrack, Object attributes, int[] sampleRate, int channelMask, int channelIndexMask, int audioFormat, int buffSizeInBytes, int mode, int[] sessionId, @Nonnull Parcel attributionSource, long nativeAudioTrack, boolean offload, int encapsulationMode, Object tunerConfiguration, @Nonnull String opPackageName) -
native_write_byte
@Implementation(minSdk=23) protected int native_write_byte(byte[] audioData, int offsetInBytes, int sizeInBytes, int format, boolean isBlocking) Returns the number of bytes to write. This method returns immediately even withAudioTrack.WRITE_BLOCKING. If theAudioTrackinstance was created with a non-PCM encoding and the encoding can no longer be played directly, the method will returnAudioTrack.ERROR_DEAD_OBJECT; -
native_write_native_bytes
@Implementation(minSdk=23, maxSdk=28) protected int native_write_native_bytes(Object audioData, int positionInBytes, int sizeInBytes, int format, boolean blocking) -
native_write_native_bytes
@Implementation(minSdk=29) protected int native_write_native_bytes(ByteBuffer audioData, int positionInBytes, int sizeInBytes, int format, boolean blocking) -
getRoutedDevice
-
addOnRoutingChangedListener
@Implementation(minSdk=24) protected void addOnRoutingChangedListener(@Nonnull AudioRouting.OnRoutingChangedListener listener, Handler handler) -
removeOnRoutingChangedListener
@Implementation(minSdk=24) protected void removeOnRoutingChangedListener(@Nonnull AudioRouting.OnRoutingChangedListener listener) -
setPlaybackParams
-
setLatency
public void setLatency(int latencyMs) Sets the estimated latency of thisAudioTrackthat will be returned byAudioTrack.getLatency(), in milliseconds. -
native_get_latency
Returns the estimated latency of thisAudioTrack, in milliseconds. -
getPlaybackParams
-
getPlaybackHeadPosition
-
play
-
flush
-
addAudioDataListener
Registers anShadowAudioTrack.OnAudioDataWrittenListenerto theShadowAudioTrack.- Parameters:
listener- TheShadowAudioTrack.OnAudioDataWrittenListenerto be registered.
-
removeAudioDataListener
Removes anShadowAudioTrack.OnAudioDataWrittenListenerfrom theShadowAudioTrack.- Parameters:
listener- TheShadowAudioTrack.OnAudioDataWrittenListenerto be removed.
-
enableIllegalStateOnPlay
public static void enableIllegalStateOnPlay(boolean enabled) Simulates anAudioTrackIllegalStateExceptionwhile playing. -
resetTest
-