Search in sources :

Example 1 with ExoPlayer

use of androidx.media3.exoplayer.ExoPlayer in project media by androidx.

the class MainActivity method initializePlayer.

private void initializePlayer() {
    Intent intent = getIntent();
    String action = intent.getAction();
    Uri uri = ACTION_VIEW.equals(action) ? Assertions.checkNotNull(intent.getData()) : Uri.parse(DEFAULT_MEDIA_URI);
    DrmSessionManager drmSessionManager;
    if (intent.hasExtra(DRM_SCHEME_EXTRA)) {
        String drmScheme = Assertions.checkNotNull(intent.getStringExtra(DRM_SCHEME_EXTRA));
        String drmLicenseUrl = Assertions.checkNotNull(intent.getStringExtra(DRM_LICENSE_URL_EXTRA));
        UUID drmSchemeUuid = Assertions.checkNotNull(Util.getDrmUuid(drmScheme));
        HttpDataSource.Factory licenseDataSourceFactory = new DefaultHttpDataSource.Factory();
        HttpMediaDrmCallback drmCallback = new HttpMediaDrmCallback(drmLicenseUrl, licenseDataSourceFactory);
        drmSessionManager = new DefaultDrmSessionManager.Builder().setUuidAndExoMediaDrmProvider(drmSchemeUuid, FrameworkMediaDrm.DEFAULT_PROVIDER).build(drmCallback);
    } else {
        drmSessionManager = DrmSessionManager.DRM_UNSUPPORTED;
    }
    DataSource.Factory dataSourceFactory = new DefaultDataSource.Factory(this);
    MediaSource mediaSource;
    @C.ContentType int type = Util.inferContentType(uri, intent.getStringExtra(EXTENSION_EXTRA));
    if (type == C.TYPE_DASH) {
        mediaSource = new DashMediaSource.Factory(dataSourceFactory).setDrmSessionManagerProvider(unusedMediaItem -> drmSessionManager).createMediaSource(MediaItem.fromUri(uri));
    } else if (type == C.TYPE_OTHER) {
        mediaSource = new ProgressiveMediaSource.Factory(dataSourceFactory).setDrmSessionManagerProvider(unusedMediaItem -> drmSessionManager).createMediaSource(MediaItem.fromUri(uri));
    } else {
        throw new IllegalStateException();
    }
    ExoPlayer player = new ExoPlayer.Builder(getApplicationContext()).build();
    player.setMediaSource(mediaSource);
    player.prepare();
    player.play();
    player.setRepeatMode(Player.REPEAT_MODE_ALL);
    surfaceControl = new SurfaceControl.Builder().setName(SURFACE_CONTROL_NAME).setBufferSize(/* width= */
    0, /* height= */
    0).build();
    videoSurface = new Surface(surfaceControl);
    player.setVideoSurface(videoSurface);
    MainActivity.player = player;
}
Also used : Bundle(android.os.Bundle) SurfaceView(android.view.SurfaceView) Uri(android.net.Uri) Intent(android.content.Intent) HttpDataSource(androidx.media3.datasource.HttpDataSource) DrmSessionManager(androidx.media3.exoplayer.drm.DrmSessionManager) ExoPlayer(androidx.media3.exoplayer.ExoPlayer) View(android.view.View) Button(android.widget.Button) Assertions(androidx.media3.common.util.Assertions) DefaultDataSource(androidx.media3.datasource.DefaultDataSource) MediaItem(androidx.media3.common.MediaItem) SurfaceHolder(android.view.SurfaceHolder) DashMediaSource(androidx.media3.exoplayer.dash.DashMediaSource) DefaultDrmSessionManager(androidx.media3.exoplayer.drm.DefaultDrmSessionManager) GridLayout(android.widget.GridLayout) MediaSource(androidx.media3.exoplayer.source.MediaSource) Player(androidx.media3.common.Player) FrameworkMediaDrm(androidx.media3.exoplayer.drm.FrameworkMediaDrm) Surface(android.view.Surface) ProgressiveMediaSource(androidx.media3.exoplayer.source.ProgressiveMediaSource) UUID(java.util.UUID) Util(androidx.media3.common.util.Util) DefaultHttpDataSource(androidx.media3.datasource.DefaultHttpDataSource) HttpMediaDrmCallback(androidx.media3.exoplayer.drm.HttpMediaDrmCallback) DataSource(androidx.media3.datasource.DataSource) C(androidx.media3.common.C) Nullable(androidx.annotation.Nullable) SurfaceControl(android.view.SurfaceControl) LegacyPlayerControlView(androidx.media3.ui.LegacyPlayerControlView) Activity(android.app.Activity) DrmSessionManager(androidx.media3.exoplayer.drm.DrmSessionManager) DefaultDrmSessionManager(androidx.media3.exoplayer.drm.DefaultDrmSessionManager) DefaultDrmSessionManager(androidx.media3.exoplayer.drm.DefaultDrmSessionManager) DashMediaSource(androidx.media3.exoplayer.dash.DashMediaSource) Intent(android.content.Intent) ExoPlayer(androidx.media3.exoplayer.ExoPlayer) Uri(android.net.Uri) HttpDataSource(androidx.media3.datasource.HttpDataSource) DefaultDataSource(androidx.media3.datasource.DefaultDataSource) DefaultHttpDataSource(androidx.media3.datasource.DefaultHttpDataSource) DataSource(androidx.media3.datasource.DataSource) Surface(android.view.Surface) DashMediaSource(androidx.media3.exoplayer.dash.DashMediaSource) MediaSource(androidx.media3.exoplayer.source.MediaSource) ProgressiveMediaSource(androidx.media3.exoplayer.source.ProgressiveMediaSource) HttpDataSource(androidx.media3.datasource.HttpDataSource) DefaultHttpDataSource(androidx.media3.datasource.DefaultHttpDataSource) HttpMediaDrmCallback(androidx.media3.exoplayer.drm.HttpMediaDrmCallback) UUID(java.util.UUID)

