Search in sources :

Example 16 with DataSource

use of com.google.android.exoplayer2.upstream.DataSource in project ExoPlayer by google.

the class DashUtil method loadManifest.

/**
   * Loads a DASH manifest.
   *
   * @param dataSource The {@link HttpDataSource} from which the manifest should be read.
   * @param manifestUriString The URI of the manifest to be read.
   * @return An instance of {@link DashManifest}.
   * @throws IOException If an error occurs reading data from the stream.
   * @see DashManifestParser
   */
public static DashManifest loadManifest(DataSource dataSource, String manifestUriString) throws IOException {
    DataSourceInputStream inputStream = new DataSourceInputStream(dataSource, new DataSpec(Uri.parse(manifestUriString), DataSpec.FLAG_ALLOW_CACHING_UNKNOWN_LENGTH));
    try {
        inputStream.open();
        DashManifestParser parser = new DashManifestParser();
        return parser.parse(dataSource.getUri(), inputStream);
    } finally {
        inputStream.close();
    }
}
Also used : DashManifestParser(com.google.android.exoplayer2.source.dash.manifest.DashManifestParser) DataSpec(com.google.android.exoplayer2.upstream.DataSpec) DataSourceInputStream(com.google.android.exoplayer2.upstream.DataSourceInputStream)

Example 17 with DataSource

use of com.google.android.exoplayer2.upstream.DataSource in project ExoPlayer by google.

the class DefaultSsChunkSource method getNextChunk.

@Override
public final void getNextChunk(MediaChunk previous, long playbackPositionUs, ChunkHolder out) {
    if (fatalError != null) {
        return;
    }
    long bufferedDurationUs = previous != null ? (previous.endTimeUs - playbackPositionUs) : 0;
    trackSelection.updateSelectedTrack(bufferedDurationUs);
    StreamElement streamElement = manifest.streamElements[elementIndex];
    if (streamElement.chunkCount == 0) {
        // There aren't any chunks for us to load.
        out.endOfStream = !manifest.isLive;
        return;
    }
    int chunkIndex;
    if (previous == null) {
        chunkIndex = streamElement.getChunkIndex(playbackPositionUs);
    } else {
        chunkIndex = previous.getNextChunkIndex() - currentManifestChunkOffset;
        if (chunkIndex < 0) {
            // This is before the first chunk in the current manifest.
            fatalError = new BehindLiveWindowException();
            return;
        }
    }
    if (chunkIndex >= streamElement.chunkCount) {
        // This is beyond the last chunk in the current manifest.
        out.endOfStream = !manifest.isLive;
        return;
    }
    long chunkStartTimeUs = streamElement.getStartTimeUs(chunkIndex);
    long chunkEndTimeUs = chunkStartTimeUs + streamElement.getChunkDurationUs(chunkIndex);
    int currentAbsoluteChunkIndex = chunkIndex + currentManifestChunkOffset;
    int trackSelectionIndex = trackSelection.getSelectedIndex();
    ChunkExtractorWrapper extractorWrapper = extractorWrappers[trackSelectionIndex];
    int manifestTrackIndex = trackSelection.getIndexInTrackGroup(trackSelectionIndex);
    Uri uri = streamElement.buildRequestUri(manifestTrackIndex, chunkIndex);
    out.chunk = newMediaChunk(trackSelection.getSelectedFormat(), dataSource, uri, null, currentAbsoluteChunkIndex, chunkStartTimeUs, chunkEndTimeUs, trackSelection.getSelectionReason(), trackSelection.getSelectionData(), extractorWrapper);
}
Also used : BehindLiveWindowException(com.google.android.exoplayer2.source.BehindLiveWindowException) ChunkExtractorWrapper(com.google.android.exoplayer2.source.chunk.ChunkExtractorWrapper) StreamElement(com.google.android.exoplayer2.source.smoothstreaming.manifest.SsManifest.StreamElement) Uri(android.net.Uri)

Example 18 with DataSource

use of com.google.android.exoplayer2.upstream.DataSource in project ExoPlayer by google.

the class DefaultSsChunkSource method newMediaChunk.

// Private methods.
private static MediaChunk newMediaChunk(Format format, DataSource dataSource, Uri uri, String cacheKey, int chunkIndex, long chunkStartTimeUs, long chunkEndTimeUs, int trackSelectionReason, Object trackSelectionData, ChunkExtractorWrapper extractorWrapper) {
    DataSpec dataSpec = new DataSpec(uri, 0, C.LENGTH_UNSET, cacheKey);
    // In SmoothStreaming each chunk contains sample timestamps relative to the start of the chunk.
    // To convert them the absolute timestamps, we need to set sampleOffsetUs to chunkStartTimeUs.
    long sampleOffsetUs = chunkStartTimeUs;
    return new ContainerMediaChunk(dataSource, dataSpec, format, trackSelectionReason, trackSelectionData, chunkStartTimeUs, chunkEndTimeUs, chunkIndex, 1, sampleOffsetUs, extractorWrapper);
}
Also used : DataSpec(com.google.android.exoplayer2.upstream.DataSpec) ContainerMediaChunk(com.google.android.exoplayer2.source.chunk.ContainerMediaChunk)

Example 19 with DataSource

