# SampleQueue

Artifact: [androidx.media3:media3-exoplayer](https://developer.android.com/jetpack/androidx/releases/media3) [View Source](https://cs.android.com/search?q=file:androidx/media3/exoplayer/source/SampleQueue.java+class:androidx.media3.exoplayer.source.SampleQueue)

*** ** * ** ***


```
@UnstableApi
public class SampleQueue implements TrackOutput
```

<br />

*** ** * ** ***

A queue of media samples.

## Summary

| ### Nested types |
|---|
| `public interface https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue.UpstreamFormatChangedListener` A listener for changes to the upstream format. |

| ### Public fields |
|---|---|
| `boolean` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#isLastSampleQueued()` |
| `long` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#largestQueuedTimestampUs()` |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/common/Format` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#upstreamFormat()` |

| ### Protected constructors |
|---|
| `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#SampleQueue(androidx.media3.exoplayer.upstream.Allocator,androidx.media3.exoplayer.drm.DrmSessionManager,androidx.media3.exoplayer.drm.DrmSessionEventListener.EventDispatcher)( https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Allocator allocator, @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/exoplayer/drm/DrmSessionManager drmSessionManager, @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/exoplayer/drm/DrmSessionEventListener.EventDispatcher drmEventDispatcher )` |

| ### Public methods |
|---|---|
| `static https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#createWithDrm(androidx.media3.exoplayer.upstream.Allocator,androidx.media3.exoplayer.drm.DrmSessionManager,androidx.media3.exoplayer.drm.DrmSessionEventListener.EventDispatcher)( https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Allocator allocator, https://developer.android.com/reference/androidx/media3/exoplayer/drm/DrmSessionManager drmSessionManager, https://developer.android.com/reference/androidx/media3/exoplayer/drm/DrmSessionEventListener.EventDispatcher drmEventDispatcher )` Creates a sample queue with DRM resource management. |
| `static https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue` | `[createWithDrm](https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#createWithDrm(androidx.media3.exoplayer.upstream.Allocator,android.os.Looper,androidx.media3.exoplayer.drm.DrmSessionManager,androidx.media3.exoplayer.drm.DrmSessionEventListener.EventDispatcher))( https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Allocator allocator, https://developer.android.com/reference/android/os/Looper.html playbackLooper, https://developer.android.com/reference/androidx/media3/exoplayer/drm/DrmSessionManager drmSessionManager, https://developer.android.com/reference/androidx/media3/exoplayer/drm/DrmSessionEventListener.EventDispatcher drmEventDispatcher )` **This method is deprecated.** Use `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#createWithDrm(androidx.media3.exoplayer.upstream.Allocator,androidx.media3.exoplayer.drm.DrmSessionManager,androidx.media3.exoplayer.drm.DrmSessionEventListener.EventDispatcher)` instead. <br /> |
| `static https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#createWithoutDrm(androidx.media3.exoplayer.upstream.Allocator)(https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Allocator allocator)` Creates a sample queue without DRM resource management. |
| `synchronized long` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#discardSampleMetadataToRead()()` |
| `final void` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#discardTo(long,boolean,boolean)(long timeUs, boolean toKeyframe, boolean stopAtReadPosition)` Discards up to but not including the sample immediately before or at the specified time. |
| `final void` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#discardToEnd()()` Discards all samples in the queue and advances the read position. |
| `final void` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#discardToRead()()` Discards up to but not including the read position. |
| `final void` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#discardUpstreamFrom(long)(long timeUs)` Discards samples from the write side of the queue. |
| `final void` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#discardUpstreamSamples(int)(int discardFromIndex)` Discards samples from the write side of the queue. |
| `final void` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#expectResume()()` Prepares the queue to resume receiving samples from an earlier timestamp, matching duplicate overlap samples at the existing queue tail before appending new samples. |
| `final void` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#format(androidx.media3.common.Format)(https://developer.android.com/reference/androidx/media3/common/Format format)` Called when the `https://developer.android.com/reference/androidx/media3/common/Format` of the track has been extracted from the stream. |
| `final int` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#getFirstIndex()()` Returns the current absolute start index. |
| `synchronized final long` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#getFirstTimestampUs()()` Returns the timestamp of the first sample, or `https://developer.android.com/reference/java/lang/Long.html#MIN_VALUE` if the queue is empty. |
| `synchronized final long` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#getLargestQueuedTimestampUs()()` Returns the largest sample timestamp that has been queued since the last `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#reset()`. |
| `synchronized final long` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#getLargestReadTimestampUs()()` Returns the largest sample timestamp that has been read since the last `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#reset()`. |
| `final int` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#getReadIndex()()` Returns the current absolute read index. |
| `synchronized final int` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#getSkipCount(long,boolean)(long timeUs, boolean allowEndOfQueue)` Returns the number of samples that need to be `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#skip(int)` to advance the read position to the keyframe before or at the specified time. |
| `synchronized final @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/common/Format` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#getUpstreamFormat()()` Returns the upstream `https://developer.android.com/reference/androidx/media3/common/Format` in which samples are being queued. |
| `final int` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#getWriteIndex()()` Returns the current absolute write index. |
| `synchronized final boolean` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#hasQueuedTimestampsUpToReadEndTimeUs()()` Returns whether all required samples to read up to the configured `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#setReadEndTimeUs(long)` have been queued. |
| `static boolean` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#isDiscardingAllSamplesToStartTime(androidx.media3.common.Format)(https://developer.android.com/reference/androidx/media3/common/Format format)` Returns whether samples earlier than `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#setStartTimeUs(long)` will be discarded for the given format. |
| `synchronized final boolean` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#isLastSampleQueued()()` Returns whether the last sample of the stream has knowingly been queued. |
| `synchronized boolean` | `@https://developer.android.com/reference/androidx/annotation/CallSuper https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#isReady(boolean)(boolean loadingFinished)` Returns whether there is data available for reading. |
| `void` | `@https://developer.android.com/reference/androidx/annotation/CallSuper https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#maybeThrowError()()` Throws an error that's preventing data from being read. |
| `synchronized final long` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#peekSourceId()()` Peeks the source id of the next sample to be read, or the current upstream source id if the queue is empty or if the read position is at the end of the queue. |
| `void` | `@https://developer.android.com/reference/androidx/annotation/CallSuper https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#preRelease()()` Calls `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#discardToEnd()` and releases any resources owned by the queue. |
| `int` | `@https://developer.android.com/reference/androidx/annotation/CallSuper https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#read(androidx.media3.exoplayer.FormatHolder,androidx.media3.decoder.DecoderInputBuffer,int,boolean)( https://developer.android.com/reference/androidx/media3/exoplayer/FormatHolder formatHolder, https://developer.android.com/reference/androidx/media3/decoder/DecoderInputBuffer buffer, @https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleStream.ReadFlags int readFlags, boolean loadingFinished )` Attempts to read from the queue. |
| `void` | `@https://developer.android.com/reference/androidx/annotation/CallSuper https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#release()()` Calls `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#reset(boolean)` and releases any resources owned by the queue. |
| `final void` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#reset()()` Convenience method for `reset(false)`. |
| `void` | `@https://developer.android.com/reference/androidx/annotation/CallSuper https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#reset(boolean)(boolean resetUpstreamFormat)` Clears all samples from the queue. |
| `final void` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#sampleData(androidx.media3.common.util.ParsableByteArray,int,int)( https://developer.android.com/reference/androidx/media3/common/util/ParsableByteArray data, int length, @https://developer.android.com/reference/androidx/media3/extractor/TrackOutput.SampleDataPart int sampleDataPart )` Called to write sample data to the output. |
| `final int` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#sampleData(androidx.media3.common.DataReader,int,boolean,int)( https://developer.android.com/reference/androidx/media3/common/DataReader input, int length, boolean allowEndOfInput, @https://developer.android.com/reference/androidx/media3/extractor/TrackOutput.SampleDataPart int sampleDataPart )` Called to write sample data to the output. |
| `void` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#sampleMetadata(long,int,int,int,androidx.media3.extractor.TrackOutput.CryptoData)( long timeUs, @https://developer.android.com/reference/androidx/media3/common/C.BufferFlags int flags, int size, int offset, @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/extractor/TrackOutput.CryptoData cryptoData )` Called when metadata associated with a sample has been extracted from the stream. |
| `synchronized final boolean` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#seekTo(int)(int sampleIndex)` Attempts to seek the read position to the specified sample index. |
| `synchronized final boolean` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#seekTo(long,boolean)(long timeUs, boolean allowTimeBeyondBuffer)` Attempts to seek the read position to the keyframe before or at the specified time. |
| `synchronized final void` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#setReadEndTimeUs(long)(long readEndTimeUs)` Sets the time at which reading the stream should stop. |
| `final void` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#setSampleOffsetUs(long)(long sampleOffsetUs)` Sets an offset that will be added to the timestamps (and sub-sample timestamps) of samples that are subsequently queued. |
| `final void` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#setStartTimeUs(long)(long startTimeUs)` Sets the start time for the queue. |
| `final void` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#setUpstreamFormatChangeListener(androidx.media3.exoplayer.source.SampleQueue.UpstreamFormatChangedListener)( @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue.UpstreamFormatChangedListener listener )` Sets a listener to be notified of changes to the upstream format. |
| `synchronized final void` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#skip(int)(int count)` Advances the read position by the specified number of samples. |
| `final void` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#sourceId(long)(long sourceId)` Sets a source identifier for subsequent samples. |
| `final void` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#splice()()` Indicates samples that are subsequently queued should be spliced into those already queued. |

| ### Protected methods |
|---|---|
| `https://developer.android.com/reference/androidx/media3/common/Format` | `@https://developer.android.com/reference/androidx/annotation/CallSuper https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#getAdjustedUpstreamFormat(androidx.media3.common.Format)(https://developer.android.com/reference/androidx/media3/common/Format format)` Adjusts the upstream `https://developer.android.com/reference/androidx/media3/common/Format` (i.e., the `https://developer.android.com/reference/androidx/media3/common/Format` that was most recently passed to `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#format(androidx.media3.common.Format)`). |
| `final void` | `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#invalidateUpstreamFormatAdjustment()()` Invalidates the last upstream format adjustment. |

### Inherited Constants

From [androidx.media3.extractor.TrackOutput](https://developer.android.com/reference/androidx/media3/extractor/TrackOutput) `static final int` `https://developer.android.com/reference/androidx/media3/extractor/TrackOutput#SAMPLE_DATA_PART_ENCRYPTION() = 1`

Sample encryption data.
`static final int` `https://developer.android.com/reference/androidx/media3/extractor/TrackOutput#SAMPLE_DATA_PART_MAIN() = 0`

Main media sample data.
`static final int` `https://developer.android.com/reference/androidx/media3/extractor/TrackOutput#SAMPLE_DATA_PART_SUPPLEMENTAL() = 2`

Sample supplemental data.

### Inherited methods

From [androidx.media3.extractor.TrackOutput](https://developer.android.com/reference/androidx/media3/extractor/TrackOutput) `void` `https://developer.android.com/reference/androidx/media3/extractor/TrackOutput#durationUs(long)(long durationUs)`

Called to set the duration of the track in microseconds.
`void` `https://developer.android.com/reference/androidx/media3/extractor/TrackOutput#sampleData(androidx.media3.common.util.ParsableByteArray,int)(https://developer.android.com/reference/androidx/media3/common/util/ParsableByteArray data, int length)`

Equivalent to `https://developer.android.com/reference/androidx/media3/extractor/TrackOutput#sampleData(androidx.media3.common.util.ParsableByteArray,int,int)` sampleData(data, length, SAMPLE_DATA_PART_MAIN)}.
`int` `https://developer.android.com/reference/androidx/media3/extractor/TrackOutput#sampleData(androidx.media3.common.DataReader,int,boolean)(https://developer.android.com/reference/androidx/media3/common/DataReader input, int length, boolean allowEndOfInput)`

Equivalent to `https://developer.android.com/reference/androidx/media3/extractor/TrackOutput#sampleData(androidx.media3.common.DataReader,int,boolean,int)`.

## Public fields

### isLastSampleQueued

```
public boolean isLastSampleQueued
```

### largestQueuedTimestampUs

```
public long largestQueuedTimestampUs
```

### upstreamFormat

```
public @Nullable Format upstreamFormat
```

## Protected constructors

### SampleQueue

```
protected SampleQueue(
    Allocator allocator,
    @Nullable DrmSessionManager drmSessionManager,
    @Nullable DrmSessionEventListener.EventDispatcher drmEventDispatcher
)
```

## Public methods

### createWithDrm

```
public static SampleQueue createWithDrm(
    Allocator allocator,
    DrmSessionManager drmSessionManager,
    DrmSessionEventListener.EventDispatcher drmEventDispatcher
)
```

Creates a sample queue with DRM resource management.

For each sample added to the queue, a `https://developer.android.com/reference/androidx/media3/exoplayer/drm/DrmSession` will be attached containing the keys needed to decrypt it.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Allocator allocator` | An `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Allocator` from which allocations for sample data can be obtained. |
| `https://developer.android.com/reference/androidx/media3/exoplayer/drm/DrmSessionManager drmSessionManager` | The `https://developer.android.com/reference/androidx/media3/exoplayer/drm/DrmSessionManager` to obtain `https://developer.android.com/reference/androidx/media3/exoplayer/drm/DrmSession` from. The created instance does not take ownership of this `https://developer.android.com/reference/androidx/media3/exoplayer/drm/DrmSessionManager`. |
| `https://developer.android.com/reference/androidx/media3/exoplayer/drm/DrmSessionEventListener.EventDispatcher drmEventDispatcher` | A `https://developer.android.com/reference/androidx/media3/exoplayer/drm/DrmSessionEventListener.EventDispatcher` to notify of events related to this SampleQueue. |

### createWithDrm

```
public static SampleQueue [createWithDrm](https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#createWithDrm(androidx.media3.exoplayer.upstream.Allocator,android.os.Looper,androidx.media3.exoplayer.drm.DrmSessionManager,androidx.media3.exoplayer.drm.DrmSessionEventListener.EventDispatcher))(
    Allocator allocator,
    Looper playbackLooper,
    DrmSessionManager drmSessionManager,
    DrmSessionEventListener.EventDispatcher drmEventDispatcher
)
```

> [!CAUTION]
> **This method is deprecated.**   
>
> Use `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#createWithDrm(androidx.media3.exoplayer.upstream.Allocator,androidx.media3.exoplayer.drm.DrmSessionManager,androidx.media3.exoplayer.drm.DrmSessionEventListener.EventDispatcher)` instead. The `playbackLooper` should be configured on the `https://developer.android.com/reference/androidx/media3/exoplayer/drm/DrmSessionManager` with `https://developer.android.com/reference/androidx/media3/exoplayer/drm/DrmSessionManager#setPlayer(android.os.Looper,androidx.media3.exoplayer.analytics.PlayerId)`.

### createWithoutDrm

```
public static SampleQueue createWithoutDrm(Allocator allocator)
```

Creates a sample queue without DRM resource management.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Allocator allocator` | An `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Allocator` from which allocations for sample data can be obtained. |

### discardSampleMetadataToRead

```
synchronized public long discardSampleMetadataToRead()
```

### discardTo

```
public final void discardTo(long timeUs, boolean toKeyframe, boolean stopAtReadPosition)
```

Discards up to but not including the sample immediately before or at the specified time.

| Parameters |
|---|---|
| `long timeUs` | The time to discard up to. |
| `boolean toKeyframe` | If true then discards samples up to the keyframe before or at the specified time, rather than any sample before or at that time. |
| `boolean stopAtReadPosition` | If true then samples are only discarded if they're before the read position. If false then samples at and beyond the read position may be discarded, in which case the read position is advanced to the first remaining sample. |

### discardToEnd

```
public final void discardToEnd()
```

Discards all samples in the queue and advances the read position.

### discardToRead

```
public final void discardToRead()
```

Discards up to but not including the read position.

### discardUpstreamFrom

```
public final void discardUpstreamFrom(long timeUs)
```

Discards samples from the write side of the queue.

| Parameters |
|---|---|
| `long timeUs` | Samples will be discarded from the write end of the queue until a sample with a timestamp smaller than timeUs is encountered (this sample is not discarded). Must be larger than `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#getLargestReadTimestampUs()`. |

### discardUpstreamSamples

```
public final void discardUpstreamSamples(int discardFromIndex)
```

Discards samples from the write side of the queue.

| Parameters |
|---|---|
| `int discardFromIndex` | The absolute index of the first sample to be discarded. Must be in the range \[`https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#getReadIndex()`, `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#getWriteIndex()`\]. |

### expectResume

```
public final void expectResume()
```

Prepares the queue to resume receiving samples from an earlier timestamp, matching duplicate overlap samples at the existing queue tail before appending new samples.

### format

```
public final void format(Format format)
```

Called when the `https://developer.android.com/reference/androidx/media3/common/Format` of the track has been extracted from the stream.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/common/Format format` | The extracted `https://developer.android.com/reference/androidx/media3/common/Format`. |

### getFirstIndex

```
public final int getFirstIndex()
```

Returns the current absolute start index.

### getFirstTimestampUs

```
synchronized public final long getFirstTimestampUs()
```

Returns the timestamp of the first sample, or `https://developer.android.com/reference/java/lang/Long.html#MIN_VALUE` if the queue is empty.

### getLargestQueuedTimestampUs

```
synchronized public final long getLargestQueuedTimestampUs()
```

Returns the largest sample timestamp that has been queued since the last `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#reset()`.

Samples that were discarded by calling `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#discardUpstreamSamples(int)` are not considered as having been queued. Samples that were dequeued from the front of the queue are considered as having been queued.

| Returns |
|---|---|
| `long` | The largest sample timestamp that has been queued, or `https://developer.android.com/reference/java/lang/Long.html#MIN_VALUE` if no samples have been queued. |

### getLargestReadTimestampUs

```
synchronized public final long getLargestReadTimestampUs()
```

Returns the largest sample timestamp that has been read since the last `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#reset()`.

| Returns |
|---|---|
| `long` | The largest sample timestamp that has been read, or `https://developer.android.com/reference/java/lang/Long.html#MIN_VALUE` if no samples have been read. |

### getReadIndex

```
public final int getReadIndex()
```

Returns the current absolute read index.

### getSkipCount

```
synchronized public final int getSkipCount(long timeUs, boolean allowEndOfQueue)
```

Returns the number of samples that need to be `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#skip(int)` to advance the read position to the keyframe before or at the specified time.

| Parameters |
|---|---|
| `long timeUs` | The time to advance to. |
| `boolean allowEndOfQueue` | Whether the end of the queue is considered a keyframe when ` timeUs` is larger than the largest queued timestamp. |

| Returns |
|---|---|
| `int` | The number of samples that need to be skipped, which may be equal to 0. |

### getUpstreamFormat

```
synchronized public final @Nullable Format getUpstreamFormat()
```

Returns the upstream `https://developer.android.com/reference/androidx/media3/common/Format` in which samples are being queued.

### getWriteIndex

```
public final int getWriteIndex()
```

Returns the current absolute write index.

### hasQueuedTimestampsUpToReadEndTimeUs

```
synchronized public final boolean hasQueuedTimestampsUpToReadEndTimeUs()
```

Returns whether all required samples to read up to the configured `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#setReadEndTimeUs(long)` have been queued.

### isDiscardingAllSamplesToStartTime

```
public static boolean isDiscardingAllSamplesToStartTime(Format format)
```

Returns whether samples earlier than `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#setStartTimeUs(long)` will be discarded for the given format.

This returns `true` for audio formats where `https://developer.android.com/reference/androidx/media3/common/MimeTypes#allSamplesAreSyncSamples(java.lang.String,java.lang.String)`.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/common/Format format` | The `https://developer.android.com/reference/androidx/media3/common/Format` to check. |

| Returns |
|---|---|
| `boolean` | Whether all samples earlier than the start time will be discarded for the given format. |

### isLastSampleQueued

```
synchronized public final boolean isLastSampleQueued()
```

Returns whether the last sample of the stream has knowingly been queued. A return value of `false` means that the last sample had not been queued or that it's unknown whether the last sample has been queued.

Samples that were discarded by calling `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#discardUpstreamSamples(int)` are not considered as having been queued. Samples that were dequeued from the front of the queue are considered as having been queued.

### isReady

```
@CallSuper
synchronized public boolean isReady(boolean loadingFinished)
```

Returns whether there is data available for reading.

Note: If the stream has ended then a buffer with the end of stream flag can always be read from `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#read(androidx.media3.exoplayer.FormatHolder,androidx.media3.decoder.DecoderInputBuffer,int,boolean)`. Hence an ended stream is always ready.

| Parameters |
|---|---|
| `boolean loadingFinished` | Whether no more samples will be written to the sample queue. When true, this method returns true if the sample queue is empty, because an empty sample queue means the end of stream has been reached. When false, this method returns false if the sample queue is empty. |

### maybeThrowError

```
@CallSuper
public void maybeThrowError()
```

Throws an error that's preventing data from being read. Does nothing if no such error exists.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/io/IOException.html` | The underlying error. |

### peekSourceId

```
synchronized public final long peekSourceId()
```

Peeks the source id of the next sample to be read, or the current upstream source id if the queue is empty or if the read position is at the end of the queue.

| Returns |
|---|---|
| `long` | The source id. |

### preRelease

```
@CallSuper
public void preRelease()
```

Calls `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#discardToEnd()` and releases any resources owned by the queue.

### read

```
@CallSuper
public int read(
    FormatHolder formatHolder,
    DecoderInputBuffer buffer,
    @SampleStream.ReadFlags int readFlags,
    boolean loadingFinished
)
```

Attempts to read from the queue.

`https://developer.android.com/reference/androidx/media3/common/Format` read from this method may be associated to a `https://developer.android.com/reference/androidx/media3/exoplayer/drm/DrmSession` through `https://developer.android.com/reference/androidx/media3/exoplayer/FormatHolder#drmSession()`.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/exoplayer/FormatHolder formatHolder` | A `https://developer.android.com/reference/androidx/media3/exoplayer/FormatHolder` to populate in the case of reading a format. |
| `https://developer.android.com/reference/androidx/media3/decoder/DecoderInputBuffer buffer` | A `https://developer.android.com/reference/androidx/media3/decoder/DecoderInputBuffer` to populate in the case of reading a sample or the end of the stream. If the end of the stream has been reached, the `https://developer.android.com/reference/androidx/media3/common/C#BUFFER_FLAG_END_OF_STREAM()` flag will be set on the buffer. |
| `@https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleStream.ReadFlags int readFlags` | Flags controlling the behavior of this read operation. |
| `boolean loadingFinished` | True if an empty queue should be considered the end of the stream. |

| Returns |
|---|---|
| `int` | The result, which can be `https://developer.android.com/reference/androidx/media3/common/C#RESULT_NOTHING_READ()`, `https://developer.android.com/reference/androidx/media3/common/C#RESULT_FORMAT_READ()` or `https://developer.android.com/reference/androidx/media3/common/C#RESULT_BUFFER_READ()`. |

| Throws |
|---|---|
| `https://developer.android.com/reference/androidx/media3/decoder/DecoderInputBuffer.InsufficientCapacityException` | If the `buffer` has insufficient capacity to hold the data of a sample being read. The buffer `https://developer.android.com/reference/androidx/media3/decoder/DecoderInputBuffer#timeUs()` and flags are populated if this exception is thrown, but the read position is not advanced. |

### release

```
@CallSuper
public void release()
```

Calls `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#reset(boolean)` and releases any resources owned by the queue.

### reset

```
public final void reset()
```

Convenience method for `reset(false)`.

### reset

```
@CallSuper
public void reset(boolean resetUpstreamFormat)
```

Clears all samples from the queue.

| Parameters |
|---|---|
| `boolean resetUpstreamFormat` | Whether the upstream format should be cleared. If set to false, samples queued after the reset (and before a subsequent call to `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#format(androidx.media3.common.Format)`) are assumed to have the current upstream format. If set to true, `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#format(androidx.media3.common.Format)` must be called after the reset before any more samples can be queued. |

### sampleData

```
public final void sampleData(
    ParsableByteArray data,
    int length,
    @TrackOutput.SampleDataPart int sampleDataPart
)
```

Called to write sample data to the output.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/common/util/ParsableByteArray data` | A `https://developer.android.com/reference/androidx/media3/common/util/ParsableByteArray` from which to read the sample data. |
| `int length` | The number of bytes to read, starting from `data.getPosition()`. |
| `@https://developer.android.com/reference/androidx/media3/extractor/TrackOutput.SampleDataPart int sampleDataPart` | The part of the sample data to which this call corresponds. |

### sampleData

```
public final int sampleData(
    DataReader input,
    int length,
    boolean allowEndOfInput,
    @TrackOutput.SampleDataPart int sampleDataPart
)
```

Called to write sample data to the output.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/common/DataReader input` | A `https://developer.android.com/reference/androidx/media3/common/DataReader` from which to read the sample data. |
| `int length` | The maximum length to read from the input. |
| `boolean allowEndOfInput` | True if encountering the end of the input having read no data is allowed, and should result in `https://developer.android.com/reference/androidx/media3/common/C#RESULT_END_OF_INPUT()` being returned. False if it should be considered an error, causing an `https://developer.android.com/reference/java/io/EOFException.html` to be thrown. |
| `@https://developer.android.com/reference/androidx/media3/extractor/TrackOutput.SampleDataPart int sampleDataPart` | The part of the sample data to which this call corresponds. |

| Returns |
|---|---|
| `int` | The number of bytes appended. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/io/IOException.html` | If an error occurred reading from the input. |

### sampleMetadata

```
public void sampleMetadata(
    long timeUs,
    @C.BufferFlags int flags,
    int size,
    int offset,
    @Nullable TrackOutput.CryptoData cryptoData
)
```

Called when metadata associated with a sample has been extracted from the stream.

The corresponding sample data will have already been passed to the output via calls to `https://developer.android.com/reference/androidx/media3/extractor/TrackOutput#sampleData(androidx.media3.common.DataReader,int,boolean)` or `https://developer.android.com/reference/androidx/media3/extractor/TrackOutput#sampleData(androidx.media3.common.util.ParsableByteArray,int)`.

| Parameters |
|---|---|
| `long timeUs` | The media timestamp associated with the sample, in microseconds. |
| `@https://developer.android.com/reference/androidx/media3/common/C.BufferFlags int flags` | Flags associated with the sample. See `C.BUFFER_FLAG_*`. |
| `int size` | The size of the sample data, in bytes. |
| `int offset` | The number of bytes that have been passed to `https://developer.android.com/reference/androidx/media3/extractor/TrackOutput#sampleData(androidx.media3.common.DataReader,int,boolean)` or `https://developer.android.com/reference/androidx/media3/extractor/TrackOutput#sampleData(androidx.media3.common.util.ParsableByteArray,int)` since the last byte belonging to the sample whose metadata is being passed. |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/extractor/TrackOutput.CryptoData cryptoData` | The encryption data required to decrypt the sample. May be null. |

### seekTo

```
synchronized public final boolean seekTo(int sampleIndex)
```

Attempts to seek the read position to the specified sample index.

| Parameters |
|---|---|
| `int sampleIndex` | The sample index. |

| Returns |
|---|---|
| `boolean` | Whether the seek was successful. |

### seekTo

```
synchronized public final boolean seekTo(long timeUs, boolean allowTimeBeyondBuffer)
```

Attempts to seek the read position to the keyframe before or at the specified time.

For formats where `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#isDiscardingAllSamplesToStartTime(androidx.media3.common.Format)`, it seeks the read position to the first sample at or after the specified time.

| Parameters |
|---|---|
| `long timeUs` | The time to seek to. |
| `boolean allowTimeBeyondBuffer` | Whether the operation can succeed if `timeUs` is beyond the end of the queue, by seeking to the last sample (or keyframe). |

| Returns |
|---|---|
| `boolean` | Whether the seek was successful. |

### setReadEndTimeUs

```
synchronized public final void setReadEndTimeUs(long readEndTimeUs)
```

Sets the time at which reading the stream should stop.

The queue may offer samples beyond this time if they are out of order and required for decoding.

This setting does not influence the upstream write side of the queue.

| Parameters |
|---|---|
| `long readEndTimeUs` | The desired read end time in microseconds, or `https://developer.android.com/reference/androidx/media3/common/C#TIME_END_OF_SOURCE()` to not specify a read limit. |

### setSampleOffsetUs

```
public final void setSampleOffsetUs(long sampleOffsetUs)
```

Sets an offset that will be added to the timestamps (and sub-sample timestamps) of samples that are subsequently queued.

| Parameters |
|---|---|
| `long sampleOffsetUs` | The timestamp offset in microseconds. |

### setStartTimeUs

```
public final void setStartTimeUs(long startTimeUs)
```

Sets the start time for the queue. Samples with earlier timestamps will be discarded if `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#isDiscardingAllSamplesToStartTime(androidx.media3.common.Format)` for the input format.

| Parameters |
|---|---|
| `long startTimeUs` | The start time, in microseconds. |

### setUpstreamFormatChangeListener

```
public final void setUpstreamFormatChangeListener(
    @Nullable SampleQueue.UpstreamFormatChangedListener listener
)
```

Sets a listener to be notified of changes to the upstream format.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue.UpstreamFormatChangedListener listener` | The listener. |

### skip

```
synchronized public final void skip(int count)
```

Advances the read position by the specified number of samples.

| Parameters |
|---|---|
| `int count` | The number of samples to advance the read position by. Must be at least 0 and at most `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#getWriteIndex()` - `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#getReadIndex()`. |

### sourceId

```
public final void sourceId(long sourceId)
```

Sets a source identifier for subsequent samples.

| Parameters |
|---|---|
| `long sourceId` | The source identifier. |

### splice

```
public final void splice()
```

Indicates samples that are subsequently queued should be spliced into those already queued.

## Protected methods

### getAdjustedUpstreamFormat

```
@CallSuper
protected Format getAdjustedUpstreamFormat(Format format)
```

Adjusts the upstream `https://developer.android.com/reference/androidx/media3/common/Format` (i.e., the `https://developer.android.com/reference/androidx/media3/common/Format` that was most recently passed to `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#format(androidx.media3.common.Format)`).

The default implementation incorporates the sample offset passed to `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#setSampleOffsetUs(long)` into `https://developer.android.com/reference/androidx/media3/common/Format#subsampleOffsetUs()`.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/common/Format format` | The `https://developer.android.com/reference/androidx/media3/common/Format` to adjust. |

| Returns |
|---|---|
| `https://developer.android.com/reference/androidx/media3/common/Format` | The adjusted `https://developer.android.com/reference/androidx/media3/common/Format`. |

### invalidateUpstreamFormatAdjustment

```
protected final void invalidateUpstreamFormatAdjustment()
```

Invalidates the last upstream format adjustment. `https://developer.android.com/reference/androidx/media3/exoplayer/source/SampleQueue#getAdjustedUpstreamFormat(androidx.media3.common.Format)` will be called to adjust the upstream `https://developer.android.com/reference/androidx/media3/common/Format` again before the next sample is queued.