# DefaultBandwidthMeter

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/upstream/DefaultBandwidthMeter.java+class:androidx.media3.exoplayer.upstream.DefaultBandwidthMeter)

*** ** * ** ***

<br />

```
@UnstableApi
public final class DefaultBandwidthMeter implements BandwidthMeter, TransferListener
```

<br />

*** ** * ** ***

Estimates bandwidth by listening to data transfers.

The bandwidth estimate is calculated using a `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/SlidingPercentile` and is updated each time a transfer ends. The initial estimate is based on the current operator's network country code or the locale of the user, as well as the network connection type. This can be configured in the `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultBandwidthMeter.Builder`.

## Summary

| ### Nested types |
|---|
| `public final class https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultBandwidthMeter.Builder` Builder for a bandwidth meter. |
| `public interface https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultBandwidthMeter.InitialBitrateSupplier` Supplies an initial bitrate estimate to the `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultBandwidthMeter`. |

| ### Constants |
|---|---|
| `static final long` | `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultBandwidthMeter#DEFAULT_INITIAL_BITRATE_ESTIMATE() = 1000000` Default initial bitrate estimate used when the device is offline or the network type cannot be determined, in bits per second. |
| `static final https://guava.dev/releases/snapshot-android/api/docs/com/google/common/collect/ImmutableList.html<https://developer.android.com/reference/java/lang/Long.html>` | `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultBandwidthMeter#DEFAULT_INITIAL_BITRATE_ESTIMATES_2G()` Default initial 2G bitrate estimates in bits per second. |
| `static final https://guava.dev/releases/snapshot-android/api/docs/com/google/common/collect/ImmutableList.html<https://developer.android.com/reference/java/lang/Long.html>` | `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultBandwidthMeter#DEFAULT_INITIAL_BITRATE_ESTIMATES_3G()` Default initial 3G bitrate estimates in bits per second. |
| `static final https://guava.dev/releases/snapshot-android/api/docs/com/google/common/collect/ImmutableList.html<https://developer.android.com/reference/java/lang/Long.html>` | `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultBandwidthMeter#DEFAULT_INITIAL_BITRATE_ESTIMATES_4G()` Default initial 4G bitrate estimates in bits per second. |
| `static final https://guava.dev/releases/snapshot-android/api/docs/com/google/common/collect/ImmutableList.html<https://developer.android.com/reference/java/lang/Long.html>` | `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultBandwidthMeter#DEFAULT_INITIAL_BITRATE_ESTIMATES_5G_NSA()` Default initial 5G-NSA bitrate estimates in bits per second. |
| `static final https://guava.dev/releases/snapshot-android/api/docs/com/google/common/collect/ImmutableList.html<https://developer.android.com/reference/java/lang/Long.html>` | `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultBandwidthMeter#DEFAULT_INITIAL_BITRATE_ESTIMATES_5G_SA()` Default initial 5G-SA bitrate estimates in bits per second. |
| `static final https://guava.dev/releases/snapshot-android/api/docs/com/google/common/collect/ImmutableList.html<https://developer.android.com/reference/java/lang/Long.html>` | `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultBandwidthMeter#DEFAULT_INITIAL_BITRATE_ESTIMATES_WIFI()` Default initial Wifi bitrate estimate in bits per second. |
| `static final int` | `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultBandwidthMeter#DEFAULT_SLIDING_WINDOW_MAX_WEIGHT() = 2000` Default maximum weight for the sliding window. |

| ### Public fields |
|---|---|
| `long` | `@https://developer.android.com/reference/androidx/annotation/GuardedBy(value = "this") https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultBandwidthMeter#bitrateEstimate()` |

