public abstract class

DataSourceCallback

extends java.lang.Object

implements java.io.Closeable

 java.lang.Object

↳androidx.media2.DataSourceCallback

Gradle dependencies

compile group: 'androidx.media2', name: 'media2', version: '1.0.0-alpha04'

  • groupId: androidx.media2
  • artifactId: media2
  • version: 1.0.0-alpha04

Artifact androidx.media2:media2:1.0.0-alpha04 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.media2:media2 com.android.support:media2

Overview

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

Summary

Constructors
publicDataSourceCallback()

Methods
public abstract longgetSize()

Called to get the size of the data source.

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

Called to request data from the given position.

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public DataSourceCallback()

Methods

public abstract 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.

Parameters:

position: the position in the media item to read from.
buffer: the buffer to read the data into.
offset: the offset within buffer to read the data into.
size: the number of bytes to read.

Returns:

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

public abstract long getSize()

Called to get the size of the data source.

Returns:

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

Source

/*
 * Copyright 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package androidx.media2;

import androidx.annotation.NonNull;

import java.io.Closeable;
import java.io.IOException;

/**
 * For supplying media data, implement this if your app has special requirements for the way media
 * data is obtained.
 *
 * <p class="note">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 DataSourceCallback are visible to future calls. This means
 * you don't need to do your own synchronization unless you're modifying the
 * DataSourceCallback from another thread while it's being used by the media library.</p>
 *
 */
public abstract class DataSourceCallback implements Closeable {
    /**
     * Called to request data from the given position.
     *
     * Implementations should should write up to {@code size} bytes into
     * {@code buffer}, and return the number of bytes written.
     *
     * Return {@code 0} if size is zero (thus no bytes are read).
     *
     * Return {@code -1} to indicate that end of stream is reached.
     *
     * @param position the position in the media item to read from.
     * @param buffer the buffer to read the data into.
     * @param offset the offset within buffer to read the data into.
     * @param size the number of bytes to read.
     * @throws IOException on fatal errors.
     * @return the number of bytes read, or -1 if there was an error.
     */
    public abstract int readAt(long position, @NonNull byte[] buffer, int offset, int size)
            throws IOException;

    /**
     * Called to get the size of the data source.
     *
     * @throws IOException on fatal errors
     * @return the size of data source in bytes, or -1 if the size is unknown.
     */
    public abstract long getSize() throws IOException;
}