use of com.google.android.exoplayer2.upstream.DataSource in project android-UniversalMusicPlayer by googlesamples.

the class LocalPlayback method play.

@Override
public void play(QueueItem item) {
    mPlayOnFocusGain = true;
    tryToGetAudioFocus();
    registerAudioNoisyReceiver();
    String mediaId = item.getDescription().getMediaId();
    boolean mediaHasChanged = !TextUtils.equals(mediaId, mCurrentMediaId);
    if (mediaHasChanged) {
        mCurrentMediaId = mediaId;
    }
    if (mediaHasChanged || mExoPlayer == null) {
        // release everything except the player
        releaseResources(false);
        MediaMetadataCompat track = mMusicProvider.getMusic(MediaIDHelper.extractMusicIDFromMediaID(item.getDescription().getMediaId()));
        String source = track.getString(MusicProviderSource.CUSTOM_METADATA_TRACK_SOURCE);
        if (source != null) {
            // Escape spaces for URLs
            source = source.replaceAll(" ", "%20");
        }
        if (mExoPlayer == null) {
            mExoPlayer = ExoPlayerFactory.newSimpleInstance(mContext, new DefaultTrackSelector(), new DefaultLoadControl());
            mExoPlayer.addListener(mEventListener);
        }
        // Android "O" makes much greater use of AudioAttributes, especially
        // with regards to AudioFocus. All of UAMP's tracks are music, but
        // if your content includes spoken word such as audiobooks or podcasts
        // then the content type should be set to CONTENT_TYPE_SPEECH for those
        // tracks.
        final AudioAttributes audioAttributes = new AudioAttributes.Builder().setContentType(CONTENT_TYPE_MUSIC).setUsage(USAGE_MEDIA).build();
        mExoPlayer.setAudioAttributes(audioAttributes);
        // Produces DataSource instances through which media data is loaded.
        DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(mContext, Util.getUserAgent(mContext, "uamp"), null);
        // Produces Extractor instances for parsing the media data.
        ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
        // The MediaSource represents the media to be played.
        MediaSource mediaSource = new ExtractorMediaSource(Uri.parse(source), dataSourceFactory, extractorsFactory, null, null);
        // Prepares media to play (happens on background thread) and triggers
        // {@code onPlayerStateChanged} callback when the stream is ready to play.
        mExoPlayer.prepare(mediaSource);
        // If we are streaming from the internet, we want to hold a
        // Wifi lock, which prevents the Wifi radio from going to
        // sleep while the song is playing.
        mWifiLock.acquire();
    }
    configurePlayerState();
}
Also used : DefaultExtractorsFactory(com.google.android.exoplayer2.extractor.DefaultExtractorsFactory) MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) AudioAttributes(com.google.android.exoplayer2.audio.AudioAttributes) ExtractorMediaSource(com.google.android.exoplayer2.source.ExtractorMediaSource) DefaultLoadControl(com.google.android.exoplayer2.DefaultLoadControl) DataSource(com.google.android.exoplayer2.upstream.DataSource) ExtractorMediaSource(com.google.android.exoplayer2.source.ExtractorMediaSource) MediaSource(com.google.android.exoplayer2.source.MediaSource) ExtractorsFactory(com.google.android.exoplayer2.extractor.ExtractorsFactory) DefaultExtractorsFactory(com.google.android.exoplayer2.extractor.DefaultExtractorsFactory) DefaultDataSourceFactory(com.google.android.exoplayer2.upstream.DefaultDataSourceFactory) DefaultTrackSelector(com.google.android.exoplayer2.trackselection.DefaultTrackSelector)

Aggregations

DataSpec (com.google.android.exoplayer2.upstream.DataSpec)12 DefaultExtractorInput (com.google.android.exoplayer2.extractor.DefaultExtractorInput)5 ExtractorInput (com.google.android.exoplayer2.extractor.ExtractorInput)5 RangedUri (com.google.android.exoplayer2.source.dash.manifest.RangedUri)4 Representation (com.google.android.exoplayer2.source.dash.manifest.Representation)3 Extractor (com.google.android.exoplayer2.extractor.Extractor)2 BehindLiveWindowException (com.google.android.exoplayer2.source.BehindLiveWindowException)2 ChunkExtractorWrapper (com.google.android.exoplayer2.source.chunk.ChunkExtractorWrapper)2 ContainerMediaChunk (com.google.android.exoplayer2.source.chunk.ContainerMediaChunk)2 InitializationChunk (com.google.android.exoplayer2.source.chunk.InitializationChunk)2 DataSource (com.google.android.exoplayer2.upstream.DataSource)2 DataSourceInputStream (com.google.android.exoplayer2.upstream.DataSourceInputStream)2 Uri (android.net.Uri)1 Handler (android.os.Handler)1 MediaMetadataCompat (android.support.v4.media.MediaMetadataCompat)1 DefaultLoadControl (com.google.android.exoplayer2.DefaultLoadControl)1 Format (com.google.android.exoplayer2.Format)1 AudioAttributes (com.google.android.exoplayer2.audio.AudioAttributes)1 DefaultExtractorsFactory (com.google.android.exoplayer2.extractor.DefaultExtractorsFactory)1 ExtractorsFactory (com.google.android.exoplayer2.extractor.ExtractorsFactory)1