| ### Public methods |
|---|---|
| `void` | `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultBandwidthMeter#addEventListener(android.os.Handler,androidx.media3.exoplayer.upstream.BandwidthMeter.EventListener)( https://developer.android.com/reference/android/os/Handler.html eventHandler, https://developer.android.com/reference/androidx/media3/exoplayer/upstream/BandwidthMeter.EventListener eventListener )` Adds an `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/BandwidthMeter.EventListener`. |
| `synchronized long` | `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultBandwidthMeter#getBitrateEstimate()()` Returns the estimated bitrate. |
| `synchronized static https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultBandwidthMeter` | `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultBandwidthMeter#getSingletonInstance(android.content.Context)(https://developer.android.com/reference/android/content/Context.html context)` Returns a singleton instance of a `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultBandwidthMeter` with default configuration. |
| `https://developer.android.com/reference/androidx/media3/datasource/TransferListener` | `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultBandwidthMeter#getTransferListener()()` Returns the `https://developer.android.com/reference/androidx/media3/datasource/TransferListener` that this instance uses to gather bandwidth information from data transfers. |
| `synchronized void` | `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultBandwidthMeter#onBytesTransferred(androidx.media3.datasource.DataSource,androidx.media3.datasource.DataSpec,boolean,int)( https://developer.android.com/reference/androidx/media3/datasource/DataSource source, https://developer.android.com/reference/androidx/media3/datasource/DataSpec dataSpec, boolean isNetwork, int bytesTransferred )` Called incrementally during a transfer. |
| `synchronized void` | `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultBandwidthMeter#onTransferEnd(androidx.media3.datasource.DataSource,androidx.media3.datasource.DataSpec,boolean)(https://developer.android.com/reference/androidx/media3/datasource/DataSource source, https://developer.android.com/reference/androidx/media3/datasource/DataSpec dataSpec, boolean isNetwork)` Called when a transfer ends. |
| `void` | `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultBandwidthMeter#onTransferInitializing(androidx.media3.datasource.DataSource,androidx.media3.datasource.DataSpec,boolean)( https://developer.android.com/reference/androidx/media3/datasource/DataSource source, https://developer.android.com/reference/androidx/media3/datasource/DataSpec dataSpec, boolean isNetwork )` Called when a transfer is being initialized. |
| `synchronized void` | `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultBandwidthMeter#onTransferStart(androidx.media3.datasource.DataSource,androidx.media3.datasource.DataSpec,boolean)(https://developer.android.com/reference/androidx/media3/datasource/DataSource source, https://developer.android.com/reference/androidx/media3/datasource/DataSpec dataSpec, boolean isNetwork)` Called when a transfer starts. |
| `void` | `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultBandwidthMeter#removeEventListener(androidx.media3.exoplayer.upstream.BandwidthMeter.EventListener)(https://developer.android.com/reference/androidx/media3/exoplayer/upstream/BandwidthMeter.EventListener eventListener)` Removes an `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/BandwidthMeter.EventListener`. |
| `synchronized void` | `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultBandwidthMeter#setNetworkTypeOverride(int)(@https://developer.android.com/reference/androidx/media3/common/C.NetworkType int networkType)` Overrides the network type. |

### Inherited Constants

