Added in [API level 23](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Ctors](https://developer.android.com/reference/android/media/MediaDataSource#pubctors) \| [Methods](https://developer.android.com/reference/android/media/MediaDataSource#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/media/MediaDataSource#inhmethods)

# MediaDataSource

*** ** * ** ***

` public abstract class MediaDataSource `   
` extends https://developer.android.com/reference/java/lang/Object ` ` implements https://developer.android.com/reference/java/io/Closeable `

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.media.MediaDataSource |

<br />

*** ** * ** ***

For supplying media data to the framework. Implement this if your app has special requirements for the way media data is obtained.

Methods of this interface may be called on multiple different threads. There will be a thread synchronization point between each call to ensure that modifications to the state of your MediaDataSource are visible to future calls. This means you don't need to do your own synchronization unless you're modifying the MediaDataSource from another thread while it's being used by the framework.

<br />

## Summary

| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/android/media/MediaDataSource#MediaDataSource()() ` |

| ### Public methods ||
|---|---|
| ` abstract long` | ` https://developer.android.com/reference/android/media/MediaDataSource#getSize()() ` Called to get the size of the data source. |
| ` abstract int` | ` https://developer.android.com/reference/android/media/MediaDataSource#readAt(long,%20byte[],%20int,%20int)(long position, byte[] buffer, int offset, int size) ` Called to request data from the given position. |

### Inherited methods

From class ` https://developer.android.com/reference/java/lang/Object ` ` https://developer.android.com/reference/java/lang/Object` ` https://developer.android.com/reference/java/lang/Object#clone()() `

Creates and returns a copy of this object.
` boolean` ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) `

Indicates whether some other object is "equal to" this one.
` void` ` https://developer.android.com/reference/java/lang/Object#finalize()() `

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
` final https://developer.android.com/reference/java/lang/Class<?>` ` https://developer.android.com/reference/java/lang/Object#getClass()() `

Returns the runtime class of this `Object`.
` int` ` https://developer.android.com/reference/java/lang/Object#hashCode()() `

Returns a hash code value for the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#notify()() `

Wakes up a single thread that is waiting on this object's monitor.
` final void` ` https://developer.android.com/reference/java/lang/Object#notifyAll()() `

Wakes up all threads that are waiting on this object's monitor.
` https://developer.android.com/reference/java/lang/String` ` https://developer.android.com/reference/java/lang/Object#toString()() `

Returns a string representation of the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait()() `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*.
From interface ` https://developer.android.com/reference/java/io/Closeable ` ` abstract void` ` https://developer.android.com/reference/java/io/Closeable#close()() `

Closes this stream and releases any system resources associated with it.
From interface ` https://developer.android.com/reference/java/lang/AutoCloseable ` ` abstract void` ` https://developer.android.com/reference/java/lang/AutoCloseable#close()() `

Closes this resource, relinquishing any underlying resources.

## Public constructors

### MediaDataSource

```
public MediaDataSource ()
```

<br />

<br />

## Public methods

### getSize

Added in [API level 23](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract long getSize ()
```

Called to get the size of the data source.

<br />

| Returns ||
|---|---|
| `long` | the size of data source in bytes, or -1 if the size is unknown. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | on fatal errors |

### readAt

Added in [API level 23](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract int readAt (long position, 
                byte[] buffer, 
                int offset, 
                int size)
```

Called to request data from the given position. Implementations should fill `buffer` with up to `size` bytes of data, and return the number of valid bytes in the buffer. Return `0` if size is zero (thus no bytes are read). Return `-1` to indicate that end of stream is reached.

<br />

| Parameters ||
|---|---|
| `position` | `long`: the position in the data source to read from. <br /> |
| `buffer` | `byte`: the buffer to read the data into. <br /> |
| `offset` | `int`: the offset within buffer to read the data into. <br /> |
| `size` | `int`: the number of bytes to read. <br /> |

| Returns ||
|---|---|
| `int` | the number of bytes read, or -1 if end of stream is reached. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | on fatal errors. |