Search in sources :

Example 6 with Player

use of androidx.media3.common.Player in project media by androidx.

the class CastPlayerTest method updateTimeLine.

private void updateTimeLine(List<MediaItem> mediaItems, int[] mediaQueueItemIds, int currentItemId, int[] streamTypes, long[] durationsMs, long positionMs) {
    // Set up mocks to allow the player to update the timeline.
    List<MediaQueueItem> queueItems = new ArrayList<>();
    for (int i = 0; i < mediaQueueItemIds.length; i++) {
        MediaItem mediaItem = mediaItems.get(i);
        int mediaQueueItemId = mediaQueueItemIds[i];
        int streamType = streamTypes[i];
        long durationMs = durationsMs[i];
        MediaInfo.Builder mediaInfoBuilder = new MediaInfo.Builder(mediaItem.localConfiguration.uri.toString()).setStreamType(streamType).setContentType(mediaItem.localConfiguration.mimeType);
        if (durationMs != C.TIME_UNSET) {
            mediaInfoBuilder.setStreamDuration(durationMs);
        }
        MediaInfo mediaInfo = mediaInfoBuilder.build();
        MediaQueueItem mediaQueueItem = mock(MediaQueueItem.class);
        when(mediaQueueItem.getItemId()).thenReturn(mediaQueueItemId);
        when(mediaQueueItem.getMedia()).thenReturn(mediaInfo);
        queueItems.add(mediaQueueItem);
        if (mediaQueueItemId == currentItemId) {
            when(mockRemoteMediaClient.getCurrentItem()).thenReturn(mediaQueueItem);
            when(mockMediaStatus.getMediaInfo()).thenReturn(mediaInfo);
        }
    }
    if (positionMs != C.TIME_UNSET) {
        when(mockRemoteMediaClient.getApproximateStreamPosition()).thenReturn(positionMs);
    }
    when(mockMediaQueue.getItemIds()).thenReturn(mediaQueueItemIds);
    when(mockMediaStatus.getQueueItems()).thenReturn(queueItems);
    when(mockMediaStatus.getCurrentItemId()).thenReturn(currentItemId == C.INDEX_UNSET ? 0 : currentItemId);
    // Call listener to update the timeline of the player.
    remoteMediaClientCallback.onStatusUpdated();
}
Also used : MediaInfo(com.google.android.gms.cast.MediaInfo) MediaItem(androidx.media3.common.MediaItem) ArrayList(java.util.ArrayList) MediaQueueItem(com.google.android.gms.cast.MediaQueueItem)

Example 7 with Player

use of androidx.media3.common.Player in project media by androidx.

the class CastPlayerTest method autoTransition_notifiesPositionDiscontinuity.

@Test
// Mocks deprecated method used by the CastPlayer.
@SuppressWarnings("deprecation")
public void autoTransition_notifiesPositionDiscontinuity() {
    int[] mediaQueueItemIds = new int[] { 1, 2 };
    int[] streamTypes = { MediaInfo.STREAM_TYPE_BUFFERED, MediaInfo.STREAM_TYPE_BUFFERED };
    long[] durationsFirstMs = { 12500, C.TIME_UNSET };
    // When the remote Cast player transitions to an item that wasn't played before, the media state
    // delivers the duration for that media item which updates the timeline accordingly.
    long[] durationsSecondMs = { 12500, 22000 };
    List<MediaItem> mediaItems = createMediaItems(mediaQueueItemIds);
    castPlayer.addMediaItems(mediaItems);
    updateTimeLine(mediaItems, mediaQueueItemIds, /* currentItemId= */
    1, /* streamTypes= */
    streamTypes, /* durationsMs= */
    durationsFirstMs, /* positionMs= */
    C.TIME_UNSET);
    updateTimeLine(mediaItems, mediaQueueItemIds, /* currentItemId= */
    2, /* streamTypes= */
    streamTypes, /* durationsMs= */
    durationsSecondMs, /* positionMs= */
    C.TIME_UNSET);
    Player.PositionInfo oldPosition = new Player.PositionInfo(/* windowUid= */
    1, /* windowIndex= */
    0, new MediaItem.Builder().setUri(Uri.EMPTY).setTag(1).build(), /* periodUid= */
    1, /* periodIndex= */
    0, /* positionMs= */
    12500, /* contentPositionMs= */
    12500, /* adGroupIndex= */
    C.INDEX_UNSET, /* adIndexInAdGroup= */
    C.INDEX_UNSET);
    Player.PositionInfo newPosition = new Player.PositionInfo(/* windowUid= */
    2, /* windowIndex= */
    1, new MediaItem.Builder().setUri(Uri.EMPTY).setTag(2).build(), /* periodUid= */
    2, /* periodIndex= */
    1, /* positionMs= */
    0, /* contentPositionMs= */
    0, /* adGroupIndex= */
    C.INDEX_UNSET, /* adIndexInAdGroup= */
    C.INDEX_UNSET);
    InOrder inOrder = Mockito.inOrder(mockListener);
    inOrder.verify(mockListener).onPositionDiscontinuity(eq(Player.DISCONTINUITY_REASON_AUTO_TRANSITION));
    inOrder.verify(mockListener).onPositionDiscontinuity(eq(oldPosition), eq(newPosition), eq(Player.DISCONTINUITY_REASON_AUTO_TRANSITION));
    inOrder.verify(mockListener, never()).onPositionDiscontinuity(anyInt());
    inOrder.verify(mockListener, never()).onPositionDiscontinuity(any(), any(), anyInt());
}
Also used : Player(androidx.media3.common.Player) InOrder(org.mockito.InOrder) MediaItem(androidx.media3.common.MediaItem) Test(org.junit.Test)

