Udostępnij za pośrednictwem


AudioTrack Class

Definition

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

[Android.Runtime.Register("android/media/AudioTrack", DoNotGenerateAcw=true)]
public class AudioTrack : Java.Lang.Object, Android.Media.IAudioRouting, Android.Media.IVolumeAutomation, IDisposable, Java.Interop.IJavaPeerable
[<Android.Runtime.Register("android/media/AudioTrack", DoNotGenerateAcw=true)>]
type AudioTrack = class
    inherit Object
    interface IAudioRouting
    interface IJavaObject
    interface IDisposable
    interface IJavaPeerable
    interface IVolumeAutomation
Inheritance
AudioTrack
Attributes
Implements

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 #write(byte[], int, int), #write(short[], int, int), and #write(float[], int, int, int) methods.

An AudioTrack instance can operate under two modes: static or streaming.<br> 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:

<ul> <li>too big to fit in memory because of the duration of the sound to play,</li> <li>too big to fit in memory because of the characteristics of the audio data (high sampling rate, bits per sample ...)</li> <li>received or generated while previously queued audio is playing.</li> </ul>

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.<br> For an AudioTrack using the static mode, this size is the maximum size of the sound that can be played from it.<br> 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.

Java documentation for android.media.AudioTrack.

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Constructors

AudioTrack(AudioAttributes, AudioFormat, Int32, AudioTrackMode, Int32)

Class constructor with AudioAttributes and AudioFormat.

AudioTrack(IntPtr, JniHandleOwnership)

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

AudioTrack(Stream, Int32, ChannelConfiguration, Encoding, Int32, AudioTrackMode, Int32)
Obsolete.

Class constructor with audio session.

AudioTrack(Stream, Int32, ChannelConfiguration, Encoding, Int32, AudioTrackMode)
Obsolete.

Class constructor.

AudioTrack(Stream, Int32, ChannelOut, Encoding, Int32, AudioTrackMode, Int32)

Class constructor with audio session.

AudioTrack(Stream, Int32, ChannelOut, Encoding, Int32, AudioTrackMode)

Class constructor.

Fields

DualMonoModeLl
Obsolete.

This mode indicates that a stereo stream should be presented with the left audio channel replicated into the right audio channel.

DualMonoModeLr
Obsolete.

This mode indicates that a stereo stream should be presented with the left and right audio channels blended together and delivered to both channels.

DualMonoModeOff
Obsolete.

This mode disables any Dual Mono presentation effect.

DualMonoModeRr
Obsolete.

This mode indicates that a stereo stream should be presented with the right audio channel replicated into the left audio channel.

EncapsulationMetadataTypeDvbAdDescriptor
Obsolete.

Encapsulation metadata type for DVB AD descriptor.

EncapsulationMetadataTypeFrameworkTuner
Obsolete.

Encapsulation metadata type for framework tuner information.

EncapsulationMetadataTypeSupplementaryAudioPlacement
Obsolete.

Encapsulation metadata type for placement of supplementary audio.

EncapsulationModeElementaryStream
Obsolete.

This mode indicates metadata encapsulation with an elementary stream payload.

EncapsulationModeNone
Obsolete.

This mode indicates no metadata encapsulation, which is the default mode for sending audio data through AudioTrack.

Error
Obsolete.

Denotes a generic operation failure.

ErrorBadValue
Obsolete.

Denotes a failure due to the use of an invalid value.

ErrorDeadObject
Obsolete.

An error code indicating that the object reporting it is no longer valid and needs to be recreated.

ErrorInvalidOperation
Obsolete.

Denotes a failure due to the improper use of a method.

Success
Obsolete.

Denotes a successful operation.

SupplementaryAudioPlacementLeft
Obsolete.

Supplementary audio placement left.

SupplementaryAudioPlacementNormal
Obsolete.

Supplementary audio placement normal.

SupplementaryAudioPlacementRight
Obsolete.

Supplementary audio placement right.

Properties

AudioAttributes

Returns the AudioAttributes used in configuration.

AudioDescriptionMixLeveldB

Returns the Audio Description mix level in dB.

AudioFormat

Returns the configured audio data encoding.

AudioSessionId

Returns the audio session ID.

BufferCapacityInFrames

Returns the maximum size of the AudioTrack buffer in frames.

BufferSizeInFrames

Returns the effective size of the AudioTrack buffer that the application writes to.

ChannelConfiguration

Returns the configured channel position mask.

ChannelCount

Returns the configured number of channels.

Class

Returns the runtime class of this Object.

(Inherited from Object)
DualMonoMode

Returns the Dual Mono mode presentation setting.

