Skip to content

Commit

Permalink
CastPlayer fixed and other fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
PhantomPainX committed Apr 12, 2023
1 parent 12b64ae commit 786a4c4
Showing 1 changed file with 71 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.app.PictureInPictureParams;
import android.content.ContentUris;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
Expand Down Expand Up @@ -38,6 +39,10 @@
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.fragment.app.Fragment;
import androidx.mediarouter.app.MediaRouteButton;
import androidx.mediarouter.media.MediaControlIntent;
import androidx.mediarouter.media.MediaRouteSelector;
import androidx.mediarouter.media.MediaRouter;

import com.getcapacitor.JSObject;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.DefaultLoadControl;
Expand Down Expand Up @@ -73,9 +78,11 @@
import com.google.android.exoplayer2.upstream.DefaultHttpDataSource;
import com.google.android.exoplayer2.util.MimeTypes;
import com.google.android.exoplayer2.util.Util;
import com.google.android.gms.cast.CastMediaControlIntent;
import com.google.android.gms.cast.framework.CastButtonFactory;
import com.google.android.gms.cast.framework.CastContext;
import com.google.android.gms.cast.framework.CastState;
import com.google.android.gms.cast.framework.CastStateListener;
import com.jeep.plugin.capacitor.capacitorvideoplayer.Notifications.NotificationCenter;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -131,7 +138,6 @@ public class FullscreenExoPlayerFragment extends Fragment {
private ImageButton closeBtn;
private ImageButton pipBtn;
private ImageButton resizeBtn;
private ImageButton lockBtn;
private ConstraintLayout constLayout;
private LinearLayout linearLayout;
private TextView header_tv;
Expand Down Expand Up @@ -174,17 +180,11 @@ public void run() {
private CastContext castContext;
private CastPlayer castPlayer;
private MediaItem mediaItem;

//declare byte[] atribute
public byte[] byte_image;
private Boolean isLocked = false;
private LinearLayout videoTimeContainer;
private ConstraintLayout layout_header_view;
private LinearLayout right_buttons;
private ImageButton exo_rew;
private ImageButton exo_play;
private ImageButton exo_pause;
private ImageButton exo_ffwd;
private MediaRouter mRouter;
private MediaRouter.Callback mCallback = new EmptyCallback();
private MediaRouteSelector mSelector;
private CastStateListener castStateListener = null;
private Boolean playerReady = false;

/**
* Create Fragment View
Expand All @@ -205,35 +205,37 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
Pbar = view.findViewById(R.id.indeterminateBar);
exo_progress = view.findViewById(R.id.exo_progress);
resizeBtn = view.findViewById(R.id.exo_resize);
lockBtn = view.findViewById(R.id.player_lock);
videoTimeContainer = view.findViewById(R.id.videoTimeContainer);
exo_play = view.findViewById(R.id.exo_play);
exo_pause = view.findViewById(R.id.exo_pause);
exo_rew = view.findViewById(R.id.exo_rew);
exo_ffwd = view.findViewById(R.id.exo_ffwd);
cast_image = view.findViewById(R.id.cast_image);
mediaRouteButton = view.findViewById(R.id.media_route_button);



if (!chromecast) {
mediaRouteButton.setVisibility(View.GONE);
} else {
castContext = CastContext.getSharedInstance(getContext());
castPlayer = new CastPlayer(CastContext.getSharedInstance(context));
CastButtonFactory.setUpMediaRouteButton(context, mediaRouteButton);
castContext = CastContext.getSharedInstance(context);
castPlayer = new CastPlayer(castContext);
mRouter = MediaRouter.getInstance(context);
mSelector = new MediaRouteSelector.Builder()
.addControlCategories(
Arrays.asList(
MediaControlIntent.CATEGORY_LIVE_AUDIO,
MediaControlIntent.CATEGORY_LIVE_VIDEO))
.build();

mediaRouteButtonColorWhite(mediaRouteButton);
if (castContext != null && castContext.getCastState() != CastState.NO_DEVICES_AVAILABLE) mediaRouteButton.setVisibility(
View.VISIBLE
);

castContext.addCastStateListener(
state -> {
if (state == CastState.NO_DEVICES_AVAILABLE) {
mediaRouteButton.setVisibility(View.GONE);
} else {
if (mediaRouteButton.getVisibility() == View.GONE) {
mediaRouteButton.setVisibility(View.VISIBLE);
}
}
castStateListener = state -> {
if (state == CastState.NO_DEVICES_AVAILABLE) {
mediaRouteButton.setVisibility(View.GONE);
} else {
if (mediaRouteButton.getVisibility() == View.GONE) {
mediaRouteButton.setVisibility(View.VISIBLE);
}
}
);
};
CastButtonFactory.setUpMediaRouteButton(context, mediaRouteButton);

if (artwork != "") {
MediaMetadata movieMetadata = new MediaMetadata.Builder()
Expand All @@ -246,7 +248,10 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa

new setCastImage().execute();
} else {
MediaMetadata movieMetadata = new MediaMetadata.Builder().setTitle(title).setSubtitle(smallTitle).build();
MediaMetadata movieMetadata = new MediaMetadata.Builder()
.setTitle(title)
.setSubtitle(smallTitle)
.build();
mediaItem =
new MediaItem.Builder().setUri(videoPath).setMimeType(MimeTypes.VIDEO_UNKNOWN).setMediaMetadata(movieMetadata).build();
}
Expand All @@ -267,6 +272,8 @@ public void onCastSessionAvailable() {
styledPlayerView.setPlayer(castPlayer);
styledPlayerView.setControllerShowTimeoutMs(0);
styledPlayerView.setControllerHideOnTouch(false);
//We perform a click because for some weird reason, the layout is black until the user clicks on it
styledPlayerView.performClick();
}

@Override
Expand Down Expand Up @@ -365,6 +372,7 @@ public void onPlayerStateChanged(boolean playWhenReady, int state) {
case ExoPlayer.STATE_READY:
stateString = "ExoPlayer.STATE_READY -";
Pbar.setVisibility(View.GONE);
playerReady = true;
styledPlayerView.setUseController(true);
linearLayout.setVisibility(View.INVISIBLE);
Log.v(TAG, "**** in ExoPlayer.STATE_READY firstReadyToPlay " + firstReadyToPlay);
Expand All @@ -383,11 +391,6 @@ public void onPlayerStateChanged(boolean playWhenReady, int state) {

resizeBtn.setVisibility(View.VISIBLE);

mediaRouteButtonColorWhite(mediaRouteButton);
if (castContext != null && castContext.getCastState() != CastState.NO_DEVICES_AVAILABLE) mediaRouteButton.setVisibility(
View.VISIBLE
);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && pipEnabled) {
pipBtn.setVisibility(View.VISIBLE);
}
Expand Down Expand Up @@ -561,13 +564,14 @@ protected void onPostExecute(Bitmap result) {
private void backPressed() {
if (isCastSession) {
playerExit();
return;
}
if (
!isInPictureInPictureMode &&
Build.VERSION.SDK_INT >= Build.VERSION_CODES.N &&
packageManager.hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE) &&
isPIPModeeEnabled &&
pipEnabled
pipEnabled && playerReady // <- playerReady: this prevents a crash if the user presses back before the player is ready (when enters in pip mode and tries to get the aspect ratio)
) {
pictureInPictureMode();
} else {
Expand Down Expand Up @@ -603,7 +607,13 @@ private void playerExit() {
player.setVolume(curVolume);
}
releasePlayer();
NotificationCenter.defaultCenter().postNotification("playerFullscreenDismiss", info);

// We control if the user lock the screen when the player is in pip mode
try {
NotificationCenter.defaultCenter().postNotification("playerFullscreenDismiss", info);
} catch (Exception e) {
Log.e(TAG, "Error in posting notification");
}
}

/**
Expand Down Expand Up @@ -652,16 +662,17 @@ private void checkPIPPermission() {
@Override
public void onStart() {
super.onStart();
if (chromecast) mRouter.addCallback(mSelector, mCallback, MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY);

if (Util.SDK_INT >= 24) {
if (styledPlayerView != null) {
// If cast is playing then it doesn't start the local player once get backs from background
if (castPlayer != null && !castPlayer.isCastSessionAvailable()) {
initializePlayer();
if (chromecast && castPlayer.isCastSessionAvailable()) return;

if (player.getCurrentPosition() != 0) {
firstReadyToPlay = false;
play();
}
initializePlayer();
if (player.getCurrentPosition() != 0) {
firstReadyToPlay = false;
play();
}
} else {
getActivity().finishAndRemoveTask();
Expand Down Expand Up @@ -690,6 +701,7 @@ public void onStop() {
@Override
public void onDestroy() {
super.onDestroy();
if (chromecast) mRouter.removeCallback(mCallback);
releasePlayer();
}

Expand All @@ -699,6 +711,7 @@ public void onDestroy() {
@Override
public void onPause() {
super.onPause();
if (chromecast) castContext.removeCastStateListener(castStateListener);
boolean isAppBackground = false;
if (bkModeEnabled) isAppBackground = isApplicationSentToBackground(context);

Expand Down Expand Up @@ -739,8 +752,9 @@ public void releasePlayer() {
player = null;
showSystemUI();
resetVariables();
if(castPlayer != null) {
castPlayer.release();
if (chromecast) {
castPlayer.release();
castPlayer = null;
}
}
}
Expand All @@ -751,6 +765,7 @@ public void releasePlayer() {
@Override
public void onResume() {
super.onResume();
if (chromecast) castContext.addCastStateListener(castStateListener);
if (!isInPictureInPictureMode) {
hideSystemUi();
if ((Util.SDK_INT < 24 || player == null)) {
Expand Down Expand Up @@ -1072,7 +1087,10 @@ public boolean isPlaying() {
public void play() {
PlaybackParameters param = new PlaybackParameters(videoRate);
player.setPlaybackParameters(param);
player.setPlayWhenReady(true);

/* If the user start the cast before the player is ready and playing, then the video will start
in the device and chromecast at the same time. This is to avoid that behaviour.*/
if (!isCastSession) player.setPlayWhenReady(true);
}

/**
Expand Down Expand Up @@ -1260,4 +1278,7 @@ public boolean isApplicationSentToBackground(final Context context) {
}
return false;
}

private final class EmptyCallback extends MediaRouter.Callback {
}
}

0 comments on commit 786a4c4

Please sign in to comment.