Share via


AudioTrack.Write Method

Definition

Overloads

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(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(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(ByteBuffer, Int32, WriteMode, Int64)

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

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(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(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).

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).

[Android.Runtime.Register("write", "(Ljava/nio/ByteBuffer;II)I", "GetWrite_Ljava_nio_ByteBuffer_IIHandler")]
public virtual int Write (Java.Nio.ByteBuffer audioData, int sizeInBytes, Android.Media.WriteMode writeMode);
[<Android.Runtime.Register("write", "(Ljava/nio/ByteBuffer;II)I", "GetWrite_Ljava_nio_ByteBuffer_IIHandler")>]
abstract member Write : Java.Nio.ByteBuffer * int * Android.Media.WriteMode -> int
override this.Write : Java.Nio.ByteBuffer * int * Android.Media.WriteMode -> int

Parameters

audioData
ByteBuffer

the buffer that holds the data to write, starting at the position reported by audioData.position(). <BR>Note that upon return, the buffer position (audioData.position()) will have been advanced to reflect the amount of data that was successfully written to the AudioTrack.

sizeInBytes
Int32

number of bytes to write. It is recommended but not enforced that the number of bytes requested be a multiple of the frame size (sample size in bytes multiplied by the channel count). <BR>Note this may differ from audioData.remaining(), but cannot exceed it.

writeMode
WriteMode

one of #WRITE_BLOCKING, #WRITE_NON_BLOCKING. It has no effect in static mode. <BR>With #WRITE_BLOCKING, the write will block until all data has been written to the audio sink. <BR>With #WRITE_NON_BLOCKING, the write will return immediately after queuing as much audio data for playback as possible without blocking.

Returns

zero or the positive number of bytes that were written, or one of the following error codes. <ul> <li>#ERROR_INVALID_OPERATION if the track isn't properly initialized</li> <li>#ERROR_BAD_VALUE if the parameters don't resolve to valid data and indexes</li> <li>#ERROR_DEAD_OBJECT if the AudioTrack is not valid anymore and needs to be recreated. The dead object error code is not returned if some data was successfully transferred. In this case, the error is returned at the next write()</li> <li>#ERROR in case of other error</li> </ul>

Attributes

Remarks

Writes the audio data to the audio sink for playback (streaming mode), or copies audio data for later playback (static buffer mode). The audioData in ByteBuffer should match the format specified in the AudioTrack constructor.

In streaming mode, the blocking behavior depends on the write mode. If the write mode is #WRITE_BLOCKING, the write will normally block until all the data has been enqueued for playback, and will return a full transfer count. However, if the write mode is #WRITE_NON_BLOCKING, or the track is stopped or paused on entry, or another thread interrupts the write by calling stop or pause, or an I/O error occurs during the write, then the write may return a short transfer count.

In static buffer mode, copies the data to the buffer starting at offset 0, and the write mode is ignored. Note that the actual playback of this data might occur after this function returns.

Java documentation for android.media.AudioTrack.write(java.nio.ByteBuffer, int, int).

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

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).

[Android.Runtime.Register("write", "([BII)I", "GetWrite_arrayBIIHandler")]
public virtual int Write (byte[] audioData, int offsetInBytes, int sizeInBytes);
[<Android.Runtime.Register("write", "([BII)I", "GetWrite_arrayBIIHandler")>]
abstract member Write : byte[] * int * int -> int
override this.Write : byte[] * int * int -> int

Parameters

audioData
Byte[]

the array that holds the data to play.

offsetInBytes
Int32

the offset expressed in bytes in audioData where the data to write starts. Must not be negative, or cause the data access to go out of bounds of the array.

sizeInBytes
Int32

the number of bytes to write in audioData after the offset. Must not be negative, or cause the data access to go out of bounds of the array.

Returns

zero or the positive number of bytes that were written, or one of the following error codes. The number of bytes will be a multiple of the frame size in bytes not to exceed sizeInBytes. <ul> <li>#ERROR_INVALID_OPERATION if the track isn't properly initialized</li> <li>#ERROR_BAD_VALUE if the parameters don't resolve to valid data and indexes</li> <li>#ERROR_DEAD_OBJECT if the AudioTrack is not valid anymore and needs to be recreated. The dead object error code is not returned if some data was successfully transferred. In this case, the error is returned at the next write()</li> <li>#ERROR in case of other error</li> </ul> This is equivalent to #write(byte[], int, int, int) with writeMode set to #WRITE_BLOCKING.

Attributes

Remarks

Writes the audio data to the audio sink for playback (streaming mode), or copies audio data for later playback (static buffer mode). The format specified in the AudioTrack constructor should be AudioFormat#ENCODING_PCM_8BIT to correspond to the data in the array. The format can be AudioFormat#ENCODING_PCM_16BIT, but this is deprecated.

In streaming mode, the write will normally block until all the data has been enqueued for playback, and will return a full transfer count. However, if the track is stopped or paused on entry, or another thread interrupts the write by calling stop or pause, or an I/O error occurs during the write, then the write may return a short transfer count.

In static buffer mode, copies the data to the buffer starting at offset 0. Note that the actual playback of this data might occur after this function returns.

Java documentation for android.media.AudioTrack.write(byte[], int, int).

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

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).

