Java Code Examples for com.google.android.exoplayer2.SimpleExoPlayer#setPlayWhenReady()

The following examples show how to use com.google.android.exoplayer2.SimpleExoPlayer#setPlayWhenReady() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AdPlayingMonitor.java    From TubiPlayer with MIT License 6 votes vote down vote up
private void seekOrSkip() {
    if (fsmPlayer == null) {
        return;
    }

    if (fsmPlayer instanceof FsmPlayer) {

        if (((FsmPlayer) fsmPlayer).getController() != null) {
            SimpleExoPlayer adPlayer = ((FsmPlayer) fsmPlayer).getController().getAdPlayer();

            if (adPlayer != null && adPlayer.getPlaybackState() == ExoPlayer.STATE_BUFFERING) {

                long position = adPlayer.getCurrentPosition() + 1000 < adPlayer.getDuration() ?
                        adPlayer.getCurrentPosition() + 1000 :
                        adPlayer.getDuration();
                adPlayer.seekTo(position);
                adPlayer.setPlayWhenReady(true);
                return;
            }
        }
    }
}
 
Example 2
Source File: ExoPlayerManager.java    From PreviewSeekBar with Apache License 2.0 6 votes vote down vote up
private SimpleExoPlayer createPlayer() {
    SimpleExoPlayer player = new SimpleExoPlayer.Builder(playerView.getContext()).build();
    player.setPlayWhenReady(true);
    player.prepare(mediaSourceBuilder.getMediaSource(false));
    player.addListener(eventListener);
    return player;
}
 
Example 3
Source File: MoviePlayingState.java    From TubiPlayer with MIT License 4 votes vote down vote up
private void stopAdandPlayerMovie(PlayerUIController controller, PlayerAdLogicController componentController,
        MediaModel movieMedia) {

    SimpleExoPlayer adPlayer = controller.getAdPlayer();
    SimpleExoPlayer moviePlayer = controller.getContentPlayer();

    boolean shouldReprepareForSinglePlayer = PlayerDeviceUtils.useSinglePlayer() && controller.isPlayingAds;

    //first remove the AdPlayer's listener and pause the player
    adPlayer.removeAnalyticsListener(componentController.getAdPlayingMonitor());

    if (shouldReprepareForSinglePlayer) {
        adPlayer.setPlayWhenReady(false);
    }

    //then update the playerView with SimpleExoPlayer and Movie MediaModel
    TubiExoPlayerView tubiExoPlayerView = (TubiExoPlayerView) controller.getExoPlayerView();
    tubiExoPlayerView.setPlayer(moviePlayer, componentController.getTubiPlaybackInterface());
    tubiExoPlayerView.setMediaModel(movieMedia);

    //prepare the moviePlayer with data source and set it play

    boolean haveResumePosition = controller.getMovieResumePosition() != C.TIME_UNSET;

    boolean isPlayerIdle = moviePlayer.getPlaybackState() == Player.STATE_IDLE;

    if (shouldReprepareForSinglePlayer || isPlayerIdle) {
        moviePlayer.prepare(movieMedia.getMediaSource(), !haveResumePosition, false);
        updatePlayerPosition(moviePlayer, controller);
    }

    moviePlayer.setPlayWhenReady(true);

    controller.isPlayingAds = false;

    hideVpaidNShowPlayer(controller);

    //when return to the movie playing state, show the subtitle if necessary
    if (shouldShowSubtitle()) {
        ((TubiExoPlayerView) controller.getExoPlayerView()).getSubtitleView().setVisibility(View.VISIBLE);
    }
}
 