Example 8 with Player

use of androidx.media3.common.Player in project media by androidx.

the class PlayerManager method setCurrentPlayer.

private void setCurrentPlayer(Player currentPlayer) {
    if (this.currentPlayer == currentPlayer) {
        return;
    }
    playerView.setPlayer(currentPlayer);
    playerView.setControllerHideOnTouch(currentPlayer == localPlayer);
    if (currentPlayer == castPlayer) {
        playerView.setControllerShowTimeoutMs(0);
        playerView.showController();
        playerView.setDefaultArtwork(ResourcesCompat.getDrawable(context.getResources(), R.drawable.ic_baseline_cast_connected_400, /* theme= */
        null));
    } else {
        // currentPlayer == localPlayer
        playerView.setControllerShowTimeoutMs(PlayerControlView.DEFAULT_SHOW_TIMEOUT_MS);
        playerView.setDefaultArtwork(null);
    }
    // Player state management.
    long playbackPositionMs = C.TIME_UNSET;
    int currentItemIndex = C.INDEX_UNSET;
    boolean playWhenReady = false;
    Player previousPlayer = this.currentPlayer;
    if (previousPlayer != null) {
        // Save state from the previous player.
        int playbackState = previousPlayer.getPlaybackState();
        if (playbackState != Player.STATE_ENDED) {
            playbackPositionMs = previousPlayer.getCurrentPosition();
            playWhenReady = previousPlayer.getPlayWhenReady();
            currentItemIndex = previousPlayer.getCurrentMediaItemIndex();
            if (currentItemIndex != this.currentItemIndex) {
                playbackPositionMs = C.TIME_UNSET;
                currentItemIndex = this.currentItemIndex;
            }
        }
        previousPlayer.stop();
        previousPlayer.clearMediaItems();
    }
    this.currentPlayer = currentPlayer;
    // Media queue management.
    currentPlayer.setMediaItems(mediaQueue, currentItemIndex, playbackPositionMs);
    currentPlayer.setPlayWhenReady(playWhenReady);
    currentPlayer.prepare();
}
Also used : Player(androidx.media3.common.Player) ExoPlayer(androidx.media3.exoplayer.ExoPlayer) CastPlayer(androidx.media3.cast.CastPlayer)

Example 9 with Player

use of androidx.media3.common.Player 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 10 with Player

use of androidx.media3.common.Player in project media by androidx.

the class DefaultRenderersFactory method buildAudioRenderers.

/**
 * Builds audio renderers for use by the player.
 *
 * @param context The {@link Context} associated with the player.
 * @param extensionRendererMode The extension renderer mode.
 * @param mediaCodecSelector A decoder selector.
 * @param enableDecoderFallback Whether to enable fallback to lower-priority decoders if decoder
 *     initialization fails. This may result in using a decoder that is slower/less efficient than
 *     the primary decoder.
 * @param audioSink A sink to which the renderers will output.
 * @param eventHandler A handler to use when invoking event listeners and outputs.
 * @param eventListener An event listener.
 * @param out An array to which the built renderers should be appended.
 */