[Android.Runtime.Register("write", "([SII)I", "GetWrite_arraySIIHandler")]
public virtual int Write (short[] audioData, int offsetInShorts, int sizeInShorts);
[<Android.Runtime.Register("write", "([SII)I", "GetWrite_arraySIIHandler")>]
abstract member Write : int16[] * int * int -> int
override this.Write : int16[] * int * int -> int

Parameters

audioData
Int16[]

the array that holds the data to play.

offsetInShorts
Int32

the offset expressed in shorts in audioData where the data to play starts. Must not be negative, or cause the data access to go out of bounds of the array.

sizeInShorts
Int32

the number of shorts to read in audioData after the offset. Must not be negative, or cause the data access to go out of bounds of the array.

Returns

zero or the positive number of shorts that were written, or one of the following error codes. The number of shorts will be a multiple of the channel count not to exceed sizeInShorts. <ul> <li>#ERROR_INVALID_OPERATION if the track isn't properly initialized</li> <li>#ERROR_BAD_VALUE if the parameters don't resolve to valid data and indexes</li> <li>#ERROR_DEAD_OBJECT if the AudioTrack is not valid anymore and needs to be recreated. The dead object error code is not returned if some data was successfully transferred. In this case, the error is returned at the next write()</li> <li>#ERROR in case of other error</li> </ul> This is equivalent to #write(short[], int, int, int) with writeMode set to #WRITE_BLOCKING.

Attributes

Remarks

Writes the audio data to the audio sink for playback (streaming mode), or copies audio data for later playback (static buffer mode). The format specified in the AudioTrack constructor should be AudioFormat#ENCODING_PCM_16BIT to correspond to the data in the array.

In streaming mode, the write will normally block until all the data has been enqueued for playback, and will return a full transfer count. However, if the track is stopped or paused on entry, or another thread interrupts the write by calling stop or pause, or an I/O error occurs during the write, then the write may return a short transfer count.

In static buffer mode, copies the data to the buffer starting at offset 0. Note that the actual playback of this data might occur after this function returns.

Java documentation for android.media.AudioTrack.write(short[], int, int).

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

Write(ByteBuffer, Int32, WriteMode, Int64)

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

[Android.Runtime.Register("write", "(Ljava/nio/ByteBuffer;IIJ)I", "GetWrite_Ljava_nio_ByteBuffer_IIJHandler", ApiSince=23)]
public virtual int Write (Java.Nio.ByteBuffer audioData, int sizeInBytes, Android.Media.WriteMode writeMode, long timestamp);
[<Android.Runtime.Register("write", "(Ljava/nio/ByteBuffer;IIJ)I", "GetWrite_Ljava_nio_ByteBuffer_IIJHandler", ApiSince=23)>]
abstract member Write : Java.Nio.ByteBuffer * int * Android.Media.WriteMode * int64 -> int
override this.Write : Java.Nio.ByteBuffer * int * Android.Media.WriteMode * int64 -> int

Parameters

audioData
ByteBuffer

the buffer that holds the data to write, starting at the position reported by audioData.position(). <BR>Note that upon return, the buffer position (audioData.position()) will have been advanced to reflect the amount of data that was successfully written to the AudioTrack.

sizeInBytes
Int32

number of bytes to write. It is recommended but not enforced that the number of bytes requested be a multiple of the frame size (sample size in bytes multiplied by the channel count). <BR>Note this may differ from audioData.remaining(), but cannot exceed it.

writeMode
WriteMode

one of #WRITE_BLOCKING, #WRITE_NON_BLOCKING. <BR>With #WRITE_BLOCKING, the write will block until all data has been written to the audio sink. <BR>With #WRITE_NON_BLOCKING, the write will return immediately after queuing as much audio data for playback as possible without blocking.

timestamp
Int64

The timestamp, in nanoseconds, of the first decodable audio frame in the provided audioData.

Returns

