Search in sources :

Example 1 with PlayerView

use of com.google.android.exoplayer2.ui.PlayerView in project ExoPlayer by google.

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) Util(com.google.android.exoplayer2.util.Util) Bundle(android.os.Bundle) FrameworkMediaDrm(com.google.android.exoplayer2.drm.FrameworkMediaDrm) ProgressiveMediaSource(com.google.android.exoplayer2.source.ProgressiveMediaSource) Uri(android.net.Uri) FrameLayout(android.widget.FrameLayout) Intent(android.content.Intent) Player(com.google.android.exoplayer2.Player) DashMediaSource(com.google.android.exoplayer2.source.dash.DashMediaSource) ExoPlayer(com.google.android.exoplayer2.ExoPlayer) DefaultDataSource(com.google.android.exoplayer2.upstream.DefaultDataSource) Toast(android.widget.Toast) EventLogger(com.google.android.exoplayer2.util.EventLogger) MediaSource(com.google.android.exoplayer2.source.MediaSource) C(com.google.android.exoplayer2.C) DefaultDrmSessionManager(com.google.android.exoplayer2.drm.DefaultDrmSessionManager) MediaItem(com.google.android.exoplayer2.MediaItem) HttpMediaDrmCallback(com.google.android.exoplayer2.drm.HttpMediaDrmCallback) StyledPlayerView(com.google.android.exoplayer2.ui.StyledPlayerView) UUID(java.util.UUID) HttpDataSource(com.google.android.exoplayer2.upstream.HttpDataSource) GlUtil(com.google.android.exoplayer2.util.GlUtil) DataSource(com.google.android.exoplayer2.upstream.DataSource) DefaultHttpDataSource(com.google.android.exoplayer2.upstream.DefaultHttpDataSource) Nullable(androidx.annotation.Nullable) DrmSessionManager(com.google.android.exoplayer2.drm.DrmSessionManager) Activity(android.app.Activity) Assertions(com.google.android.exoplayer2.util.Assertions) EventLogger(com.google.android.exoplayer2.util.EventLogger) DefaultDrmSessionManager(com.google.android.exoplayer2.drm.DefaultDrmSessionManager) DrmSessionManager(com.google.android.exoplayer2.drm.DrmSessionManager) DefaultDrmSessionManager(com.google.android.exoplayer2.drm.DefaultDrmSessionManager) DashMediaSource(com.google.android.exoplayer2.source.dash.DashMediaSource) Intent(android.content.Intent) ExoPlayer(com.google.android.exoplayer2.ExoPlayer) Uri(android.net.Uri) DefaultDataSource(com.google.android.exoplayer2.upstream.DefaultDataSource) HttpDataSource(com.google.android.exoplayer2.upstream.HttpDataSource) DataSource(com.google.android.exoplayer2.upstream.DataSource) DefaultHttpDataSource(com.google.android.exoplayer2.upstream.DefaultHttpDataSource) ProgressiveMediaSource(com.google.android.exoplayer2.source.ProgressiveMediaSource) DashMediaSource(com.google.android.exoplayer2.source.dash.DashMediaSource) MediaSource(com.google.android.exoplayer2.source.MediaSource) HttpDataSource(com.google.android.exoplayer2.upstream.HttpDataSource) DefaultHttpDataSource(com.google.android.exoplayer2.upstream.DefaultHttpDataSource) HttpMediaDrmCallback(com.google.android.exoplayer2.drm.HttpMediaDrmCallback) UUID(java.util.UUID)

Example 2 with PlayerView

use of com.google.android.exoplayer2.ui.PlayerView in project ExoPlayer by google.

the class TransformerActivity method startTransformation.