Example 2 with ExoPlayer

use of androidx.media3.exoplayer.ExoPlayer in project media by androidx.

the class TransformerActivity method playMediaItem.

@RequiresNonNull({ "playerView", "debugTextView" })
private void playMediaItem(MediaItem mediaItem) {
    playerView.setPlayer(null);
    releasePlayer();
    ExoPlayer player = new ExoPlayer.Builder(/* context= */
    this).build();
    playerView.setPlayer(player);
    player.setMediaItem(mediaItem);
    player.play();
    player.prepare();
    this.player = player;
    debugTextViewHelper = new DebugTextViewHelper(player, debugTextView);
    debugTextViewHelper.start();
}
Also used : DebugTextViewHelper(androidx.media3.exoplayer.util.DebugTextViewHelper) ExoPlayer(androidx.media3.exoplayer.ExoPlayer) RequiresNonNull(org.checkerframework.checker.nullness.qual.RequiresNonNull)

Example 3 with ExoPlayer

use of androidx.media3.exoplayer.ExoPlayer in project media by androidx.

the class MainActivity method initializePlayer.

private void initializePlayer() {
    Intent intent = getIntent();
    String action = intent.getAction();
    Uri uri = ACTION_VIEW.equals(action) ? Assertions.checkNotNull(intent.getData()) : Uri.parse(DEFAULT_MEDIA_URI);
    DrmSessionManager drmSessionManager;
    if (Util.SDK_INT >= 18 && intent.hasExtra(DRM_SCHEME_EXTRA)) {
        String drmScheme = Assertions.checkNotNull(intent.getStringExtra(DRM_SCHEME_EXTRA));
        String drmLicenseUrl = Assertions.checkNotNull(intent.getStringExtra(DRM_LICENSE_URL_EXTRA));
        UUID drmSchemeUuid = Assertions.checkNotNull(Util.getDrmUuid(drmScheme));
        HttpDataSource.Factory licenseDataSourceFactory = new DefaultHttpDataSource.Factory();
        HttpMediaDrmCallback drmCallback = new HttpMediaDrmCallback(drmLicenseUrl, licenseDataSourceFactory);
        drmSessionManager = new DefaultDrmSessionManager.Builder().setUuidAndExoMediaDrmProvider(drmSchemeUuid, FrameworkMediaDrm.DEFAULT_PROVIDER).build(drmCallback);
    } else {
        drmSessionManager = DrmSessionManager.DRM_UNSUPPORTED;
    }
    DataSource.Factory dataSourceFactory = new DefaultDataSource.Factory(this);
    MediaSource mediaSource;
    @C.ContentType int type = Util.inferContentType(uri, intent.getStringExtra(EXTENSION_EXTRA));
    if (type == C.TYPE_DASH) {
        mediaSource = new DashMediaSource.Factory(dataSourceFactory).setDrmSessionManagerProvider(unusedMediaItem -> drmSessionManager).createMediaSource(MediaItem.fromUri(uri));
    } else if (type == C.TYPE_OTHER) {
        mediaSource = new ProgressiveMediaSource.Factory(dataSourceFactory).setDrmSessionManagerProvider(unusedMediaItem -> drmSessionManager).createMediaSource(MediaItem.fromUri(uri));
    } else {
        throw new IllegalStateException();
    }
    ExoPlayer player = new ExoPlayer.Builder(getApplicationContext()).build();
    player.setRepeatMode(Player.REPEAT_MODE_ALL);
    player.setMediaSource(mediaSource);
    player.prepare();
    player.play();
    VideoProcessingGLSurfaceView videoProcessingGLSurfaceView = Assertions.checkNotNull(this.videoProcessingGLSurfaceView);
    videoProcessingGLSurfaceView.setPlayer(player);
    Assertions.checkNotNull(playerView).setPlayer(player);
    player.addAnalyticsListener(new EventLogger(/* trackSelector= */
    null));
    this.player = player;
}
Also used : Context(android.content.Context) Bundle(android.os.Bundle) Uri(android.net.Uri) FrameLayout(android.widget.FrameLayout) Intent(android.content.Intent) GlUtil(androidx.media3.common.util.GlUtil) HttpDataSource(androidx.media3.datasource.HttpDataSource) DrmSessionManager(androidx.media3.exoplayer.drm.DrmSessionManager) ExoPlayer(androidx.media3.exoplayer.ExoPlayer) Toast(android.widget.Toast) Assertions(androidx.media3.common.util.Assertions) DefaultDataSource(androidx.media3.datasource.DefaultDataSource) MediaItem(androidx.media3.common.MediaItem) DashMediaSource(androidx.media3.exoplayer.dash.DashMediaSource) DefaultDrmSessionManager(androidx.media3.exoplayer.drm.DefaultDrmSessionManager) MediaSource(androidx.media3.exoplayer.source.MediaSource) Player(androidx.media3.common.Player) FrameworkMediaDrm(androidx.media3.exoplayer.drm.FrameworkMediaDrm) ProgressiveMediaSource(androidx.media3.exoplayer.source.ProgressiveMediaSource) UUID(java.util.UUID) Util(androidx.media3.common.util.Util) DefaultHttpDataSource(androidx.media3.datasource.DefaultHttpDataSource) HttpMediaDrmCallback(androidx.media3.exoplayer.drm.HttpMediaDrmCallback) DataSource(androidx.media3.datasource.DataSource) EventLogger(androidx.media3.exoplayer.util.EventLogger) C(androidx.media3.common.C) Nullable(androidx.annotation.Nullable) PlayerView(androidx.media3.ui.PlayerView) Activity(android.app.Activity) EventLogger(androidx.media3.exoplayer.util.EventLogger) DrmSessionManager(androidx.media3.exoplayer.drm.DrmSessionManager) DefaultDrmSessionManager(androidx.media3.exoplayer.drm.DefaultDrmSessionManager) DefaultDrmSessionManager(androidx.media3.exoplayer.drm.DefaultDrmSessionManager) DashMediaSource(androidx.media3.exoplayer.dash.DashMediaSource) Intent(android.content.Intent) ExoPlayer(androidx.media3.exoplayer.ExoPlayer) Uri(android.net.Uri) HttpDataSource(androidx.media3.datasource.HttpDataSource) DefaultDataSource(androidx.media3.datasource.DefaultDataSource) DefaultHttpDataSource(androidx.media3.datasource.DefaultHttpDataSource) DataSource(androidx.media3.datasource.DataSource) DashMediaSource(androidx.media3.exoplayer.dash.DashMediaSource) MediaSource(androidx.media3.exoplayer.source.MediaSource) ProgressiveMediaSource(androidx.media3.exoplayer.source.ProgressiveMediaSource) HttpDataSource(androidx.media3.datasource.HttpDataSource) DefaultHttpDataSource(androidx.media3.datasource.DefaultHttpDataSource) HttpMediaDrmCallback(androidx.media3.exoplayer.drm.HttpMediaDrmCallback) UUID(java.util.UUID)

