use of com.google.android.exoplayer2.DefaultLoadControl in project MusicLibrary by lizixian18.
the class ExoPlayback method play.
@Override
public void play(SongInfo info) {
mPlayOnFocusGain = true;
mFocusAndLockManager.tryToGetAudioFocus();
registerAudioNoisyReceiver();
String mediaId = info.getSongId();
boolean mediaHasChanged = !TextUtils.equals(mediaId, mCurrentMediaId);
if (mediaHasChanged) {
mCurrentMediaId = mediaId;
mCurrentMediaSongInfo = info;
}
if (mediaHasChanged || mExoPlayer == null) {
// release everything except the player
releaseResources(false);
String source = info.getSongUrl();
if (source != null) {
// Escape spaces for URLs
source = source.replaceAll(" ", "%20");
}
if (TextUtils.isEmpty(source)) {
if (mCallback != null) {
mCallback.onError("song url is null");
}
return;
}
Uri playUri;
if (BaseUtil.isOnLineSource(source)) {
String proxyUrl;
if (isOpenCacheWhenPlaying && (getMediaType(null, Uri.parse(source)) == C.TYPE_OTHER)) {
proxyUrl = mProxyCacheServer.getProxyUrl(source);
} else {
proxyUrl = source;
}
playUri = Uri.parse(proxyUrl);
} else {
playUri = BaseUtil.getLocalSourceUri(source);
}
if (playUri == null) {
if (mCallback != null) {
mCallback.onError("song uri is null");
}
return;
}
LogUtil.i("isOpenCacheWhenPlaying = " + isOpenCacheWhenPlaying + " playUri = " + playUri.toString());
if (mExoPlayer == null) {
mExoPlayer = ExoPlayerFactory.newSimpleInstance(new DefaultRenderersFactory(mContext), new DefaultTrackSelector(), new DefaultLoadControl());
mExoPlayer.addListener(mEventListener);
}
final AudioAttributes audioAttributes = new AudioAttributes.Builder().setContentType(CONTENT_TYPE_MUSIC).setUsage(USAGE_MEDIA).build();
mExoPlayer.setAudioAttributes(audioAttributes);
MediaSource mediaSource = buildMediaSource(playUri, null, null, null);
mExoPlayer.prepare(mediaSource);
mFocusAndLockManager.acquireWifiLock();
}
configurePlayerState();
}
use of com.google.android.exoplayer2.DefaultLoadControl in project ExoPlayer by google.
the class ExoPlayerTest method infiniteLoading_withSmallAllocations_oomIsPreventedByLoadControl_andThrowsStuckBufferingIllegalStateException.
@Test
public void infiniteLoading_withSmallAllocations_oomIsPreventedByLoadControl_andThrowsStuckBufferingIllegalStateException() {
DefaultLoadControl loadControl = new DefaultLoadControl.Builder().setTargetBufferBytes(10 * C.DEFAULT_BUFFER_SEGMENT_SIZE).build();
// Return no end of stream signal to prevent playback from ending.
FakeMediaPeriod.TrackDataFactory trackDataWithoutEos = (format, periodId) -> ImmutableList.of();
MediaSource continuouslyAllocatingMediaSource = new FakeMediaSource(new FakeTimeline(), ExoPlayerTestRunner.VIDEO_FORMAT) {
@Override
protected MediaPeriod createMediaPeriod(MediaPeriodId id, TrackGroupArray trackGroupArray, Allocator allocator, MediaSourceEventListener.EventDispatcher mediaSourceEventDispatcher, DrmSessionManager drmSessionManager, DrmSessionEventListener.EventDispatcher drmEventDispatcher, @Nullable TransferListener transferListener) {
return new FakeMediaPeriod(trackGroupArray, allocator, trackDataWithoutEos, mediaSourceEventDispatcher, drmSessionManager, drmEventDispatcher, /* deferOnPrepared= */
false) {
private final List<Allocation> allocations = new ArrayList<>();
private Callback callback;
@Override
public synchronized void prepare(Callback callback, long positionUs) {
this.callback = callback;
super.prepare(callback, positionUs);
}
@Override
public long getBufferedPositionUs() {
// Pretend not to make loading progress, so that continueLoading keeps being called.
return 0;
}
@Override
public long getNextLoadPositionUs() {
// Pretend not to make loading progress, so that continueLoading keeps being called.
return 0;
}
@Override
public boolean continueLoading(long positionUs) {
allocations.add(allocator.allocate());
callback.onContinueLoadingRequested(this);
return true;
}
};
}
};
ExoPlayerTestRunner testRunner = new ExoPlayerTestRunner.Builder(context).setMediaSources(continuouslyAllocatingMediaSource).setLoadControl(loadControl).build();
ExoPlaybackException exception = assertThrows(ExoPlaybackException.class, () -> testRunner.start().blockUntilEnded(TIMEOUT_MS));
assertThat(exception.type).isEqualTo(ExoPlaybackException.TYPE_UNEXPECTED);
assertThat(exception.getUnexpectedException()).isInstanceOf(IllegalStateException.class);
}
use of com.google.android.exoplayer2.DefaultLoadControl in project ExoPlayer by google.
the class Transformer method startTransformation.
private void startTransformation(MediaItem mediaItem, Muxer muxer) {
verifyApplicationThread();
if (player != null) {
throw new IllegalStateException("There is already a transformation in progress.");
}
MuxerWrapper muxerWrapper = new MuxerWrapper(muxer, muxerFactory, containerMimeType);
this.muxerWrapper = muxerWrapper;
DefaultTrackSelector trackSelector = new DefaultTrackSelector(context);
trackSelector.setParameters(new DefaultTrackSelector.ParametersBuilder(context).setForceHighestSupportedBitrate(true).build());
// Arbitrarily decrease buffers for playback so that samples start being sent earlier to the
// muxer (rebuffers are less problematic for the transformation use case).
DefaultLoadControl loadControl = new DefaultLoadControl.Builder().setBufferDurationsMs(DEFAULT_MIN_BUFFER_MS, DEFAULT_MAX_BUFFER_MS, DEFAULT_BUFFER_FOR_PLAYBACK_MS / 10, DEFAULT_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS / 10).build();
ExoPlayer.Builder playerBuilder = new ExoPlayer.Builder(context, new TransformerRenderersFactory(context, muxerWrapper, removeAudio, removeVideo, transformationRequest, encoderFactory, decoderFactory, new FallbackListener(mediaItem, listeners, transformationRequest), debugViewProvider)).setMediaSourceFactory(mediaSourceFactory).setTrackSelector(trackSelector).setLoadControl(loadControl).setLooper(looper);
if (clock != Clock.DEFAULT) {
// Transformer.Builder#setClock is also @VisibleForTesting, so if we're using a non-default
// clock we must be in a test context.
@SuppressWarnings("VisibleForTests") ExoPlayer.Builder unusedForAnnotation = playerBuilder.setClock(clock);
}
player = playerBuilder.build();
player.setMediaItem(mediaItem);
player.addListener(new TransformerPlayerListener(mediaItem, muxerWrapper));
player.prepare();
progressState = PROGRESS_STATE_WAITING_FOR_AVAILABILITY;
}
use of com.google.android.exoplayer2.DefaultLoadControl in project ExoPlayer by google.
the class ExoPlayerTest method loadControlNeverWantsToPlay_playbackDoesNotGetStuck.
@Test
public void loadControlNeverWantsToPlay_playbackDoesNotGetStuck() throws Exception {
LoadControl neverLoadingOrPlayingLoadControl = new DefaultLoadControl() {
@Override
public boolean shouldContinueLoading(long playbackPositionUs, long bufferedDurationUs, float playbackSpeed) {
return true;
}
@Override
public boolean shouldStartPlayback(long bufferedDurationUs, float playbackSpeed, boolean rebuffering, long targetLiveOffsetUs) {
return false;
}
};
// Use chunked data to ensure the player actually needs to continue loading and playing.
FakeAdaptiveDataSet.Factory dataSetFactory = new FakeAdaptiveDataSet.Factory(/* chunkDurationUs= */
500_000, /* bitratePercentStdDev= */
10.0, new Random(0));
MediaSource chunkedMediaSource = new FakeAdaptiveMediaSource(new FakeTimeline(), new TrackGroupArray(new TrackGroup(ExoPlayerTestRunner.VIDEO_FORMAT)), new FakeChunkSource.Factory(dataSetFactory, new FakeDataSource.Factory()));
new ExoPlayerTestRunner.Builder(context).setLoadControl(neverLoadingOrPlayingLoadControl).setMediaSources(chunkedMediaSource).build().start().blockUntilEnded(TIMEOUT_MS);
}
Aggregations