Class ShadowAudioTrack

java.lang.Object
org.robolectric.shadows.ShadowAudioTrack

@Implements(value=android.media.AudioTrack.class, looseSignatures=true) public class ShadowAudioTrack extends Object
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.
  • 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 in native_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(AudioFormat format, AudioAttributes attr)
      Adds support for direct playback for the pair of AudioFormat and AudioAttributes where the format encoding must be non-PCM. Calling AudioTrack.isDirectPlaybackSupported(AudioFormat, AudioAttributes) will return true for matching AudioFormat and AudioAttributes. 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 - The AudioFormat, which must be of a non-PCM encoding. If the encoding is PCM, the method will throw an IllegalArgumentException.
      attr - The AudioAttributes.
    • clearDirectPlaybackSupportedFormats

      public static void clearDirectPlaybackSupportedFormats()
      Clears all encodings that have been added for direct playback support with addDirectPlaybackSupport(android.media.AudioFormat, android.media.AudioAttributes).
    • addAllowedNonPcmEncoding

      public static void addAllowedNonPcmEncoding(int encoding)
      Add a non-PCM encoding for which AudioTrack instances are allowed to be created.
      Parameters:
      encoding - One of AudioFormat ENCODING_ constants that represents a non-PCM encoding. If encoding is PCM, this method throws an IllegalArgumentException.
    • clearAllowedNonPcmEncodings

      public static void clearAllowedNonPcmEncodings()
      Clears all encodings that have been added with addAllowedNonPcmEncoding(int).
    • setRoutedDevice

      @RequiresApi(24) public static void setRoutedDevice(AudioDeviceInfo routedDevice)
      Sets the routed device returned from AudioTrack.getRoutedDevice() and informs all registered AudioRouting.OnRoutingChangedListener.

      Note that this affects the routed device for all AudioTrack instances.

      Parameters:
      routedDevice - The route device, or null to reset it to unknown.
    • native_get_FCC_8

      @Implementation(minSdk=24, maxSdk=28) protected static int 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_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, Parcel attributionSource, long nativeAudioTrack, boolean offload, int encapsulationMode, Object tunerConfiguration, 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 with AudioTrack.WRITE_BLOCKING. If the AudioTrack instance was created with a non-PCM encoding and the encoding can no longer be played directly, the method will return AudioTrack.ERROR_DEAD_OBJECT;
    • getRoutedDevice

      @Implementation(minSdk=24) protected AudioDeviceInfo getRoutedDevice()
    • addOnRoutingChangedListener

      @Implementation(minSdk=24) protected void addOnRoutingChangedListener(AudioRouting.OnRoutingChangedListener listener, Handler handler)
    • removeOnRoutingChangedListener

      @Implementation(minSdk=24) protected void removeOnRoutingChangedListener(AudioRouting.OnRoutingChangedListener listener)
    • setPlaybackParams

      @Implementation(minSdk=23) public void setPlaybackParams(PlaybackParams params)
    • getPlaybackParams

      @Implementation(minSdk=23) protected PlaybackParams getPlaybackParams()
    • write

      @Implementation(minSdk=21) protected int write(ByteBuffer audioData, int sizeInBytes, int writeMode)
      Returns the number of bytes to write, except with invalid parameters. If the AudioTrack was created for a non-PCM encoding that can no longer be played directly, it returns AudioTrack.ERROR_DEAD_OBJECT. Assumes AudioTrack is already initialized (object properly created). Do not block even if AudioTrack in offload mode is in STOPPING play state. This method returns immediately even with AudioTrack.WRITE_BLOCKING
    • getPlaybackHeadPosition

      @Implementation protected int getPlaybackHeadPosition()
    • flush

      @Implementation protected void flush()
    • addAudioDataListener

      public static void addAudioDataListener(ShadowAudioTrack.OnAudioDataWrittenListener listener)
      Parameters:
      listener - The ShadowAudioTrack.OnAudioDataWrittenListener to be registered.
    • removeAudioDataListener

      public static void removeAudioDataListener(ShadowAudioTrack.OnAudioDataWrittenListener listener)
      Parameters:
      listener - The ShadowAudioTrack.OnAudioDataWrittenListener to be removed.
    • resetTest

      @Resetter public static void resetTest()