# BinarySearchSeeker

# BinarySearchSeeker

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

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/media3/extractor/BinarySearchSeeker "View this page in Java")

<br />

```
@UnstableApi
abstract class BinarySearchSeeker
```

<br />

*** ** * ** ***

A seeker that supports seeking within a stream by searching for the target frame using binary search.

This seeker operates on a stream that contains multiple frames (or samples). Each frame is associated with some kind of timestamps, such as stream time, or frame indices. Given a target seek time, the seeker will find the corresponding target timestamp, and perform a search operation within the stream to identify the target frame and return the byte position in the stream of the target frame.

## Summary

|                                                                                                                                                                                                                                                                                                                                                                                               ### Nested types                                                                                                                                                                                                                                                                                                                                                                                                |
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `class `[BinarySearchSeeker.BinarySearchSeekMap](https://developer.android.com/reference/kotlin/androidx/media3/extractor/BinarySearchSeeker.BinarySearchSeekMap)` : `[SeekMap](https://developer.android.com/reference/kotlin/androidx/media3/extractor/SeekMap) A[SeekMap](https://developer.android.com/reference/kotlin/androidx/media3/extractor/SeekMap)implementation that returns the estimated byte location from[calculateNextSearchBytePosition](https://developer.android.com/reference/kotlin/androidx/media3/extractor/BinarySearchSeeker.SeekOperationParams#calculateNextSearchBytePosition(long,long,long,long,long,long))for each[getSeekPoints](https://developer.android.com/reference/kotlin/androidx/media3/extractor/BinarySearchSeeker.BinarySearchSeekMap#getSeekPoints(long))query. |
| `class `[BinarySearchSeeker.DefaultSeekTimestampConverter](https://developer.android.com/reference/kotlin/androidx/media3/extractor/BinarySearchSeeker.DefaultSeekTimestampConverter)` : `[BinarySearchSeeker.SeekTimestampConverter](https://developer.android.com/reference/kotlin/androidx/media3/extractor/BinarySearchSeeker.SeekTimestampConverter) A[SeekTimestampConverter](https://developer.android.com/reference/kotlin/androidx/media3/extractor/BinarySearchSeeker.SeekTimestampConverter)implementation that returns the seek time itself as the timestamp for a seek time position.                                                                                                                                                                                                            |
| `protected class `[BinarySearchSeeker.SeekOperationParams](https://developer.android.com/reference/kotlin/androidx/media3/extractor/BinarySearchSeeker.SeekOperationParams) Contains parameters for a pending seek operation by[BinarySearchSeeker](https://developer.android.com/reference/kotlin/androidx/media3/extractor/BinarySearchSeeker).                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| `protected interface `[BinarySearchSeeker.SeekTimestampConverter](https://developer.android.com/reference/kotlin/androidx/media3/extractor/BinarySearchSeeker.SeekTimestampConverter) A converter that converts seek time in stream time into target timestamp for the[BinarySearchSeeker](https://developer.android.com/reference/kotlin/androidx/media3/extractor/BinarySearchSeeker).                                                                                                                                                                                                                                                                                                                                                                                                                      |
| `class `[BinarySearchSeeker.TimestampSearchResult](https://developer.android.com/reference/kotlin/androidx/media3/extractor/BinarySearchSeeker.TimestampSearchResult) Represents possible search results for[searchForTimestamp](https://developer.android.com/reference/kotlin/androidx/media3/extractor/BinarySearchSeeker.TimestampSeeker#searchForTimestamp(androidx.media3.extractor.ExtractorInput,long)).                                                                                                                                                                                                                                                                                                                                                                                              |
| `protected interface `[BinarySearchSeeker.TimestampSeeker](https://developer.android.com/reference/kotlin/androidx/media3/extractor/BinarySearchSeeker.TimestampSeeker) A seeker that looks for a given timestamp from an input.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |

|                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  ### Protected constructors                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [BinarySearchSeeker](https://developer.android.com/reference/kotlin/androidx/media3/extractor/BinarySearchSeeker#BinarySearchSeeker(androidx.media3.extractor.BinarySearchSeeker.SeekTimestampConverter,androidx.media3.extractor.BinarySearchSeeker.TimestampSeeker,long,long,long,long,long,long,int))`(` ` seekTimestampConverter: `[BinarySearchSeeker.SeekTimestampConverter](https://developer.android.com/reference/kotlin/androidx/media3/extractor/BinarySearchSeeker.SeekTimestampConverter)`!,` ` timestampSeeker: `[BinarySearchSeeker.TimestampSeeker](https://developer.android.com/reference/kotlin/androidx/media3/extractor/BinarySearchSeeker.TimestampSeeker)`!,` ` durationUs: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`,` ` floorTimePosition: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`,` ` ceilingTimePosition: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`,` ` floorBytePosition: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`,` ` ceilingBytePosition: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`,` ` approxBytesPerFrame: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`,` ` minimumSearchRange: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) `)` Constructs an instance. |

|                                      ### Public functions                                      |
|------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [SeekMap](https://developer.android.com/reference/kotlin/androidx/media3/extractor/SeekMap)`!` | [getSeekMap](https://developer.android.com/reference/kotlin/androidx/media3/extractor/BinarySearchSeeker#getSeekMap())`()` Returns the seek map for the stream.                                                                                                                                                                                                                                                                                                                                                                          |
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)                    | [handlePendingSeek](https://developer.android.com/reference/kotlin/androidx/media3/extractor/BinarySearchSeeker#handlePendingSeek(androidx.media3.extractor.ExtractorInput,androidx.media3.extractor.PositionHolder))`(` ` input: `[ExtractorInput](https://developer.android.com/reference/kotlin/androidx/media3/extractor/ExtractorInput)`!,` ` seekPositionHolder: `[PositionHolder](https://developer.android.com/reference/kotlin/androidx/media3/extractor/PositionHolder)`!` `)` Continues to handle the pending seek operation. |
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)            | [isSeeking](https://developer.android.com/reference/kotlin/androidx/media3/extractor/BinarySearchSeeker#isSeeking())`()` Returns whether the last operation set by[setSeekTargetUs](https://developer.android.com/reference/kotlin/androidx/media3/extractor/BinarySearchSeeker#setSeekTargetUs(long))is still pending.                                                                                                                                                                                                                  |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                  | [setSeekTargetUs](https://developer.android.com/reference/kotlin/androidx/media3/extractor/BinarySearchSeeker#setSeekTargetUs(long))`(timeUs: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`)` Sets the target time in microseconds within the stream to seek to.                                                                                                                                                                                                                                       |

|                                                                   ### Protected functions                                                                    |
|--------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [BinarySearchSeeker.SeekOperationParams](https://developer.android.com/reference/kotlin/androidx/media3/extractor/BinarySearchSeeker.SeekOperationParams)`!` | [createSeekParamsForTargetTimeUs](https://developer.android.com/reference/kotlin/androidx/media3/extractor/BinarySearchSeeker#createSeekParamsForTargetTimeUs(long))`(timeUs: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`)`                                                                                                                                                                                                                                                                                                                       |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                                                                | [markSeekOperationFinished](https://developer.android.com/reference/kotlin/androidx/media3/extractor/BinarySearchSeeker#markSeekOperationFinished(boolean,long))`(` ` foundTargetFrame: `[Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)`,` ` resultPosition: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html) `)`                                                                                                                                                                                                  |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                                                                | [onSeekOperationFinished](https://developer.android.com/reference/kotlin/androidx/media3/extractor/BinarySearchSeeker#onSeekOperationFinished(boolean,long))`(foundTargetFrame: `[Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)`, resultPosition: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`)`                                                                                                                                                                                                              |
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)                                                                                  | [seekToPosition](https://developer.android.com/reference/kotlin/androidx/media3/extractor/BinarySearchSeeker#seekToPosition(androidx.media3.extractor.ExtractorInput,long,androidx.media3.extractor.PositionHolder))`(` ` input: `[ExtractorInput](https://developer.android.com/reference/kotlin/androidx/media3/extractor/ExtractorInput)`!,` ` position: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`,` ` seekPositionHolder: `[PositionHolder](https://developer.android.com/reference/kotlin/androidx/media3/extractor/PositionHolder)`!` `)` |
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)                                                                          | [skipInputUntilPosition](https://developer.android.com/reference/kotlin/androidx/media3/extractor/BinarySearchSeeker#skipInputUntilPosition(androidx.media3.extractor.ExtractorInput,long))`(input: `[ExtractorInput](https://developer.android.com/reference/kotlin/androidx/media3/extractor/ExtractorInput)`!, position: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`)`                                                                                                                                                                         |

|                                                                   ### Protected properties                                                                   |
|--------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------|
| [BinarySearchSeeker.BinarySearchSeekMap](https://developer.android.com/reference/kotlin/androidx/media3/extractor/BinarySearchSeeker.BinarySearchSeekMap)`!` | [seekMap](https://developer.android.com/reference/kotlin/androidx/media3/extractor/BinarySearchSeeker#seekMap())                         |
| [BinarySearchSeeker.SeekOperationParams](https://developer.android.com/reference/kotlin/androidx/media3/extractor/BinarySearchSeeker.SeekOperationParams)`?` | [seekOperationParams](https://developer.android.com/reference/kotlin/androidx/media3/extractor/BinarySearchSeeker#seekOperationParams()) |
| [BinarySearchSeeker.TimestampSeeker](https://developer.android.com/reference/kotlin/androidx/media3/extractor/BinarySearchSeeker.TimestampSeeker)`!`         | [timestampSeeker](https://developer.android.com/reference/kotlin/androidx/media3/extractor/BinarySearchSeeker#timestampSeeker())         |

## Protected constructors

### BinarySearchSeeker

```
protected BinarySearchSeeker(
    seekTimestampConverter: BinarySearchSeeker.SeekTimestampConverter!,
    timestampSeeker: BinarySearchSeeker.TimestampSeeker!,
    durationUs: Long,
    floorTimePosition: Long,
    ceilingTimePosition: Long,
    floorBytePosition: Long,
    ceilingBytePosition: Long,
    approxBytesPerFrame: Long,
    minimumSearchRange: Int
)
```

Constructs an instance.  

|                                                                                          Parameters                                                                                          |
|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `seekTimestampConverter: `[BinarySearchSeeker.SeekTimestampConverter](https://developer.android.com/reference/kotlin/androidx/media3/extractor/BinarySearchSeeker.SeekTimestampConverter)`!` | The[SeekTimestampConverter](https://developer.android.com/reference/kotlin/androidx/media3/extractor/BinarySearchSeeker.SeekTimestampConverter)that converts seek time in stream time into target timestamp.                     |
| `timestampSeeker: `[BinarySearchSeeker.TimestampSeeker](https://developer.android.com/reference/kotlin/androidx/media3/extractor/BinarySearchSeeker.TimestampSeeker)`!`                      | A[TimestampSeeker](https://developer.android.com/reference/kotlin/androidx/media3/extractor/BinarySearchSeeker.TimestampSeeker)that will be used to search for timestamps within the stream.                                     |
| `durationUs: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)                                                                                                  | The duration of the stream in microseconds.                                                                                                                                                                                      |
| `floorTimePosition: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)                                                                                           | The minimum timestamp value (inclusive) in the stream.                                                                                                                                                                           |
| `ceilingTimePosition: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)                                                                                         | The minimum timestamp value (exclusive) in the stream.                                                                                                                                                                           |
| `floorBytePosition: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)                                                                                           | The starting position of the frame with minimum timestamp value (inclusive) in the stream.                                                                                                                                       |
| `ceilingBytePosition: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)                                                                                         | The position after the frame with maximum timestamp value in the stream.                                                                                                                                                         |
| `approxBytesPerFrame: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)                                                                                         | Approximated bytes per frame.                                                                                                                                                                                                    |
| `minimumSearchRange: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)                                                                                            | The minimum byte range that this binary seeker will operate on. If the remaining search range is smaller than this value, the search will stop, and the seeker will return the position at the floor of the range as the result. |

## Public functions

### getSeekMap

```
fun getSeekMap(): SeekMap!
```

Returns the seek map for the stream.  

### handlePendingSeek

```
fun handlePendingSeek(
    input: ExtractorInput!,
    seekPositionHolder: PositionHolder!
): Int
```

Continues to handle the pending seek operation. Returns one of the`RESULT_`values from[Extractor](https://developer.android.com/reference/kotlin/androidx/media3/extractor/Extractor).  

|                                                             Parameters                                                             |
|------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `input: `[ExtractorInput](https://developer.android.com/reference/kotlin/androidx/media3/extractor/ExtractorInput)`!`              | The[ExtractorInput](https://developer.android.com/reference/kotlin/androidx/media3/extractor/ExtractorInput)from which data should be read.                                                     |
| `seekPositionHolder: `[PositionHolder](https://developer.android.com/reference/kotlin/androidx/media3/extractor/PositionHolder)`!` | If[RESULT_SEEK](https://developer.android.com/reference/kotlin/androidx/media3/extractor/Extractor#RESULT_SEEK())is returned, this holder is updated to hold the position of the required seek. |

|                                   Returns                                   |
|-----------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------|
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | One of the`RESULT_`values defined in[Extractor](https://developer.android.com/reference/kotlin/androidx/media3/extractor/Extractor). |

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

### isSeeking

```
fun isSeeking(): Boolean
```

Returns whether the last operation set by[setSeekTargetUs](https://developer.android.com/reference/kotlin/androidx/media3/extractor/BinarySearchSeeker#setSeekTargetUs(long))is still pending.  

### setSeekTargetUs

```
fun setSeekTargetUs(timeUs: Long): Unit
```

Sets the target time in microseconds within the stream to seek to.  

|                                       Parameters                                        |
|-----------------------------------------------------------------------------------------|----------------------------------------------------|
| `timeUs: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html) | The target time in microseconds within the stream. |

## Protected functions

### createSeekParamsForTargetTimeUs

```
protected fun createSeekParamsForTargetTimeUs(timeUs: Long): BinarySearchSeeker.SeekOperationParams!
```  

### markSeekOperationFinished

```
protected fun markSeekOperationFinished(
    foundTargetFrame: Boolean,
    resultPosition: Long
): Unit
```  

### onSeekOperationFinished

```
protected fun onSeekOperationFinished(foundTargetFrame: Boolean, resultPosition: Long): Unit
```  

### seekToPosition

```
protected fun seekToPosition(
    input: ExtractorInput!,
    position: Long,
    seekPositionHolder: PositionHolder!
): Int
```  

### skipInputUntilPosition

```
protected fun skipInputUntilPosition(input: ExtractorInput!, position: Long): Boolean
```  

|                                         Throws                                          |
|-----------------------------------------------------------------------------------------|---|
| [java.io.IOException](https://developer.android.com/reference/java/io/IOException.html) |   |

## Protected properties

### seekMap

```
protected val seekMap: BinarySearchSeeker.BinarySearchSeekMap!
```  

### seekOperationParams

```
protected val seekOperationParams: BinarySearchSeeker.SeekOperationParams?
```  

### timestampSeeker

```
protected val timestampSeeker: BinarySearchSeeker.TimestampSeeker!
```