Android Guides | Samples

Android.Media.AudioTrack Class

The AudioTrack class manages and plays a single audio resource for Java applications.

See Also: AudioTrack

Syntax

[Android.Runtime.Register("android/media/AudioTrack", DoNotGenerateAcw=true)]
public class AudioTrack : Object

Remarks

The AudioTrack class manages and plays a single audio resource for Java applications. It allows streaming of PCM audio buffers to the audio sink for playback. This is achieved by "pushing" the data to the AudioTrack object using one of the AudioTrack.Write(Byte[],Int32,Int32), AudioTrack.Write(Int16[],Int32,Int32), and AudioTrack.Write(Single[],Int32,Int32,Int32) methods.

An AudioTrack instance can operate under two modes: static or streaming.
In Streaming mode, the application writes a continuous stream of data to the AudioTrack, using one of the write() methods. These are blocking and return when the data has been transferred from the Java layer to the native layer and queued for playback. The streaming mode is most useful when playing blocks of audio data that for instance are:

  • too big to fit in memory because of the duration of the sound to play,
  • too big to fit in memory because of the characteristics of the audio data (high sampling rate, bits per sample ...)
  • received or generated while previously queued audio is playing.
The static mode should be chosen when dealing with short sounds that fit in memory and that need to be played with the smallest latency possible. The static mode will therefore be preferred for UI and game sounds that are played often, and with the smallest overhead possible.

Upon creation, an AudioTrack object initializes its associated audio buffer. The size of this buffer, specified during the construction, determines how long an AudioTrack can play before running out of data.
For an AudioTrack using the static mode, this size is the maximum size of the sound that can be played from it.
For the streaming mode, data will be written to the audio sink in chunks of sizes less than or equal to the total buffer size. AudioTrack is not final and thus permits subclasses, but such use is not recommended.

[Android Documentation]

Requirements

Namespace: Android.Media
Assembly: Mono.Android (in Mono.Android.dll)
Assembly Versions: 0.0.0.0
Since: Added in API level 3

The members of Android.Media.AudioTrack are listed below.

See Also: Object

Public Constructors

Protected Constructors

A constructor used when creating managed representations of JNI objects; called by the runtime.

Public Fields

const
ErrorTrackStatus (-1). Denotes a generic operation failure.
const
ErrorBadValueTrackStatus (-2). Denotes a failure due to the use of an invalid value.
const
ErrorInvalidOperationTrackStatus (-3). Denotes a failure due to the improper use of a method.
const
SuccessTrackStatus (0). Denotes a successful operation.

Public Properties

[read-only]
AudioFormatEncoding. Returns the configured audio data encoding.
[read-only]
AudioSessionIdInt32. Returns the audio session ID.
[read-only]
ChannelConfigurationChannelOut. Returns the configured channel position mask.
[read-only]
ChannelCountInt32. Returns the configured number of channels.
[read-only]
static
MaxVolumeSingle. Returns the maximum gain value, which is greater than or equal to 1.
[read-only]
static
MinVolumeSingle. Returns the minimum gain value, which is the constant 0.
[read-only]
NotificationMarkerPositionInt32. Returns marker position expressed in frames.
[read-only]
PlaybackHeadPositionInt32. Returns the playback head position expressed in frames.
[read-only]
PlaybackRateInt32. Returns the current playback sample rate rate in Hz.
[read-only]
PlayStatePlayState. Returns the playback state of the AudioTrack instance.
[read-only]
PositionNotificationPeriodInt32. Returns the notification update period expressed in frames.
[read-only]
SampleRateInt32. Returns the configured audio data sample rate in Hz
[read-only]
StateAudioTrackState. Returns the state of the AudioTrack instance.
[read-only]
StreamTypeStream. Returns the type of audio stream this AudioTrack is configured for.

Protected Properties

[read-only]
NativeFrameCountInt32. Returns the frame count of the native AudioTrack buffer.
[read-only]
override
ThresholdClassIntPtr. This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.
[read-only]
override
ThresholdTypeType. This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

Public Methods

AttachAuxEffect(Int32) : TrackStatus
Attaches an auxiliary effect to the audio track.
Flush()
Flushes the audio data currently queued for playback.
static
GetMinBufferSize(Int32, ChannelOut, Encoding) : Int32
Returns the minimum buffer size required for the successful creation of an AudioTrack object to be created in the AudioTrack.MODE_STREAM mode.
static
GetNativeOutputSampleRate(Stream) : Int32
Returns the output sample rate in Hz for the specified stream type.
GetTimestamp(AudioTimestamp) : Boolean
Poll for a timestamp on demand.
Pause()
Pauses the playback of the audio data.
Play()
Starts playing an AudioTrack.
Release()
Releases the native AudioTrack resources.
ReloadStaticData() : TrackStatus
Sets the playback head position within the static buffer to zero, that is it rewinds to start of static buffer.
SetAuxEffectSendLevel(Single) : TrackStatus
Sets the send level of the audio track to the attached auxiliary effect AudioTrack.AttachAuxEffect(Int32).
SetLoopPoints(Int32, Int32, Int32) : TrackStatus
Sets the loop points and the loop count.
SetNotificationMarkerPosition(Int32) : TrackStatus
Sets the position of the notification marker.
SetPlaybackHeadPosition(Int32) : TrackStatus
Sets the playback head position within the static buffer.
SetPlaybackPositionUpdateListener(AudioTrack+IOnPlaybackPositionUpdateListener)
Sets the listener the AudioTrack notifies when a previously set marker is reached or for each periodic playback head position update.
SetPlaybackPositionUpdateListener(AudioTrack+IOnPlaybackPositionUpdateListener, Handler)
Sets the listener the AudioTrack notifies when a previously set marker is reached or for each periodic playback head position update.
SetPlaybackRate(Int32) : Int32
Sets the playback sample rate for this track.
SetPositionNotificationPeriod(Int32) : TrackStatus
Sets the period for the periodic notification event.
SetStereoVolume(Single, Single) : TrackStatus
Sets the specified left and right output gain values on the AudioTrack.
SetVolume(Single) : TrackStatus
Sets the specified output gain value on all channels of this track.
Stop()
Stops playing the audio data.
Write(ByteBuffer, Int32, WriteMode) : Int32
Writes the audio data to the audio sink for playback (streaming mode), or copies audio data for later playback (static buffer mode).
Write(Byte[], Int32, Int32) : Int32
Writes the audio data to the audio sink for playback (streaming mode), or copies audio data for later playback (static buffer mode).
Write(Int16[], Int32, Int32) : Int32
Writes the audio data to the audio sink for playback (streaming mode), or copies audio data for later playback (static buffer mode).
Write(Single[], Int32, Int32, WriteMode) : Int32
Writes the audio data to the audio sink for playback (streaming mode), or copies audio data for later playback (static buffer mode).
WriteAsync(ByteBuffer, Int32, WriteMode) : System.Threading.Tasks.Task<int>
WriteAsync(Byte[], Int32, Int32) : System.Threading.Tasks.Task<int>
WriteAsync(Int16[], Int32, Int32) : System.Threading.Tasks.Task<int>
WriteAsync(Single[], Int32, Int32, WriteMode) : System.Threading.Tasks.Task<int>

Protected Methods

SetState(Int32)
Sets the initialization state of the instance.

Public Events