Freigeben über


AudioTrack.Play Method

Definition

Starts playing an AudioTrack.

[Android.Runtime.Register("play", "()V", "GetPlayHandler")]
public virtual void Play ();
[<Android.Runtime.Register("play", "()V", "GetPlayHandler")>]
abstract member Play : unit -> unit
override this.Play : unit -> unit
Attributes

Exceptions

if the track isn't properly initialized

Remarks

Starts playing an AudioTrack.

If track's creation mode is #MODE_STATIC, you must have called one of the write methods (#write(byte[], int, int), #write(byte[], int, int, int), #write(short[], int, int), #write(short[], int, int, int), #write(float[], int, int, int), or #write(ByteBuffer, int, int)) prior to play().

If the mode is #MODE_STREAM, you can optionally prime the data path prior to calling play(), by writing up to bufferSizeInBytes (from constructor). If you don't call write() first, or if you call write() but with an insufficient amount of data, then the track will be in underrun state at play(). In this case, playback will not actually start playing until the data path is filled to a device-specific minimum level. This requirement for the path to be filled to a minimum level is also true when resuming audio playback after calling stop(). Similarly the buffer will need to be filled up again after the track underruns due to failure to call write() in a timely manner with sufficient data. For portability, an application should prime the data path to the maximum allowed by writing data until the write() method returns a short transfer count. This allows play() to start immediately, and reduces the chance of underrun.

As of android.os.Build.VERSION_CODES#S the minimum level to start playing can be obtained using #getStartThresholdInFrames() and set with #setStartThresholdInFrames(int).

Java documentation for android.media.AudioTrack.play().

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.

Applies to