Search in sources :

Example 21 with Player

use of com.google.android.exoplayer2.Player in project android by owncloud.

the class PreviewVideoActivity method preparePlayer.

// Video player internal methods
private void preparePlayer() {
    // Create a default TrackSelector
    AdaptiveTrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory();
    trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
    player = new SimpleExoPlayer.Builder(this).setTrackSelector(trackSelector).setLoadControl(new DefaultLoadControl()).build();
    player.addListener(this);
    exoPlayerView.setPlayer(player);
    // Prepare video player asynchronously
    new PrepareVideoPlayerAsyncTask(getApplicationContext(), this, getFile(), getAccount()).execute();
}
Also used : AdaptiveTrackSelection(com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection) DefaultTrackSelector(com.google.android.exoplayer2.trackselection.DefaultTrackSelector) DefaultLoadControl(com.google.android.exoplayer2.DefaultLoadControl)

Example 22 with Player

use of com.google.android.exoplayer2.Player in project android by owncloud.

the class PreviewVideoErrorAdapter method handlePlayerSourceError.

/**
 * Handle video player source exceptions and create a PreviewVideoError with the appropriate info
 *
 * @param error   Exoplayer source exception
 * @param context
 * @return preview video error after processing the Exoplayer source exception
 */
private static PreviewVideoError handlePlayerSourceError(ExoPlaybackException error, Context context) {
    // PreviewVideoError previewVideoError;
    final IOException sourceException = error.getSourceException();
    final Throwable cause = sourceException.getCause();
    if (cause != null) {
        if (cause.getCause() instanceof CertificateException) {
            return new PreviewVideoError(context.getString(R.string.streaming_certificate_error), true, false);
        }
        // Cannot connect with the server
        if (sourceException.getCause() instanceof UnknownHostException) {
            return new PreviewVideoError(context.getString(R.string.network_error_socket_exception), false, false);
        }
        // this case, the parent folder is refreshed and login view is shown
        if (sourceException.getCause() != null && sourceException.getCause() instanceof EOFException) {
            return new PreviewVideoError(context.getString(R.string.streaming_position_not_available), false, true);
        }
    }
    // To handle this case, the parent folder is refreshed and login view is shown
    if (sourceException instanceof UnrecognizedInputFormatException) {
        return new PreviewVideoError(context.getString(R.string.streaming_unrecognized_input), true, true);
    }
    if (sourceException instanceof HttpDataSource.InvalidResponseCodeException) {
        // Video file no longer exists in the server
        if (((HttpDataSource.InvalidResponseCodeException) sourceException).responseCode == NOT_FOUND_ERROR) {
            return new PreviewVideoError(context.getString(R.string.streaming_file_not_found_error), false, false);
        }
        // redirections are allowed, but crossed redirections not
        if ((((HttpDataSource.InvalidResponseCodeException) sourceException).responseCode == TEMPORARY_REDIRECTION)) {
            return new PreviewVideoError(context.getString(R.string.streaming_crossed_redirection), true, false);
        }
    }
    // if error could not be detected properly
    return new PreviewVideoError(context.getString(R.string.previewing_video_common_error), true, false);
}
Also used : UnrecognizedInputFormatException(com.google.android.exoplayer2.source.UnrecognizedInputFormatException) UnknownHostException(java.net.UnknownHostException) EOFException(java.io.EOFException) CertificateException(java.security.cert.CertificateException) HttpDataSource(com.google.android.exoplayer2.upstream.HttpDataSource) IOException(java.io.IOException)

Example 23 with Player

use of com.google.android.exoplayer2.Player in project android by owncloud.

the class PreviewVideoFragment method preparePlayer.

// Video player internal methods
private void preparePlayer() {
    AdaptiveTrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory();
    trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
    // Video streaming is only supported at Jelly Bean or higher Android versions (>= API 16)
    // Create the player
    player = new SimpleExoPlayer.Builder(requireContext()).setTrackSelector(trackSelector).setLoadControl(new DefaultLoadControl()).build();
    player.addListener(this);
    // Bind the player to the view.
    exoPlayerView.setPlayer(player);
    // Prepare video player asynchronously
    new PrepareVideoPlayerAsyncTask(getActivity(), this, getFile(), mAccount).execute();
}
Also used : AdaptiveTrackSelection(com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection) DefaultTrackSelector(com.google.android.exoplayer2.trackselection.DefaultTrackSelector) DefaultLoadControl(com.google.android.exoplayer2.DefaultLoadControl)

Example 24 with Player

use of com.google.android.exoplayer2.Player in project Douya by DreaminginCodeZH.

the class MediaQueueNavigator method onSkipToQueueItem.

@Override
public void onSkipToQueueItem(Player player, long id) {
    Timeline timeline = player.getCurrentTimeline();
    if (timeline.isEmpty()) {
        return;
    }
    int windowIndex = (int) id;
    if (windowIndex < 0 || windowIndex >= timeline.getWindowCount()) {
        return;
    }
    player.seekToDefaultPosition(windowIndex);
    player.setPlayWhenReady(true);
}
Also used : Timeline(com.google.android.exoplayer2.Timeline)

Example 25 with Player