Example 4 with ExoPlayer

use of androidx.media3.exoplayer.ExoPlayer in project media by androidx.

the class DrmPlaybackTest method clearkeyPlayback.

@Test
public void clearkeyPlayback() throws Exception {
    MockWebServer mockWebServer = new MockWebServer();
    mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(CLEARKEY_RESPONSE));
    mockWebServer.start();
    MediaItem mediaItem = new MediaItem.Builder().setUri("asset:///media/drm/sample_fragmented_clearkey.mp4").setDrmConfiguration(new MediaItem.DrmConfiguration.Builder(C.CLEARKEY_UUID).setLicenseUri(mockWebServer.url("license").toString()).build()).build();
    AtomicReference<ExoPlayer> player = new AtomicReference<>();
    ConditionVariable playbackComplete = new ConditionVariable();
    AtomicReference<PlaybackException> playbackException = new AtomicReference<>();
    getInstrumentation().runOnMainSync(() -> {
        player.set(new ExoPlayer.Builder(getInstrumentation().getContext()).build());
        player.get().addListener(new Player.Listener() {

            @Override
            public void onPlaybackStateChanged(@Player.State int playbackState) {
                if (playbackState == Player.STATE_ENDED) {
                    playbackComplete.open();
                }
            }

            @Override
            public void onPlayerError(PlaybackException error) {
                playbackException.set(error);
                playbackComplete.open();
            }
        });
        player.get().setMediaItem(mediaItem);
        player.get().prepare();
        player.get().play();
    });
    playbackComplete.block();
    getInstrumentation().runOnMainSync(() -> player.get().release());
    getInstrumentation().waitForIdleSync();
    assertThat(playbackException.get()).isNull();
}
Also used : PlaybackException(androidx.media3.common.PlaybackException) MockResponse(okhttp3.mockwebserver.MockResponse) Player(androidx.media3.common.Player) ExoPlayer(androidx.media3.exoplayer.ExoPlayer) AtomicReference(java.util.concurrent.atomic.AtomicReference) ExoPlayer(androidx.media3.exoplayer.ExoPlayer) ConditionVariable(androidx.media3.common.util.ConditionVariable) MediaItem(androidx.media3.common.MediaItem) MockWebServer(okhttp3.mockwebserver.MockWebServer) Test(org.junit.Test)