zero or the positive number of bytes that were written, or one of the following error codes. <ul> <li>#ERROR_INVALID_OPERATION if the track isn't properly initialized</li> <li>#ERROR_BAD_VALUE if the parameters don't resolve to valid data and indexes</li> <li>#ERROR_DEAD_OBJECT if the AudioTrack is not valid anymore and needs to be recreated. The dead object error code is not returned if some data was successfully transferred. In this case, the error is returned at the next write()</li> <li>#ERROR in case of other error</li> </ul>

Attributes

Remarks

Writes the audio data to the audio sink for playback in streaming mode on a HW_AV_SYNC track. The blocking behavior will depend on the write mode.

Java documentation for android.media.AudioTrack.write(java.nio.ByteBuffer, int, int, long).

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

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).

[Android.Runtime.Register("write", "([BIII)I", "GetWrite_arrayBIIIHandler", ApiSince=23)]
public virtual int Write (byte[] audioData, int offsetInBytes, int sizeInBytes, Android.Media.WriteMode writeMode);
[<Android.Runtime.Register("write", "([BIII)I", "GetWrite_arrayBIIIHandler", ApiSince=23)>]
abstract member Write : byte[] * int * int * Android.Media.WriteMode -> int
override this.Write : byte[] * int * int * Android.Media.WriteMode -> int

Parameters

audioData
Byte[]

the array that holds the data to play.

offsetInBytes
Int32

the offset expressed in bytes in audioData where the data to write starts. Must not be negative, or cause the data access to go out of bounds of the array.

sizeInBytes
Int32

the number of bytes to write in audioData after the offset. Must not be negative, or cause the data access to go out of bounds of the array.

writeMode
WriteMode

one of #WRITE_BLOCKING, #WRITE_NON_BLOCKING. It has no effect in static mode. <br>With #WRITE_BLOCKING, the write will block until all data has been written to the audio sink. <br>With #WRITE_NON_BLOCKING, the write will return immediately after queuing as much audio data for playback as possible without blocking.

Returns

zero or the positive number of bytes that were written, or one of the following error codes. The number of bytes will be a multiple of the frame size in bytes not to exceed sizeInBytes. <ul> <li>#ERROR_INVALID_OPERATION if the track isn't properly initialized</li> <li>#ERROR_BAD_VALUE if the parameters don't resolve to valid data and indexes</li> <li>#ERROR_DEAD_OBJECT if the AudioTrack is not valid anymore and needs to be recreated. The dead object error code is not returned if some data was successfully transferred. In this case, the error is returned at the next write()</li> <li>#ERROR in case of other error</li> </ul>

Attributes

Remarks

Writes the audio data to the audio sink for playback (streaming mode), or copies audio data for later playback (static buffer mode). The format specified in the AudioTrack constructor should be AudioFormat#ENCODING_PCM_8BIT to correspond to the data in the array. The format can be AudioFormat#ENCODING_PCM_16BIT, but this is deprecated.

In streaming mode, the blocking behavior depends on the write mode. If the write mode is #WRITE_BLOCKING, the write will normally block until all the data has been enqueued for playback, and will return a full transfer count. However, if the write mode is #WRITE_NON_BLOCKING, or the track is stopped or paused on entry, or another thread interrupts the write by calling stop or pause, or an I/O error occurs during the write, then the write may return a short transfer count.

In static buffer mode, copies the data to the buffer starting at offset 0, and the write mode is ignored. Note that the actual playback of this data might occur after this function returns.

Java documentation for android.media.AudioTrack.write(byte[], int, int, int).

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

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).

[Android.Runtime.Register("write", "([SIII)I", "GetWrite_arraySIIIHandler", ApiSince=23)]
public virtual int Write (short[] audioData, int offsetInShorts, int sizeInShorts, Android.Media.WriteMode writeMode);
[<Android.Runtime.Register("write", "([SIII)I", "GetWrite_arraySIIIHandler", ApiSince=23)>]
abstract member Write : int16[] * int * int * Android.Media.WriteMode -> int
override this.Write : int16[] * int * int * Android.Media.WriteMode -> int

Parameters

audioData
Int16[]

the array that holds the data to write.

offsetInShorts
Int32

the offset expressed in shorts in audioData where the data to write starts. Must not be negative, or cause the data access to go out of bounds of the array.

sizeInShorts
Int32

the number of shorts to read in audioData after the offset. Must not be negative, or cause the data access to go out of bounds of the array.

writeMode
WriteMode

one of #WRITE_BLOCKING, #WRITE_NON_BLOCKING. It has no effect in static mode. <br>With #WRITE_BLOCKING, the write will block until all data has been written to the audio sink. <br>With #WRITE_NON_BLOCKING, the write will return immediately after queuing as much audio data for playback as possible without blocking.