protected void buildAudioRenderers(Context context, @ExtensionRendererMode int extensionRendererMode, MediaCodecSelector mediaCodecSelector, boolean enableDecoderFallback, AudioSink audioSink, Handler eventHandler, AudioRendererEventListener eventListener, ArrayList<Renderer> out) {
    MediaCodecAudioRenderer audioRenderer = new MediaCodecAudioRenderer(context, getCodecAdapterFactory(), mediaCodecSelector, enableDecoderFallback, eventHandler, eventListener, audioSink);
    out.add(audioRenderer);
    if (extensionRendererMode == EXTENSION_RENDERER_MODE_OFF) {
        return;
    }
    int extensionRendererIndex = out.size();
    if (extensionRendererMode == EXTENSION_RENDERER_MODE_PREFER) {
        extensionRendererIndex--;
    }
    try {
        // Full class names used for constructor args so the LINT rule triggers if any of them move.
        Class<?> clazz = Class.forName("androidx.media3.decoder.opus.LibopusAudioRenderer");
        Constructor<?> constructor = clazz.getConstructor(android.os.Handler.class, androidx.media3.exoplayer.audio.AudioRendererEventListener.class, androidx.media3.exoplayer.audio.AudioSink.class);
        Renderer renderer = (Renderer) constructor.newInstance(eventHandler, eventListener, audioSink);
        out.add(extensionRendererIndex++, renderer);
        Log.i(TAG, "Loaded LibopusAudioRenderer.");
    } catch (ClassNotFoundException e) {
    // Expected if the app was built without the extension.
    } catch (Exception e) {
        // The extension is present, but instantiation failed.
        throw new RuntimeException("Error instantiating Opus extension", e);
    }
    try {
        // Full class names used for constructor args so the LINT rule triggers if any of them move.
        Class<?> clazz = Class.forName("androidx.media3.decoder.flac.LibflacAudioRenderer");
        Constructor<?> constructor = clazz.getConstructor(android.os.Handler.class, androidx.media3.exoplayer.audio.AudioRendererEventListener.class, androidx.media3.exoplayer.audio.AudioSink.class);
        Renderer renderer = (Renderer) constructor.newInstance(eventHandler, eventListener, audioSink);
        out.add(extensionRendererIndex++, renderer);
        Log.i(TAG, "Loaded LibflacAudioRenderer.");
    } catch (ClassNotFoundException e) {
    // Expected if the app was built without the extension.
    } catch (Exception e) {
        // The extension is present, but instantiation failed.
        throw new RuntimeException("Error instantiating FLAC extension", e);
    }
    try {
        // Full class names used for constructor args so the LINT rule triggers if any of them move.
        Class<?> clazz = Class.forName("androidx.media3.decoder.ffmpeg.FfmpegAudioRenderer");
        Constructor<?> constructor = clazz.getConstructor(android.os.Handler.class, androidx.media3.exoplayer.audio.AudioRendererEventListener.class, androidx.media3.exoplayer.audio.AudioSink.class);
        Renderer renderer = (Renderer) constructor.newInstance(eventHandler, eventListener, audioSink);
        out.add(extensionRendererIndex++, renderer);
        Log.i(TAG, "Loaded FfmpegAudioRenderer.");
    } catch (ClassNotFoundException e) {
    // Expected if the app was built without the extension.
    } catch (Exception e) {
        // The extension is present, but instantiation failed.
        throw new RuntimeException("Error instantiating FFmpeg extension", e);
    }
}
Also used : MediaCodecAudioRenderer(androidx.media3.exoplayer.audio.MediaCodecAudioRenderer) MediaCodecAudioRenderer(androidx.media3.exoplayer.audio.MediaCodecAudioRenderer) TextRenderer(androidx.media3.exoplayer.text.TextRenderer) MediaCodecVideoRenderer(androidx.media3.exoplayer.video.MediaCodecVideoRenderer) CameraMotionRenderer(androidx.media3.exoplayer.video.spherical.CameraMotionRenderer) MetadataRenderer(androidx.media3.exoplayer.metadata.MetadataRenderer)

Aggregations

Test (org.junit.Test)347 FakeMediaSource (androidx.media3.test.utils.FakeMediaSource)185 Player (androidx.media3.common.Player)183 TestExoPlayerBuilder (androidx.media3.test.utils.TestExoPlayerBuilder)174 Timeline (androidx.media3.common.Timeline)137 FakeTimeline (androidx.media3.test.utils.FakeTimeline)127 CountDownLatch (java.util.concurrent.CountDownLatch)108 PlayerRunnable (androidx.media3.test.utils.ActionSchedule.PlayerRunnable)107 LargeTest (androidx.test.filters.LargeTest)97 AtomicReference (java.util.concurrent.atomic.AtomicReference)95 ActionSchedule (androidx.media3.test.utils.ActionSchedule)91 SinglePeriodTimeline (androidx.media3.exoplayer.source.SinglePeriodTimeline)89 NoUidTimeline (androidx.media3.test.utils.NoUidTimeline)89 Listener (androidx.media3.common.Player.Listener)85 MediaItem (androidx.media3.common.MediaItem)84 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)81 TimelineWindowDefinition (androidx.media3.test.utils.FakeTimeline.TimelineWindowDefinition)72 Nullable (androidx.annotation.Nullable)68 ExoPlayerTestRunner (androidx.media3.test.utils.ExoPlayerTestRunner)67 ConcatenatingMediaSource (androidx.media3.exoplayer.source.ConcatenatingMediaSource)65