writeStreamData

@Synchronized
fun writeStreamData(buffer: ByteBuffer, frameCount: Int, blocking: Boolean): Long

Writes pcm audio data in push mode operation, which is suitable for real-time audio,the buffer must be direct buffer and the owner of the buffer must be the caller, and the buffer must be released after no longer needed.

Note: This method only supports writing raw PCM audio data. Any audio file format headers must be removed before writing. In order to get smooth playback experience, the best practice is to write audio data in IO thread with coroutines, please call writeStreamData in IO thread, don't call it in main thread or UI thread.

Buffer Lifecycle Rules

  1. Ownership:

    • Caller retains ownership of the buffer.

    • Caller is responsible for allocation and deallocation.

  2. Release Timing

    • After write completion: Safe to reuse when: a) Blocking mode: After method returns. b) Non-blocking: After buffer is fully consumed (check via position).

    • After controller close: Must release all associated buffers.

Return

number of frames actually written, or -1 on error.

Parameters

buffer

PCM audio data buffer (must be direct buffer).

frameCount

Number of audio frames to write.

blocking

Whether to wait for buffer consumption.