OggFileAudioBufferSink


@UnstableApi
public final class OggFileAudioBufferSink extends ForwardingAudioSink


A sink for audio buffers that writes output audio as .ogg files with a given path prefix. When new audio data is handled after flushing the audio packetizer, a counter is incremented and its value is appended to the output file name.

Note: if writing to external storage it's necessary to grant the WRITE_EXTERNAL_STORAGE permission.

Summary

Constants

static final int
SAMPLE_RATE = 48000

Opus streams are always 48000 Hz.

Public constructors

OggFileAudioBufferSink(
    AudioSink audioSink,
    String outputFileNamePrefix
)

Creates an instance.

Public methods

void

Flushes the sink, after which it is ready to receive buffers from a new playback position.

boolean
handleBuffer(
    ByteBuffer buffer,
    long presentationTimeUs,
    int encodedAccessUnitCount
)

Attempts to process data from a ByteBuffer, starting from its current position and ending at its limit (exclusive).

void

Resets the sink, releasing any resources that it currently holds.

Inherited Constants

From androidx.media3.exoplayer.audio.AudioSink
static final long
CURRENT_POSITION_NOT_SET = -9223372036854775808

Returned by getCurrentPositionUs when the position is not set.

static final int

The audio sink will never play in offload mode.

static final int

The audio sink will prefer offload playback even if this might result in silence gaps between tracks.

static final int

The audio sink will prefer offload playback except in the case where both the track is gapless and the device does support gapless offload playback.

static final int

The sink supports the format directly, without the need for internal transcoding.

static final int

The sink supports the format, but needs to transcode it internally to do so.

static final int

The sink does not support the format.

Inherited methods

From androidx.media3.exoplayer.audio.AudioSink
abstract boolean
handleBuffer(
    ByteBuffer buffer,
    long presentationTimeUs,
    int encodedAccessUnitCount
)

Attempts to process data from a ByteBuffer, starting from its current position and ending at its limit (exclusive).

From androidx.media3.exoplayer.audio.ForwardingAudioSink
void
configure(
    Format inputFormat,
    int specifiedBufferSize,
    @Nullable int[] outputChannels
)

Configures (or reconfigures) the sink.

void

Disables tunneling.

void

Enables tunneling, if possible.

@Nullable AudioAttributes

Returns the audio attributes used for audio playback, or null if the sink does not use audio attributes.

long
getCurrentPositionUs(boolean sourceEnded)

Returns the playback position in the stream starting at zero, in microseconds, or CURRENT_POSITION_NOT_SET if it is not yet available.

AudioOffloadSupport

Returns the level of offload support that the sink can provide for a given Format.

int

Returns the level of support that the sink provides for a given Format.

PlaybackParameters

Returns the active PlaybackParameters.

boolean

Returns whether silences are skipped in the audio stream.

void

Signals to the sink that the next buffer may be discontinuous with the previous buffer.

boolean

Returns whether the sink has data pending that has not been consumed yet.

boolean

Returns whether playToEndOfStream has been called and all buffers have been processed.

void

Pauses playback.

void

Starts or resumes consuming audio if initialized.

void

Processes any remaining data.

void

Releases the audio sink.

void

Sets attributes for audio playback.

void
setAudioSessionId(int audioSessionId)

Sets the audio session id.

void

Sets the auxiliary effect.

void
setClock(Clock clock)

Sets the Clock to use for timing in this audio sink.

void

Sets the listener for sink events, which should be the audio renderer.

void
@RequiresApi(value = 29)
setOffloadDelayPadding(int delayInFrames, int paddingInFrames)

Sets offload delay padding on the AudioTrack, if possible.

void
@RequiresApi(value = 29)
setOffloadMode(@AudioSink.OffloadMode int offloadMode)

Sets audio offload mode, if possible.

void
setOutputStreamOffsetUs(long outputStreamOffsetUs)

Sets the offset that is added to the media timestamp before it is passed as presentationTimeUs in handleBuffer.

void

Attempts to set the playback parameters.

void

Sets the PlayerId of the player using this audio sink.

void
@RequiresApi(value = 23)
setPreferredDevice(@Nullable AudioDeviceInfo audioDeviceInfo)

Sets the preferred audio device.

void
setSkipSilenceEnabled(boolean skipSilenceEnabled)

Sets whether silences should be skipped in the audio stream.

void
setVolume(float volume)

Sets the playback volume.

boolean

Returns whether the sink supports a given Format.

Constants

SAMPLE_RATE

public static final int SAMPLE_RATE = 48000

Opus streams are always 48000 Hz.

Public constructors

OggFileAudioBufferSink

public OggFileAudioBufferSink(
    AudioSink audioSink,
    String outputFileNamePrefix
)

Creates an instance.

Parameters
AudioSink audioSink

The base audioSink calls are forwarded to.

String outputFileNamePrefix

The prefix for output files.

Public methods

flush

public void flush()

Flushes the sink, after which it is ready to receive buffers from a new playback position.

The audio session may remain active until reset is called.

handleBuffer

public boolean handleBuffer(
    ByteBuffer buffer,
    long presentationTimeUs,
    int encodedAccessUnitCount
)

Attempts to process data from a ByteBuffer, starting from its current position and ending at its limit (exclusive). The position of the ByteBuffer is advanced by the number of bytes that were handled. onPositionDiscontinuity will be called if presentationTimeUs is discontinuous with the last buffer handled since the last reset.

Returns whether the data was handled in full. If the data was not handled in full then the same ByteBuffer must be provided to subsequent calls until it has been fully consumed, except in the case of an intervening call to flush (or to configure that causes the sink to be flushed).

Parameters
ByteBuffer buffer

The buffer containing audio data.

long presentationTimeUs

The presentation timestamp of the buffer in microseconds.

int encodedAccessUnitCount

The number of encoded access units in the buffer, or 1 if the buffer contains PCM audio. This allows batching multiple encoded access units in one buffer.

Returns
boolean

Whether the buffer was handled fully.

Throws
androidx.media3.exoplayer.audio.AudioSink.InitializationException

If an error occurs initializing the sink.

androidx.media3.exoplayer.audio.AudioSink.WriteException

If an error occurs writing the audio data.

reset

public void reset()

Resets the sink, releasing any resources that it currently holds.