Search in sources :

Example 1 with Action

use of androidx.media3.test.utils.Action in project media by androidx.

the class PlayerActivity method createMediaItems.

private List<MediaItem> createMediaItems(Intent intent) {
    String action = intent.getAction();
    boolean actionIsListView = IntentUtil.ACTION_VIEW_LIST.equals(action);
    if (!actionIsListView && !IntentUtil.ACTION_VIEW.equals(action)) {
        showToast(getString(R.string.unexpected_intent_action, action));
        finish();
        return Collections.emptyList();
    }
    List<MediaItem> mediaItems = createMediaItems(intent, DemoUtil.getDownloadTracker(/* context= */
    this));
    for (int i = 0; i < mediaItems.size(); i++) {
        MediaItem mediaItem = mediaItems.get(i);
        if (!Util.checkCleartextTrafficPermitted(mediaItem)) {
            showToast(R.string.error_cleartext_not_permitted);
            finish();
            return Collections.emptyList();
        }
        if (Util.maybeRequestReadExternalStoragePermission(/* activity= */
        this, mediaItem)) {
            // The player will be reinitialized if the permission is granted.
            return Collections.emptyList();
        }
        MediaItem.DrmConfiguration drmConfiguration = mediaItem.localConfiguration.drmConfiguration;
        if (drmConfiguration != null) {
            if (Util.SDK_INT < 18) {
                showToast(R.string.error_drm_unsupported_before_api_18);
                finish();
                return Collections.emptyList();
            } else if (!FrameworkMediaDrm.isCryptoSchemeSupported(drmConfiguration.scheme)) {
                showToast(R.string.error_drm_unsupported_scheme);
                finish();
                return Collections.emptyList();
            }
        }
    }
    return mediaItems;
}
Also used : MediaItem(androidx.media3.common.MediaItem)

Example 2 with Action

use of androidx.media3.test.utils.Action 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 3 with Action

use of androidx.media3.test.utils.Action 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 Action

use of androidx.media3.test.utils.Action in project media by androidx.

the class DownloadService method onStartCommand.

@Override
public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
    lastStartId = startId;
    taskRemoved = false;
    @Nullable String intentAction = null;
    @Nullable String contentId = null;
    if (intent != null) {
        intentAction = intent.getAction();
        contentId = intent.getStringExtra(KEY_CONTENT_ID);
        startedInForeground |= intent.getBooleanExtra(KEY_FOREGROUND, false) || ACTION_RESTART.equals(intentAction);
    }
    // intentAction is null if the service is restarted or no action is specified.
    if (intentAction == null) {
        intentAction = ACTION_INIT;
    }
    DownloadManager downloadManager = Assertions.checkNotNull(downloadManagerHelper).downloadManager;
    switch(intentAction) {
        case ACTION_INIT:
        case ACTION_RESTART:
            // Do nothing.
            break;
        case ACTION_ADD_DOWNLOAD:
            @Nullable DownloadRequest downloadRequest = Assertions.checkNotNull(intent).getParcelableExtra(KEY_DOWNLOAD_REQUEST);
            if (downloadRequest == null) {
                Log.e(TAG, "Ignored ADD_DOWNLOAD: Missing " + KEY_DOWNLOAD_REQUEST + " extra");
            } else {
                int stopReason = intent.getIntExtra(KEY_STOP_REASON, Download.STOP_REASON_NONE);
                downloadManager.addDownload(downloadRequest, stopReason);
            }
            break;
        case ACTION_REMOVE_DOWNLOAD:
            if (contentId == null) {
                Log.e(TAG, "Ignored REMOVE_DOWNLOAD: Missing " + KEY_CONTENT_ID + " extra");
            } else {
                downloadManager.removeDownload(contentId);
            }
            break;
        case ACTION_REMOVE_ALL_DOWNLOADS:
            downloadManager.removeAllDownloads();
            break;
        case ACTION_RESUME_DOWNLOADS:
            downloadManager.resumeDownloads();
            break;
        case ACTION_PAUSE_DOWNLOADS:
            downloadManager.pauseDownloads();
            break;
        case ACTION_SET_STOP_REASON:
            if (!Assertions.checkNotNull(intent).hasExtra(KEY_STOP_REASON)) {
                Log.e(TAG, "Ignored SET_STOP_REASON: Missing " + KEY_STOP_REASON + " extra");
            } else {
                int stopReason = intent.getIntExtra(KEY_STOP_REASON, /* defaultValue= */
                0);
                downloadManager.setStopReason(contentId, stopReason);
            }
            break;
        case ACTION_SET_REQUIREMENTS:
            @Nullable Requirements requirements = Assertions.checkNotNull(intent).getParcelableExtra(KEY_REQUIREMENTS);
            if (requirements == null) {
                Log.e(TAG, "Ignored SET_REQUIREMENTS: Missing " + KEY_REQUIREMENTS + " extra");
            } else {
                downloadManager.setRequirements(requirements);
            }
            break;
        default:
            Log.e(TAG, "Ignored unrecognized action: " + intentAction);
            break;
    }
    if (Util.SDK_INT >= 26 && startedInForeground && foregroundNotificationUpdater != null) {
        // From API level 26, services started in the foreground are required to show a notification.
        foregroundNotificationUpdater.showNotificationIfNotAlready();
    }
    isStopped = false;
    if (downloadManager.isIdle()) {
        onIdle();
    }
    return START_STICKY;
}
Also used : Nullable(androidx.annotation.Nullable) Requirements(androidx.media3.exoplayer.scheduler.Requirements)

