Skip to content

Most visited

Recently visited



public abstract class Media2DataSource
extends Object implements Closeable


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 Media2DataSource are visible to future calls. This means you don't need to do your own synchronization unless you're modifying the Media2DataSource from another thread while it's being used by the framework.


Public constructors


Public methods

abstract long getSize()

Called to get the size of the data source.

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

Called to request data from the given position.

Inherited methods

From class java.lang.Object
From interface
From interface java.lang.AutoCloseable

Public constructors


Media2DataSource ()

Public methods


long getSize ()

Called to get the size of the data source.

long the size of data source in bytes, or -1 if the size is unknown.

IOException on fatal errors


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

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

position long: the position in the data source to read from.

buffer byte: the buffer to read the data into.

offset int: the offset within buffer to read the data into.

size int: the number of bytes to read.

int the number of bytes read, or -1 if there was an error.

IOException on fatal errors.
This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields


Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience. (Dec 2017 Android Platform & Tools Survey)