Returns

zero or the positive number of shorts that were written, or one of the following error codes. The number of shorts will be a multiple of the channel count not to exceed sizeInShorts. <ul> <li>#ERROR_INVALID_OPERATION if the track isn't properly initialized</li> <li>#ERROR_BAD_VALUE if the parameters don't resolve to valid data and indexes</li> <li>#ERROR_DEAD_OBJECT if the AudioTrack is not valid anymore and needs to be recreated. The dead object error code is not returned if some data was successfully transferred. In this case, the error is returned at the next write()</li> <li>#ERROR in case of other error</li> </ul>

Attributes

Remarks

Writes the audio data to the audio sink for playback (streaming mode), or copies audio data for later playback (static buffer mode). The format specified in the AudioTrack constructor should be AudioFormat#ENCODING_PCM_16BIT to correspond to the data in the array.

In streaming mode, the blocking behavior depends on the write mode. If the write mode is #WRITE_BLOCKING, the write will normally block until all the data has been enqueued for playback, and will return a full transfer count. However, if the write mode is #WRITE_NON_BLOCKING, or the track is stopped or paused on entry, or another thread interrupts the write by calling stop or pause, or an I/O error occurs during the write, then the write may return a short transfer count.

In static buffer mode, copies the data to the buffer starting at offset 0. Note that the actual playback of this data might occur after this function returns.

Java documentation for android.media.AudioTrack.write(short[], int, int, int).

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

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).

[Android.Runtime.Register("write", "([FIII)I", "GetWrite_arrayFIIIHandler")]
public virtual int Write (float[] audioData, int offsetInFloats, int sizeInFloats, Android.Media.WriteMode writeMode);
[<Android.Runtime.Register("write", "([FIII)I", "GetWrite_arrayFIIIHandler")>]
abstract member Write : single[] * int * int * Android.Media.WriteMode -> int
override this.Write : single[] * int * int * Android.Media.WriteMode -> int

Parameters

audioData
Single[]

the array that holds the data to write. The implementation does not clip for sample values within the nominal range [-1.0f, 1.0f], provided that all gains in the audio pipeline are less than or equal to unity (1.0f), and in the absence of post-processing effects that could add energy, such as reverb. For the convenience of applications that compute samples using filters with non-unity gain, sample values +3 dB beyond the nominal range are permitted. However such values may eventually be limited or clipped, depending on various gains and later processing in the audio path. Therefore applications are encouraged to provide samples values within the nominal range.

offsetInFloats
Int32

the offset, expressed as a number of floats, in audioData where the data to write starts. Must not be negative, or cause the data access to go out of bounds of the array.

sizeInFloats
Int32

the number of floats to write in audioData after the offset. Must not be negative, or cause the data access to go out of bounds of the array.

writeMode
WriteMode

one of #WRITE_BLOCKING, #WRITE_NON_BLOCKING. It has no effect in static mode. <br>With #WRITE_BLOCKING, the write will block until all data has been written to the audio sink. <br>With #WRITE_NON_BLOCKING, the write will return immediately after queuing as much audio data for playback as possible without blocking.

Returns

zero or the positive number of floats that were written, or one of the following error codes. The number of floats will be a multiple of the channel count not to exceed sizeInFloats. <ul> <li>#ERROR_INVALID_OPERATION if the track isn't properly initialized</li> <li>#ERROR_BAD_VALUE if the parameters don't resolve to valid data and indexes</li> <li>#ERROR_DEAD_OBJECT if the AudioTrack is not valid anymore and needs to be recreated. The dead object error code is not returned if some data was successfully transferred. In this case, the error is returned at the next write()</li> <li>#ERROR in case of other error</li> </ul>

Attributes

Remarks

Writes the audio data to the audio sink for playback (streaming mode), or copies audio data for later playback (static buffer mode). The format specified in the AudioTrack constructor should be AudioFormat#ENCODING_PCM_FLOAT to correspond to the data in the array.

In streaming mode, the blocking behavior depends on the write mode. If the write mode is #WRITE_BLOCKING, the write will normally block until all the data has been enqueued for playback, and will return a full transfer count. However, if the write mode is #WRITE_NON_BLOCKING, or the track is stopped or paused on entry, or another thread interrupts the write by calling stop or pause, or an I/O error occurs during the write, then the write may return a short transfer count.

In static buffer mode, copies the data to the buffer starting at offset 0, and the write mode is ignored. Note that the actual playback of this data might occur after this function returns.

Java documentation for android.media.AudioTrack.write(float[], int, int, int).

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