Format

Returns the configured AudioTrack format.

Handle

The handle to the underlying Android instance.

(Inherited from Object)
IsOffloadedPlayback

Returns whether the track was built with Builder#setOffloadedPlayback(boolean) set to true.

JniIdentityHashCode (Inherited from Object)
JniPeerMembers
LogSessionId

Returns the LogSessionId. -or- Sets a LogSessionId instance to this AudioTrack for metrics collection.

MaxVolume

Returns the maximum gain value, which is greater than or equal to 1.

Metrics

Return Metrics data about the current AudioTrack instance.

MinVolume

Returns the minimum gain value, which is the constant 0.

NativeFrameCount
Obsolete.

Returns the frame count of the native AudioTrack buffer.

NotificationMarkerPosition

Returns marker position expressed in frames.

OffloadDelay

Return the decoder delay of an offloaded track, expressed in frames, previously set with #setOffloadDelayPadding(int, int), or 0 if it was never modified.

OffloadPadding

Return the decoder padding of an offloaded track, expressed in frames, previously set with #setOffloadDelayPadding(int, int), or 0 if it was never modified.

PeerReference (Inherited from Object)
PerformanceMode

Returns the current performance mode of the AudioTrack.

PlaybackHeadPosition

Returns the playback head position expressed in frames.

PlaybackParams

Returns the current playback parameters. -or- Sets the playback parameters.

PlaybackRate

Returns the current playback sample rate rate in Hz.

PlayState

Returns the playback state of the AudioTrack instance.

PositionNotificationPeriod

Returns the notification update period expressed in frames.

PreferredDevice

Returns the selected output specified by #setPreferredDevice.

RoutedDevice

Returns an AudioDeviceInfo identifying the current routing of this AudioTrack.

SampleRate

Returns the configured audio source sample rate in Hz.

StartThresholdInFrames

Returns the streaming start threshold of the AudioTrack.

State

Returns the state of the AudioTrack instance.

StreamType

Returns the volume stream type of this AudioTrack.

ThresholdClass

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

ThresholdType

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

UnderrunCount

Returns the number of underrun occurrences in the application-level write buffer since the AudioTrack was created.

Methods

AddOnCodecFormatChangedListener(IExecutor, AudioTrack+IOnCodecFormatChangedListener)

Adds an OnCodecFormatChangedListener to receive notifications of codec format change events on this AudioTrack.

AddOnRoutingChangedListener(AudioTrack+IOnRoutingChangedListener, Handler)

Adds an AudioRouting.OnRoutingChangedListener to receive notifications of routing changes on this AudioTrack.

AddOnRoutingChangedListener(IAudioRoutingOnRoutingChangedListener, Handler)

Adds an AudioRouting.OnRoutingChangedListener to receive notifications of routing changes on this AudioTrack.

AttachAuxEffect(Int32)

Attaches an auxiliary effect to the audio track.

Clone()

Creates and returns a copy of this object.

(Inherited from Object)
CreateVolumeShaper(VolumeShaper+Configuration)
Dispose() (Inherited from Object)
Dispose(Boolean) (Inherited from Object)
Equals(Object)

Indicates whether some other object is "equal to" this one.

(Inherited from Object)
Flush()

Flushes the audio data currently queued for playback.

GetHashCode()

Returns a hash code value for the object.

(Inherited from Object)
GetMinBufferSize(Int32, ChannelOut, Encoding)

Returns the estimated minimum buffer size required for an AudioTrack object to be created in the #MODE_STREAM mode.

GetNativeOutputSampleRate(Stream)

Returns the output sample rate in Hz for the specified stream type.

GetTimestamp(AudioTimestamp)

Poll for a timestamp on demand.

IsDirectPlaybackSupported(AudioFormat, AudioAttributes)

Returns whether direct playback of an audio format with the provided attributes is currently supported on the system.

JavaFinalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

(Inherited from Object)
Notify()

Wakes up a single thread that is waiting on this object's monitor.

(Inherited from Object)
NotifyAll()

Wakes up all threads that are waiting on this object's monitor.

(Inherited from Object)
Pause()

Pauses the playback of the audio data.

Play()

Starts playing an AudioTrack.

RegisterStreamEventCallback(IExecutor, AudioTrack+StreamEventCallback)

Registers a callback for the notification of stream events.

Release()

Releases the native AudioTrack resources.

ReloadStaticData()

Sets the playback head position within the static buffer to zero, that is it rewinds to start of static buffer.

RemoveOnCodecFormatChangedListener(AudioTrack+IOnCodecFormatChangedListener)

