Pcm Output
Where a player sends audio: either a direct USB stream (DacStream) or Android's own audio stack (AndroidManagedOutput). The two share this interface so a player can use either.
The audio is bytes in the layout the output reports: for a DacStream, the wire layout of its StreamFormat (little-endian, interleaved, left-justified in the subslot); for an AndroidManagedOutput, the PCM encoding of its AudioTrack.
Threading
write never blocks; it takes what fits and returns the count. Use one writer thread: write is thread-safe, but the bytes form one ordered sequence. The control calls and getters are safe from any thread and do not block for long. close may take about a tenth of a second.
Inheritors
Properties
Whether the output is bit-perfect, and how well that can be known; see BitPerfectState.
Frames between the host and the DAC: what is buffered plus what is in flight.
Frames that have actually played since the start or the last flush, in the output's own frame rate. Silence inserted while paused or underrun does not count. Audible position is startPosition + playedFrames / frameRate.
How many bytes a writer should write now: the room up to the target depth, a whole number of frames. A pull loop writes this much and sleeps when it is 0. It is a target, not a hard limit.
Functions
Discards everything buffered and resets playedFrames to 0. Use it for a seek, a stop, or a track the user picked; it keeps a deep buffer from costing responsiveness. On the direct USB output with DacConfig.cancelInFlightOnFlush (on by default), the output thread also asks the host controller to cancel the submitted transfers that carry music from before the flush, waits up to a second for them to be handed back (the stream fails if they are not), and sends silence until the ring refills. A transfer that completes before its cancellation takes effect still plays, and no time to silence is guaranteed. With the flag off, or on the managed output, audio already on the bus or in the platform's last buffer may still be heard.
Writes whole frames from the buffer's position and advances the position by the bytes taken. Direct buffers are read in place; a heap buffer is copied. Returns the bytes taken, which can be less than buffer.remaining() (the output is full: keep the rest and try again), and 0 for a partial frame. On success the return value equals the position delta. Accepted source frames may be held in a converter before playback; use playedFrames for playback position. Keep the remaining buffer after a failure. For INVALID_ARGUMENT, correct the layout or padding before retrying; do not silently truncate or mask samples.