Search in sources :

Example 6 with Listener

use of com.google.android.exoplayer2.source.MediaSource.Listener in project ExoPlayer by google.

the class OkHttpDataSource method open.

@Override
public long open(DataSpec dataSpec) throws HttpDataSourceException {
    this.dataSpec = dataSpec;
    this.bytesRead = 0;
    this.bytesSkipped = 0;
    Request request = makeRequest(dataSpec);
    try {
        response = callFactory.newCall(request).execute();
        responseByteStream = response.body().byteStream();
    } catch (IOException e) {
        throw new HttpDataSourceException("Unable to connect to " + dataSpec.uri.toString(), e, dataSpec, HttpDataSourceException.TYPE_OPEN);
    }
    int responseCode = response.code();
    // Check for a valid response code.
    if (!response.isSuccessful()) {
        Map<String, List<String>> headers = request.headers().toMultimap();
        closeConnectionQuietly();
        InvalidResponseCodeException exception = new InvalidResponseCodeException(responseCode, headers, dataSpec);
        if (responseCode == 416) {
            exception.initCause(new DataSourceException(DataSourceException.POSITION_OUT_OF_RANGE));
        }
        throw exception;
    }
    // Check for a valid content type.
    MediaType mediaType = response.body().contentType();
    String contentType = mediaType != null ? mediaType.toString() : null;
    if (contentTypePredicate != null && !contentTypePredicate.evaluate(contentType)) {
        closeConnectionQuietly();
        throw new InvalidContentTypeException(contentType, dataSpec);
    }
    // If we requested a range starting from a non-zero position and received a 200 rather than a
    // 206, then the server does not support partial requests. We'll need to manually skip to the
    // requested position.
    bytesToSkip = responseCode == 200 && dataSpec.position != 0 ? dataSpec.position : 0;
    // Determine the length of the data to be read, after skipping.
    if (dataSpec.length != C.LENGTH_UNSET) {
        bytesToRead = dataSpec.length;
    } else {
        long contentLength = response.body().contentLength();
        bytesToRead = contentLength != -1 ? (contentLength - bytesToSkip) : C.LENGTH_UNSET;
    }
    opened = true;
    if (listener != null) {
        listener.onTransferStart(this, dataSpec);
    }
    return bytesToRead;
}
Also used : DataSourceException(com.google.android.exoplayer2.upstream.DataSourceException) Request(okhttp3.Request) MediaType(okhttp3.MediaType) List(java.util.List) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException)

Example 7 with Listener

use of com.google.android.exoplayer2.source.MediaSource.Listener in project ExoPlayer by google.

the class ClippingMediaSourceTest method getClippedTimeline.

/**
   * Wraps the specified timeline in a {@link ClippingMediaSource} and returns the clipped timeline.
   */
private Timeline getClippedTimeline(Timeline timeline, long startMs, long endMs) {
    mockMediaSourceSourceWithTimeline(timeline);
    new ClippingMediaSource(mockMediaSource, startMs, endMs).prepareSource(null, true, new Listener() {

        @Override
        public void onSourceInfoRefreshed(Timeline timeline, Object manifest) {
            clippedTimeline = timeline;
        }
    });
    return clippedTimeline;
}
Also used : Timeline(com.google.android.exoplayer2.Timeline) Listener(com.google.android.exoplayer2.source.MediaSource.Listener)

Example 8 with Listener

use of com.google.android.exoplayer2.source.MediaSource.Listener in project ExoPlayer by google.

the class SsMediaSource method prepareSource.

// MediaSource implementation.
@Override
public void prepareSource(ExoPlayer player, boolean isTopLevelSource, Listener listener) {
    sourceListener = listener;
    if (manifest != null) {
        manifestLoaderErrorThrower = new LoaderErrorThrower.Dummy();
        processManifest();
    } else {
        manifestDataSource = manifestDataSourceFactory.createDataSource();
        manifestLoader = new Loader("Loader:Manifest");
        manifestLoaderErrorThrower = manifestLoader;
        manifestRefreshHandler = new Handler();
        startLoadingManifest();
    }
}
Also used : LoaderErrorThrower(com.google.android.exoplayer2.upstream.LoaderErrorThrower) Loader(com.google.android.exoplayer2.upstream.Loader) Handler(android.os.Handler)

Example 9 with Listener

use of com.google.android.exoplayer2.source.MediaSource.Listener in project ExoPlayer by google.

the class ConcatenatingMediaSource method handleSourceInfoRefreshed.

private void handleSourceInfoRefreshed(int sourceFirstIndex, Timeline sourceTimeline, Object sourceManifest) {
    // Set the timeline and manifest.
    timelines[sourceFirstIndex] = sourceTimeline;
    manifests[sourceFirstIndex] = sourceManifest;
    // Also set the timeline and manifest for any duplicate entries of the same source.
    for (int i = sourceFirstIndex + 1; i < mediaSources.length; i++) {
        if (mediaSources[i] == mediaSources[sourceFirstIndex]) {
            timelines[i] = sourceTimeline;
            manifests[i] = sourceManifest;
        }
    }
    for (Timeline timeline : timelines) {
        if (timeline == null) {
            // Don't invoke the listener until all sources have timelines.
            return;
        }
    }
    timeline = new ConcatenatedTimeline(timelines.clone());
    listener.onSourceInfoRefreshed(timeline, manifests.clone());
}
Also used : Timeline(com.google.android.exoplayer2.Timeline)

Aggregations

Handler (android.os.Handler)2 Timeline (com.google.android.exoplayer2.Timeline)2 MediaCodecAudioRenderer (com.google.android.exoplayer2.audio.MediaCodecAudioRenderer)2 MetadataRenderer (com.google.android.exoplayer2.metadata.MetadataRenderer)2 TextRenderer (com.google.android.exoplayer2.text.TextRenderer)2 DataSourceException (com.google.android.exoplayer2.upstream.DataSourceException)2 Loader (com.google.android.exoplayer2.upstream.Loader)2 LoaderErrorThrower (com.google.android.exoplayer2.upstream.LoaderErrorThrower)2 MediaCodecVideoRenderer (com.google.android.exoplayer2.video.MediaCodecVideoRenderer)2 AudioProcessor (com.google.android.exoplayer2.audio.AudioProcessor)1 Listener (com.google.android.exoplayer2.source.MediaSource.Listener)1 HlsPlaylistTracker (com.google.android.exoplayer2.source.hls.playlist.HlsPlaylistTracker)1 IOException (java.io.IOException)1 InterruptedIOException (java.io.InterruptedIOException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 List (java.util.List)1 MediaType (okhttp3.MediaType)1 Request (okhttp3.Request)1