Removes an OnCodecFormatChangedListener which has been previously added to receive codec format change events.

RemoveOnRoutingChangedListener(AudioTrack+IOnRoutingChangedListener)

Removes an AudioRouting.OnRoutingChangedListener which has been previously added to receive rerouting notifications.

RemoveOnRoutingChangedListener(IAudioRoutingOnRoutingChangedListener)

Removes an AudioRouting.OnRoutingChangedListener which has been previously added to receive rerouting notifications.

SetAudioDescriptionMixLeveldB(Single)

Sets the Audio Description mix level in dB.

SetAuxEffectSendLevel(Single)

Sets the send level of the audio track to the attached auxiliary effect #attachAuxEffect(int).

SetBufferSizeInFrames(Int32)

Limits the effective size of the AudioTrack buffer that the application writes to.

SetDualMonoMode(Int32)

Sets the Dual Mono mode presentation on the output device.

SetHandle(IntPtr, JniHandleOwnership)

Sets the Handle property.

(Inherited from Object)
SetLoopPoints(Int32, Int32, Int32)

Sets the loop points and the loop count.

SetNotificationMarkerPosition(Int32)

Sets the position of the notification marker.

SetOffloadDelayPadding(Int32, Int32)

Configures the delay and padding values for the current compressed stream playing in offload mode.

SetOffloadEndOfStream()

Declares that the last write() operation on this track provided the last buffer of this stream.

SetPlaybackHeadPosition(Int32)

Sets the playback head position within the static buffer.

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.

SetPlaybackPositionUpdateListener(AudioTrack+IOnPlaybackPositionUpdateListener)

Sets the listener the AudioTrack notifies when a previously set marker is reached or for each periodic playback head position update.

SetPlaybackRate(Int32)

Sets the playback sample rate for this track.

SetPositionNotificationPeriod(Int32)

Sets the period for the periodic notification event.

SetPreferredDevice(AudioDeviceInfo)

Specifies an audio device (via an AudioDeviceInfo object) to route the output from this AudioTrack.

SetPresentation(AudioPresentation)

Sets the audio presentation.

SetStartThresholdInFrames(Int32)

Sets the streaming start threshold for an AudioTrack.

SetState(Int32)
Obsolete.

Sets the initialization state of the instance.

SetStereoVolume(Single, Single)
Obsolete.

Sets the specified left and right output gain values on the AudioTrack.

SetVolume(Single)

Sets the specified output gain value on all channels of this track.

Stop()

Stops playing the audio data.

ToArray<T>() (Inherited from Object)
ToString()

Returns a string representation of the object.

(Inherited from Object)
UnregisterFromRuntime() (Inherited from Object)
UnregisterStreamEventCallback(AudioTrack+StreamEventCallback)

Unregisters the callback for notification of stream events, previously registered with #registerStreamEventCallback(Executor, StreamEventCallback).

Wait()

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>.

(Inherited from Object)
Wait(Int64, Int32)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)
Wait(Int64)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)
Write(Byte[], Int32, Int32, WriteMode)

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)

Writes the audio data to the audio sink for playback (streaming mode), or copies audio data for later playback (static buffer mode).

Write(ByteBuffer, Int32, WriteMode, Int64)

Writes the audio data to the audio sink for playback in streaming mode on a HW_AV_SYNC track.

Write(ByteBuffer, Int32, WriteMode)

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, WriteMode)

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)

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)

Writes the audio data to the audio sink for playback (streaming mode), or copies audio data for later playback (static buffer mode).

WriteAsync(Byte[], Int32, Int32, WriteMode)
WriteAsync(Byte[], Int32, Int32)
WriteAsync(ByteBuffer, Int32, WriteMode, Int64)
WriteAsync(ByteBuffer, Int32, WriteMode)
WriteAsync(Int16[], Int32, Int32, WriteMode)
WriteAsync(Int16[], Int32, Int32)
WriteAsync(Single[], Int32, Int32, WriteMode)

Events

MarkerReached
PeriodicNotification
RoutingChanged

Explicit Interface Implementations

IJavaPeerable.Disposed() (Inherited from Object)
IJavaPeerable.DisposeUnlessReferenced() (Inherited from Object)
IJavaPeerable.Finalized() (Inherited from Object)
IJavaPeerable.JniManagedPeerState (Inherited from Object)
IJavaPeerable.SetJniIdentityHashCode(Int32) (Inherited from Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) (Inherited from Object)
IJavaPeerable.SetPeerReference(JniObjectReference) (Inherited from Object)

Extension Methods

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
GetJniTypeName(IJavaPeerable)

Applies to