Example 5 with ExoPlayer

use of androidx.media3.exoplayer.ExoPlayer in project media by androidx.

the class ProgressiveMediaSource method notifySourceInfoRefreshed.

// Internal methods.
private void notifySourceInfoRefreshed() {
    // TODO: Split up isDynamic into multiple fields to indicate which values may change. Then
    // indicate that the duration may change until it's known. See [internal: b/69703223].
    Timeline timeline = new SinglePeriodTimeline(timelineDurationUs, timelineIsSeekable, /* isDynamic= */
    false, /* useLiveConfiguration= */
    timelineIsLive, /* manifest= */
    null, mediaItem);
    if (timelineIsPlaceholder) {
        // TODO: Actually prepare the extractors during preparation so that we don't need a
        // placeholder. See https://github.com/google/ExoPlayer/issues/4727.
        timeline = new ForwardingTimeline(timeline) {

            @Override
            public Window getWindow(int windowIndex, Window window, long defaultPositionProjectionUs) {
                super.getWindow(windowIndex, window, defaultPositionProjectionUs);
                window.isPlaceholder = true;
                return window;
            }

            @Override
            public Period getPeriod(int periodIndex, Period period, boolean setIds) {
                super.getPeriod(periodIndex, period, setIds);
                period.isPlaceholder = true;
                return period;
            }
        };
    }
    refreshSourceInfo(timeline);
}
Also used : Timeline(androidx.media3.common.Timeline)

Aggregations

Test (org.junit.Test)242 FakeMediaSource (androidx.media3.test.utils.FakeMediaSource)179 TestExoPlayerBuilder (androidx.media3.test.utils.TestExoPlayerBuilder)172 FakeTimeline (androidx.media3.test.utils.FakeTimeline)121 PlayerRunnable (androidx.media3.test.utils.ActionSchedule.PlayerRunnable)108 Timeline (androidx.media3.common.Timeline)96 ActionSchedule (androidx.media3.test.utils.ActionSchedule)92 SinglePeriodTimeline (androidx.media3.exoplayer.source.SinglePeriodTimeline)89 NoUidTimeline (androidx.media3.test.utils.NoUidTimeline)89 Listener (androidx.media3.common.Player.Listener)85 Player (androidx.media3.common.Player)83 TimelineWindowDefinition (androidx.media3.test.utils.FakeTimeline.TimelineWindowDefinition)73 ExoPlayerTestRunner (androidx.media3.test.utils.ExoPlayerTestRunner)67 ConcatenatingMediaSource (androidx.media3.exoplayer.source.ConcatenatingMediaSource)64 MediaSource (androidx.media3.exoplayer.source.MediaSource)52 MediaItem (androidx.media3.common.MediaItem)48 FakeClock (androidx.media3.test.utils.FakeClock)48 ClippingMediaSource (androidx.media3.exoplayer.source.ClippingMediaSource)47 CompositeMediaSource (androidx.media3.exoplayer.source.CompositeMediaSource)47 MaskingMediaSource (androidx.media3.exoplayer.source.MaskingMediaSource)47