@RequiresNonNull({ "playerView", "debugTextView", "informationTextView", "progressIndicator", "transformationStopwatch", "progressViewGroup", "debugFrame" })
private void startTransformation() {
    requestTransformerPermission();
    Intent intent = getIntent();
    Uri uri = checkNotNull(intent.getData());
    try {
        externalCacheFile = createExternalCacheFile("transformer-output.mp4");
        String filePath = externalCacheFile.getAbsolutePath();
        @Nullable Bundle bundle = intent.getExtras();
        Transformer transformer = createTransformer(bundle, filePath);
        transformationStopwatch.start();
        transformer.startTransformation(MediaItem.fromUri(uri), filePath);
        this.transformer = transformer;
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
    informationTextView.setText(R.string.transformation_started);
    playerView.setVisibility(View.GONE);
    Handler mainHandler = new Handler(getMainLooper());
    ProgressHolder progressHolder = new ProgressHolder();
    mainHandler.post(new Runnable() {

        @Override
        public void run() {
            if (transformer != null && transformer.getProgress(progressHolder) != Transformer.PROGRESS_STATE_NO_TRANSFORMATION) {
                progressIndicator.setProgress(progressHolder.progress);
                informationTextView.setText(getString(R.string.transformation_timer, transformationStopwatch.elapsed(TimeUnit.SECONDS)));
                mainHandler.postDelayed(/* r= */
                this, /* delayMillis= */
                500);
            }
        }
    });
}
Also used : Transformer(com.google.android.exoplayer2.transformer.Transformer) Bundle(android.os.Bundle) Handler(android.os.Handler) Intent(android.content.Intent) IOException(java.io.IOException) Uri(android.net.Uri) Nullable(androidx.annotation.Nullable) ProgressHolder(com.google.android.exoplayer2.transformer.ProgressHolder) RequiresNonNull(org.checkerframework.checker.nullness.qual.RequiresNonNull)

Example 3 with PlayerView

use of com.google.android.exoplayer2.ui.PlayerView in project ExoPlayer by google.

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(com.google.android.exoplayer2.util.DebugTextViewHelper) ExoPlayer(com.google.android.exoplayer2.ExoPlayer) RequiresNonNull(org.checkerframework.checker.nullness.qual.RequiresNonNull)

Example 4 with PlayerView

use of com.google.android.exoplayer2.ui.PlayerView in project ExoPlayer by google.

the class PlayerView method setPlayer.

/**
 * Sets the {@link Player} to use.
 *
 * <p>To transition a {@link Player} from targeting one view to another, it's recommended to use
 * {@link #switchTargetView(Player, PlayerView, PlayerView)} rather than this method. If you do
 * wish to use this method directly, be sure to attach the player to the new view <em>before</em>
 * calling {@code setPlayer(null)} to detach it from the old one. This ordering is significantly
 * more efficient and may allow for more seamless transitions.
 *
 * @param player The {@link Player} to use, or {@code null} to detach the current player. Only
 *     players which are accessed on the main thread are supported ({@code
 *     player.getApplicationLooper() == Looper.getMainLooper()}).
 */
public void setPlayer(@Nullable Player player) {
    Assertions.checkState(Looper.myLooper() == Looper.getMainLooper());
    Assertions.checkArgument(player == null || player.getApplicationLooper() == Looper.getMainLooper());
    if (this.player == player) {
        return;
    }
    @Nullable Player oldPlayer = this.player;
    if (oldPlayer != null) {
        oldPlayer.removeListener(componentListener);
        if (oldPlayer.isCommandAvailable(COMMAND_SET_VIDEO_SURFACE)) {
            if (surfaceView instanceof TextureView) {
                oldPlayer.clearVideoTextureView((TextureView) surfaceView);
            } else if (surfaceView instanceof SurfaceView) {
                oldPlayer.clearVideoSurfaceView((SurfaceView) surfaceView);
            }
        }
    }
    if (subtitleView != null) {
        subtitleView.setCues(null);
    }
    this.player = player;
    if (useController()) {
        controller.setPlayer(player);
    }
    updateBuffering();
    updateErrorMessage();
    updateForCurrentTrackSelections(/* isNewPlayer= */
    true);
    if (player != null) {
        if (player.isCommandAvailable(COMMAND_SET_VIDEO_SURFACE)) {
            if (surfaceView instanceof TextureView) {
                player.setVideoTextureView((TextureView) surfaceView);
            } else if (surfaceView instanceof SurfaceView) {
                player.setVideoSurfaceView((SurfaceView) surfaceView);
            }
            updateAspectRatio();
        }
        if (subtitleView != null && player.isCommandAvailable(COMMAND_GET_TEXT)) {
            subtitleView.setCues(player.getCurrentCues());
        }
        player.addListener(componentListener);
        maybeShowController(false);
    } else {
        hideController();
    }
}
Also used : Player(com.google.android.exoplayer2.Player) TextureView(android.view.TextureView) Nullable(androidx.annotation.Nullable) SurfaceView(android.view.SurfaceView) GLSurfaceView(android.opengl.GLSurfaceView)

Example 5 with PlayerView

use of com.google.android.exoplayer2.ui.PlayerView in project prebid-mobile-android by prebid.

the class XandrInstreamVideoGamActivity method initializePlayer.

private void initializePlayer() {
    SimpleExoPlayer.Builder playerBuilder = new SimpleExoPlayer.Builder(this);
    player = playerBuilder.build();
    playerView.setPlayer(player);
    adsLoader.setPlayer(player);
    Uri uri = Uri.parse(getString(R.string.content_url));
    MediaItem mediaItem = MediaItem.fromUri(uri);
    DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this, getString(R.string.app_name));
    ProgressiveMediaSource.Factory mediaSourceFactory = new ProgressiveMediaSource.Factory(dataSourceFactory);
    MediaSource mediaSource = mediaSourceFactory.createMediaSource(mediaItem);
    DataSpec dataSpec = new DataSpec(adsUri);
    AdsMediaSource adsMediaSource = new AdsMediaSource(mediaSource, dataSpec, "ad", mediaSourceFactory, adsLoader, playerView);
    player.setMediaSource(adsMediaSource);
    player.setPlayWhenReady(true);
    player.prepare();
}
Also used : SimpleExoPlayer(com.google.android.exoplayer2.SimpleExoPlayer) ProgressiveMediaSource(com.google.android.exoplayer2.source.ProgressiveMediaSource) DefaultDataSourceFactory(com.google.android.exoplayer2.upstream.DefaultDataSourceFactory) Uri(android.net.Uri) DataSource(com.google.android.exoplayer2.upstream.DataSource) ProgressiveMediaSource(com.google.android.exoplayer2.source.ProgressiveMediaSource) AdsMediaSource(com.google.android.exoplayer2.source.ads.AdsMediaSource) MediaSource(com.google.android.exoplayer2.source.MediaSource) MediaItem(com.google.android.exoplayer2.MediaItem) DefaultDataSourceFactory(com.google.android.exoplayer2.upstream.DefaultDataSourceFactory) AdsMediaSource(com.google.android.exoplayer2.source.ads.AdsMediaSource) DataSpec(com.google.android.exoplayer2.upstream.DataSpec)

Aggregations

Uri (android.net.Uri)10 Nullable (androidx.annotation.Nullable)8 Intent (android.content.Intent)7 PlayerView (com.google.android.exoplayer2.ui.PlayerView)7 FrameLayout (android.widget.FrameLayout)6 MediaItem (com.google.android.exoplayer2.MediaItem)6 SimpleExoPlayer (com.google.android.exoplayer2.SimpleExoPlayer)5 Bundle (android.os.Bundle)4 Player (com.google.android.exoplayer2.Player)4 MediaSource (com.google.android.exoplayer2.source.MediaSource)4 ProgressiveMediaSource (com.google.android.exoplayer2.source.ProgressiveMediaSource)4 DataSource (com.google.android.exoplayer2.upstream.DataSource)3 DefaultDataSourceFactory (com.google.android.exoplayer2.upstream.DefaultDataSourceFactory)3 EventLogger (com.google.android.exoplayer2.util.EventLogger)3 UUID (java.util.UUID)3 SuppressLint (android.annotation.SuppressLint)2 Activity (android.app.Activity)2 Context (android.content.Context)2 SurfaceView (android.view.SurfaceView)2 TextureView (android.view.TextureView)2