Example 4
Source File: AdPlayingState.java    From TubiPlayer with MIT License 4 votes vote down vote up
private void playingAdAndPauseMovie(PlayerUIController controller, AdMediaModel adMediaModel,
        PlayerAdLogicController componentController, FsmPlayer fsmPlayer) {

    SimpleExoPlayer adPlayer = controller.getAdPlayer();
    SimpleExoPlayer moviePlayer = controller.getContentPlayer();

    // then setup the player for ad to playe
    MediaModel adMedia = adMediaModel.nextAD();

    //TODO: Handle situation when ad medaia is empty, or invalid urls.
    if (adMedia != null) {

        if (adMedia.isVpaid()) {
            fsmPlayer.transit(Input.VPAID_MANIFEST);
            return;
        }

        hideVpaidNShowPlayer(controller);

        moviePlayer.setPlayWhenReady(false);

        // We need save movie play position before play ads for single player instance case
        if (PlayerDeviceUtils.useSinglePlayer() && !controller.isPlayingAds) {
            long resumePosition = Math.max(0, moviePlayer.getCurrentPosition());
            controller.setMovieResumeInfo(moviePlayer.getCurrentWindowIndex(), resumePosition);
        }

        //prepare the moviePlayer with data source and set it play

        boolean haveResumePosition = controller.getAdResumePosition() != C.TIME_UNSET;

        //prepare the mediaSource to AdPlayer
        adPlayer.prepare(adMedia.getMediaSource(), !haveResumePosition, true);
        controller.isPlayingAds = true;

        if (haveResumePosition) {
            adPlayer.seekTo(adPlayer.getCurrentWindowIndex(), controller.getAdResumePosition());
        }

        //update the ExoPlayerView with AdPlayer and AdMedia
        TubiExoPlayerView tubiExoPlayerView = (TubiExoPlayerView) controller.getExoPlayerView();
        tubiExoPlayerView.setPlayer(adPlayer, componentController.getTubiPlaybackInterface());
        tubiExoPlayerView.setMediaModel(adMedia);
        //update the numbers of ad left to give user indicator
        tubiExoPlayerView.setAvailableAdLeft(adMediaModel.nubmerOfAd());

        //Player the Ad.
        adPlayer.setPlayWhenReady(true);
        adPlayer.addAnalyticsListener(componentController.getAdPlayingMonitor());
        adPlayer.setMetadataOutput(componentController.getAdPlayingMonitor());

        //hide the subtitle view when ad is playing
        ((TubiExoPlayerView) controller.getExoPlayerView()).getSubtitleView().setVisibility(View.INVISIBLE);
    }
}
 
Example 5
Source File: MainActivity.java    From WhatsAppStatusSaver with Apache License 2.0 4 votes vote down vote up
public void showImagePopup(Point p, final String uri) {
        Activity context = MainActivity.this;
        //COMPLETED solving video problem

        final Dialog dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.image_popup_layout);
        dialog.show();
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.copyFrom(dialog.getWindow().getAttributes());
        dialog.getWindow().setAttributes(lp);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        dialog.getWindow().setDimAmount(0);


        // Getting a reference to Close button, and close the popup when clicked.
        FloatingActionButton close = (FloatingActionButton) dialog.findViewById(R.id.close_image_popup_button);
        ImageView statusImage = (ImageView) dialog.findViewById(R.id.full_status_image_view);
        final SimpleExoPlayerView simpleExoPlayerView = dialog.findViewById(R.id.full_status_video_view);
        final SimpleExoPlayer player;
        if (uri.endsWith(".jpg")) {
            GlideApp.with(context).load(uri).into(statusImage);
        } else if (uri.endsWith(".mp4")) {
            statusImage.setVisibility(View.GONE);
            simpleExoPlayerView.setVisibility(View.VISIBLE);
            Uri myUri = Uri.parse(uri); // initialize Uri here

            // 1. Create a default TrackSelector
            BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
            TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
            TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);

// 2. Create a default LoadControl
            LoadControl loadControl = new DefaultLoadControl();

// 3. Create the player
            player = ExoPlayerFactory.newSimpleInstance(this, trackSelector, loadControl);

//Set media controller
            simpleExoPlayerView.setUseController(true);
            simpleExoPlayerView.requestFocus();

// Bind the player to the view.
            simpleExoPlayerView.setPlayer(player);

            //Measures bandwidth during playback. Can be null if not required.
            DefaultBandwidthMeter bandwidthMeterA = new DefaultBandwidthMeter();
//Produces DataSource instances through which media data is loaded.
            DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(this, Util.
                    getUserAgent(this, "exoplayer2example"), bandwidthMeterA);
//Produces Extractor instances for parsing the media data.
            ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();

            MediaSource videoSource = new ExtractorMediaSource(myUri, dataSourceFactory, extractorsFactory, null, null);
            player.prepare(videoSource);
            player.setPlayWhenReady(true); //run file/link when ready to play.
            dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialogInterface) {
                    player.release();
                }
            });

        }
        close.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
//                popup.dismiss();
                dialog.cancel();
            }
        });
    }