Example 5 with Action

use of androidx.media3.test.utils.Action in project media by androidx.

the class SingleSampleMediaPeriod method onLoadError.

@Override
public LoadErrorAction onLoadError(SourceLoadable loadable, long elapsedRealtimeMs, long loadDurationMs, IOException error, int errorCount) {
    StatsDataSource dataSource = loadable.dataSource;
    LoadEventInfo loadEventInfo = new LoadEventInfo(loadable.loadTaskId, loadable.dataSpec, dataSource.getLastOpenedUri(), dataSource.getLastResponseHeaders(), elapsedRealtimeMs, loadDurationMs, dataSource.getBytesRead());
    MediaLoadData mediaLoadData = new MediaLoadData(C.DATA_TYPE_MEDIA, C.TRACK_TYPE_UNKNOWN, format, C.SELECTION_REASON_UNKNOWN, /* trackSelectionData= */
    null, /* mediaStartTimeMs= */
    0, Util.usToMs(durationUs));
    long retryDelay = loadErrorHandlingPolicy.getRetryDelayMsFor(new LoadErrorInfo(loadEventInfo, mediaLoadData, error, errorCount));
    boolean errorCanBePropagated = retryDelay == C.TIME_UNSET || errorCount >= loadErrorHandlingPolicy.getMinimumLoadableRetryCount(C.DATA_TYPE_MEDIA);
    LoadErrorAction action;
    if (treatLoadErrorsAsEndOfStream && errorCanBePropagated) {
        Log.w(TAG, "Loading failed, treating as end-of-stream.", error);
        loadingFinished = true;
        action = Loader.DONT_RETRY;
    } else {
        action = retryDelay != C.TIME_UNSET ? Loader.createRetryAction(/* resetErrorCount= */
        false, retryDelay) : Loader.DONT_RETRY_FATAL;
    }
    boolean wasCanceled = !action.isRetry();
    eventDispatcher.loadError(loadEventInfo, C.DATA_TYPE_MEDIA, C.TRACK_TYPE_UNKNOWN, format, C.SELECTION_REASON_UNKNOWN, /* trackSelectionData= */
    null, /* mediaStartTimeUs= */
    0, durationUs, error, wasCanceled);
    if (wasCanceled) {
        loadErrorHandlingPolicy.onLoadTaskConcluded(loadable.loadTaskId);
    }
    return action;
}
Also used : LoadErrorInfo(androidx.media3.exoplayer.upstream.LoadErrorHandlingPolicy.LoadErrorInfo) StatsDataSource(androidx.media3.datasource.StatsDataSource) LoadErrorAction(androidx.media3.exoplayer.upstream.Loader.LoadErrorAction)

Aggregations

Nullable (androidx.annotation.Nullable)7 Test (org.junit.Test)6 Bundle (android.os.Bundle)4 Player (androidx.media3.common.Player)4 Intent (android.content.Intent)3 Uri (android.net.Uri)3 MediaItem (androidx.media3.common.MediaItem)3 Activity (android.app.Activity)2 Surface (android.view.Surface)2 NotificationCompat (androidx.core.app.NotificationCompat)2 C (androidx.media3.common.C)2 Assertions (androidx.media3.common.util.Assertions)2 Util (androidx.media3.common.util.Util)2 DataSource (androidx.media3.datasource.DataSource)2 DefaultDataSource (androidx.media3.datasource.DefaultDataSource)2 DefaultHttpDataSource (androidx.media3.datasource.DefaultHttpDataSource)2 HttpDataSource (androidx.media3.datasource.HttpDataSource)2 ExoPlayer (androidx.media3.exoplayer.ExoPlayer)2 DashMediaSource (androidx.media3.exoplayer.dash.DashMediaSource)2 DefaultDrmSessionManager (androidx.media3.exoplayer.drm.DefaultDrmSessionManager)2