use of com.google.android.exoplayer2.Player in project ExoPlayer by google.

the class SessionPlayerConnectorTest method seekTo_skipsUnnecessarySeek.

@Test
@LargeTest
public void seekTo_skipsUnnecessarySeek() throws Exception {
    CountDownLatch readAllowedLatch = new CountDownLatch(1);
    playerTestRule.setDataSourceInstrumentation(dataSpec -> {
        try {
            assertThat(readAllowedLatch.await(PLAYBACK_COMPLETED_WAIT_TIME_MS, MILLISECONDS)).isTrue();
        } catch (Exception e) {
            assertWithMessage("Unexpected exception %s", e).fail();
        }
    });
    sessionPlayerConnector.setMediaItem(TestUtils.createMediaItem(R.raw.video_big_buck_bunny));
    // prepare() will be pending until readAllowed is countDowned.
    sessionPlayerConnector.prepare();
    CopyOnWriteArrayList<Long> positionChanges = new CopyOnWriteArrayList<>();
    long testIntermediateSeekToPosition1 = 3000;
    long testIntermediateSeekToPosition2 = 2000;
    long testFinalSeekToPosition = 1000;
    CountDownLatch onSeekCompletedLatch = new CountDownLatch(1);
    sessionPlayerConnector.registerPlayerCallback(executor, new SessionPlayer.PlayerCallback() {

        @Override
        public void onSeekCompleted(SessionPlayer player, long position) {
            // Do not assert here, because onSeekCompleted() can be called after the player is
            // closed.
            positionChanges.add(position);
            if (position == testFinalSeekToPosition) {
                onSeekCompletedLatch.countDown();
            }
        }
    });
    ListenableFuture<PlayerResult> seekFuture1 = sessionPlayerConnector.seekTo(testIntermediateSeekToPosition1);
    ListenableFuture<PlayerResult> seekFuture2 = sessionPlayerConnector.seekTo(testIntermediateSeekToPosition2);
    ListenableFuture<PlayerResult> seekFuture3 = sessionPlayerConnector.seekTo(testFinalSeekToPosition);
    readAllowedLatch.countDown();
    assertThat(seekFuture1.get().getResultCode()).isEqualTo(RESULT_INFO_SKIPPED);
    assertThat(seekFuture2.get().getResultCode()).isEqualTo(RESULT_INFO_SKIPPED);
    assertThat(seekFuture3.get().getResultCode()).isEqualTo(RESULT_SUCCESS);
    assertThat(onSeekCompletedLatch.await(PLAYBACK_COMPLETED_WAIT_TIME_MS, MILLISECONDS)).isTrue();
    assertThat(positionChanges).containsNoneOf(testIntermediateSeekToPosition1, testIntermediateSeekToPosition2);
    assertThat(positionChanges).contains(testFinalSeekToPosition);
}
Also used : TestUtils.assertPlayerResult(com.google.android.exoplayer2.ext.media2.TestUtils.assertPlayerResult) PlayerResult(androidx.media2.common.SessionPlayer.PlayerResult) SessionPlayer(androidx.media2.common.SessionPlayer) AtomicLong(java.util.concurrent.atomic.AtomicLong) CountDownLatch(java.util.concurrent.CountDownLatch) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) MediumTest(androidx.test.filters.MediumTest) LargeTest(androidx.test.filters.LargeTest) SmallTest(androidx.test.filters.SmallTest) Test(org.junit.Test) LargeTest(androidx.test.filters.LargeTest)

Aggregations

Test (org.junit.Test)250 FakeMediaSource (com.google.android.exoplayer2.testutil.FakeMediaSource)185 TestExoPlayerBuilder (com.google.android.exoplayer2.testutil.TestExoPlayerBuilder)174 FakeTimeline (com.google.android.exoplayer2.testutil.FakeTimeline)127 PlayerRunnable (com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable)107 ActionSchedule (com.google.android.exoplayer2.testutil.ActionSchedule)91 SinglePeriodTimeline (com.google.android.exoplayer2.source.SinglePeriodTimeline)89 NoUidTimeline (com.google.android.exoplayer2.testutil.NoUidTimeline)89 Listener (com.google.android.exoplayer2.Player.Listener)85 TimelineWindowDefinition (com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition)72 MediaSource (com.google.android.exoplayer2.source.MediaSource)68 ExoPlayerTestRunner (com.google.android.exoplayer2.testutil.ExoPlayerTestRunner)67 ConcatenatingMediaSource (com.google.android.exoplayer2.source.ConcatenatingMediaSource)66 Nullable (androidx.annotation.Nullable)56 ClippingMediaSource (com.google.android.exoplayer2.source.ClippingMediaSource)49 CompositeMediaSource (com.google.android.exoplayer2.source.CompositeMediaSource)49 MaskingMediaSource (com.google.android.exoplayer2.source.MaskingMediaSource)49 ServerSideAdInsertionMediaSource (com.google.android.exoplayer2.source.ads.ServerSideAdInsertionMediaSource)49 FakeAdaptiveMediaSource (com.google.android.exoplayer2.testutil.FakeAdaptiveMediaSource)49 ExoPlayer (com.google.android.exoplayer2.ExoPlayer)47