From [androidx.media3.exoplayer.upstream.BandwidthMeter](https://developer.android.com/reference/androidx/media3/exoplayer/upstream/BandwidthMeter) `static final https://developer.android.com/reference/androidx/media3/exoplayer/upstream/BandwidthMeter` `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/BandwidthMeter#NO_OP()`

A no-op `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/BandwidthMeter` that doesn't do estimation.

### Inherited methods

From [androidx.media3.exoplayer.upstream.BandwidthMeter](https://developer.android.com/reference/androidx/media3/exoplayer/upstream/BandwidthMeter) `long` `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/BandwidthMeter#getTimeToFirstByteEstimateUs()()`

Returns the estimated time to first byte, in microseconds, or `https://developer.android.com/reference/androidx/media3/common/C#TIME_UNSET()` if no estimate is available.

## Constants

### DEFAULT_INITIAL_BITRATE_ESTIMATE

```
public static final long DEFAULT_INITIAL_BITRATE_ESTIMATE = 1000000
```

Default initial bitrate estimate used when the device is offline or the network type cannot be determined, in bits per second.

### DEFAULT_INITIAL_BITRATE_ESTIMATES_2G

```
public static final ImmutableList<Long> DEFAULT_INITIAL_BITRATE_ESTIMATES_2G
```

Default initial 2G bitrate estimates in bits per second.

### DEFAULT_INITIAL_BITRATE_ESTIMATES_3G

```
public static final ImmutableList<Long> DEFAULT_INITIAL_BITRATE_ESTIMATES_3G
```

Default initial 3G bitrate estimates in bits per second.

### DEFAULT_INITIAL_BITRATE_ESTIMATES_4G

```
public static final ImmutableList<Long> DEFAULT_INITIAL_BITRATE_ESTIMATES_4G
```

Default initial 4G bitrate estimates in bits per second.

### DEFAULT_INITIAL_BITRATE_ESTIMATES_5G_NSA

```
public static final ImmutableList<Long> DEFAULT_INITIAL_BITRATE_ESTIMATES_5G_NSA
```

Default initial 5G-NSA bitrate estimates in bits per second.

### DEFAULT_INITIAL_BITRATE_ESTIMATES_5G_SA

```
public static final ImmutableList<Long> DEFAULT_INITIAL_BITRATE_ESTIMATES_5G_SA
```

Default initial 5G-SA bitrate estimates in bits per second.

### DEFAULT_INITIAL_BITRATE_ESTIMATES_WIFI

```
public static final ImmutableList<Long> DEFAULT_INITIAL_BITRATE_ESTIMATES_WIFI
```

Default initial Wifi bitrate estimate in bits per second.

### DEFAULT_SLIDING_WINDOW_MAX_WEIGHT

```
public static final int DEFAULT_SLIDING_WINDOW_MAX_WEIGHT = 2000
```

Default maximum weight for the sliding window.

## Public fields

### bitrateEstimate

```
@GuardedBy(value = "this")
public long bitrateEstimate
```

## Public methods

### addEventListener

```
public void addEventListener(
    Handler eventHandler,
    BandwidthMeter.EventListener eventListener
)
```

Adds an `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/BandwidthMeter.EventListener`.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/os/Handler.html eventHandler` | A handler for events. |
| `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/BandwidthMeter.EventListener eventListener` | A listener of events. |

### getBitrateEstimate

```
synchronized public long getBitrateEstimate()
```

Returns the estimated bitrate.

### getSingletonInstance

```
synchronized public static DefaultBandwidthMeter getSingletonInstance(Context context)
```

Returns a singleton instance of a `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultBandwidthMeter` with default configuration.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/content/Context.html context` | A `https://developer.android.com/reference/android/content/Context.html`. |

| Returns |
|---|---|
| `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultBandwidthMeter` | The singleton instance. |

### getTransferListener

```
public TransferListener getTransferListener()
```

Returns the `https://developer.android.com/reference/androidx/media3/datasource/TransferListener` that this instance uses to gather bandwidth information from data transfers. May be null if the implementation does not listen to data transfers.

### onBytesTransferred

```
synchronized public void onBytesTransferred(
    DataSource source,
    DataSpec dataSpec,
    boolean isNetwork,
    int bytesTransferred
)
```

Called incrementally during a transfer.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/datasource/DataSource source` | The source performing the transfer. |
| `https://developer.android.com/reference/androidx/media3/datasource/DataSpec dataSpec` | Describes the data being transferred. |
| `boolean isNetwork` | Whether the data is transferred through a network. |
| `int bytesTransferred` | The number of bytes transferred since the previous call to this method. |

### onTransferEnd

```
synchronized public void onTransferEnd(DataSource source, DataSpec dataSpec, boolean isNetwork)
```

Called when a transfer ends.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/datasource/DataSource source` | The source performing the transfer. |
| `https://developer.android.com/reference/androidx/media3/datasource/DataSpec dataSpec` | Describes the data being transferred. |
| `boolean isNetwork` | Whether the data is transferred through a network. |

### onTransferInitializing

```
public void onTransferInitializing(
    DataSource source,
    DataSpec dataSpec,
    boolean isNetwork
)
```

Called when a transfer is being initialized.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/datasource/DataSource source` | The source performing the transfer. |
| `https://developer.android.com/reference/androidx/media3/datasource/DataSpec dataSpec` | Describes the data for which the transfer is initialized. |
| `boolean isNetwork` | Whether the data is transferred through a network. |

### onTransferStart

```
synchronized public void onTransferStart(DataSource source, DataSpec dataSpec, boolean isNetwork)
```

Called when a transfer starts.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/datasource/DataSource source` | The source performing the transfer. |
| `https://developer.android.com/reference/androidx/media3/datasource/DataSpec dataSpec` | Describes the data being transferred. |
| `boolean isNetwork` | Whether the data is transferred through a network. |

### removeEventListener

```
public void removeEventListener(BandwidthMeter.EventListener eventListener)
```

Removes an `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/BandwidthMeter.EventListener`.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/BandwidthMeter.EventListener eventListener` | The listener to be removed. |

### setNetworkTypeOverride

```
synchronized public void setNetworkTypeOverride(@C.NetworkType int networkType)
```

Overrides the network type. Handled in the same way as if the meter had detected a change from the current network type to the specified network type internally.

Applications should not normally call this method. It is intended for testing purposes.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/media3/common/C.NetworkType int networkType` | The overriding network type. |