________________________________________________
Advance Video Player in Sketchware Pro
________________________________________________
👇 On Create Codes 👇
mVideoView = new xyz.doikki.videoplayer.player.VideoView(this);
LinearLayout.LayoutParams lplinear1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,600);
mVideoView.setLayoutParams(lplinear1);
linear1.addView(mVideoView);
mWidthPixels = getResources().getDisplayMetrics().widthPixels;
mVideoView.setLayoutParams(new LinearLayout.LayoutParams(mWidthPixels, mWidthPixels * 9 / 16 + 1));
mVideoView.setUrl(playurl);
mController = new StandardVideoController(this);
//mController.addDefaultControlComponent(title, false);
mController.addControlComponent(new CompleteView(this));
mController.addControlComponent(new ErrorView(this));
mController.addControlComponent(new PrepareView(this));
mController.addControlComponent(new GestureView(this));
VodControlView vodControlView = new VodControlView(this);
vodControlView.findViewById(R.id.speed).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new XPopup.Builder(PlayerActivity.this)
.popupPosition(com.lxj.xpopup.enums.PopupPosition.Right)//右边
//.hasStatusBarShadow(true) //启用状态栏阴影
.asCustom(new CustomDrawerPopupView(PlayerActivity.this))
.show();
}
});
vodControlView.findViewById(R.id.proportion).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new XPopup.Builder(PlayerActivity.this)
.popupPosition(com.lxj.xpopup.enums.PopupPosition.Right)//右边
//.hasStatusBarShadow(true) //启用状态栏阴影
.asCustom(new CustomDrawerPopupView1(PlayerActivity.this))
.show();
}
});
mController.addControlComponent(vodControlView);
TitleView titleView = new TitleView(this);
titleView.findViewById(R.id.pip).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Rational aspectRatio = new Rational(16, 9);
mPictureInPictureParamsBuilder.setAspectRatio(aspectRatio).build();
enterPictureInPictureMode(mPictureInPictureParamsBuilder.build());
}
});
titleView.setTitle(title);
mController.addControlComponent(titleView);
mVideoView.setVideoController(mController);
mVideoView.addOnStateChangeListener(new VideoView.SimpleOnStateChangeListener() {
@Override
public void onPlayStateChanged(int playState) {
switch (playState) {
case VideoView.STATE_PAUSED:
updatePictureInPictureActions(
R.drawable.dkplayer_ic_action_play_arrow, "播放", CONTROL_TYPE_PLAY, REQUEST_PLAY);
break;
case VideoView.STATE_PLAYING:
updatePictureInPictureActions(
R.drawable.dkplayer_ic_action_pause, "暂停", CONTROL_TYPE_PAUSE, REQUEST_PAUSE);
break;
case VideoView.STATE_PLAYBACK_COMPLETED:
updatePictureInPictureActions(
R.drawable.dkplayer_ic_action_replay, "重新播放", CONTROL_TYPE_REPLAY, REQUEST_REPLAY);
break;
}
}
});
mVideoView.start();
}
void updatePictureInPictureActions(
@DrawableRes int iconId, String title, int controlType, int requestCode) {
final ArrayList<RemoteAction> actions = new ArrayList<>();
// This is the PendingIntent that is invoked when a user clicks on the action item.
// You need to use distinct request codes for play and pause, or the PendingIntent won't
// be properly updated.
final PendingIntent intent =
PendingIntent.getBroadcast(
PlayerActivity.this,
requestCode,
new Intent(ACTION_MEDIA_CONTROL).putExtra(EXTRA_CONTROL_TYPE, controlType),
0);
final Icon icon = Icon.createWithResource(PlayerActivity.this, iconId);
actions.add(new RemoteAction(icon, title, title, intent));
mPictureInPictureParamsBuilder.setActions(actions);
// This is how you can update action items (or aspect ratio) for Picture-in-Picture mode.
// Note this call can happen even when the app is not in PiP mode. In that case, the
// arguments will be used for at the next call of #enterPictureInPictureMode.
setPictureInPictureParams(mPictureInPictureParamsBuilder.build());
}
public class CustomDrawerPopupView extends com.lxj.xpopup.core.DrawerPopupView {
public CustomDrawerPopupView(@androidx.annotation.NonNull Context context) {
super(context);
}
@Override
protected int getImplLayoutId() {
return R.layout.speed;
}
@Override
protected void onCreate() {
super.onCreate();
final TextView txt1 = findViewById(R.id.textview1);
final TextView txt2 = findViewById(R.id.textview2);
final TextView txt3 = findViewById(R.id.textview3);
final TextView txt4 = findViewById(R.id.textview4);
final TextView txt5 = findViewById(R.id.textview5);
if (speed.equals("0.75")) {
txt1.setTextColor(Color.parseColor("#FF39C5BA"));
}
if (speed.equals("1.0")) {
txt2.setTextColor(Color.parseColor("#FF39C5BC"));
}
if (speed.equals("1.25")) {
txt3.setTextColor(Color.parseColor("#FF39C5BC"));
}
if (speed.equals("1.5")) {
txt4.setTextColor(Color.parseColor("#FF39C5BC"));
}
if (speed.equals("2.0")) {
txt5.setTextColor(Color.parseColor("#FF39C5BC"));
}
findViewById(R.id.cardview1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mVideoView.setSpeed(0.75f);
speed = "0.75";
txt1.setTextColor(Color.parseColor("#FF39C5BC"));
txt2.setTextColor(Color.parseColor("#ffffff"));
txt3.setTextColor(Color.parseColor("#ffffff"));
txt4.setTextColor(Color.parseColor("#ffffff"));
txt5.setTextColor(Color.parseColor("#ffffff"));
}
});
findViewById(R.id.cardview2).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mVideoView.setSpeed(1.0f);
speed = "1.0";
txt2.setTextColor(Color.parseColor("#FF39C5BC"));
txt1.setTextColor(Color.parseColor("#ffffff"));
txt3.setTextColor(Color.parseColor("#ffffff"));
txt4.setTextColor(Color.parseColor("#ffffff"));
txt5.setTextColor(Color.parseColor("#ffffff"));
}
});
findViewById(R.id.cardview3).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mVideoView.setSpeed(1.25f);
speed = "1.25";
txt3.setTextColor(Color.parseColor("#FF39C5BC"));
txt2.setTextColor(Color.parseColor("#ffffff"));
txt1.setTextColor(Color.parseColor("#ffffff"));
txt4.setTextColor(Color.parseColor("#ffffff"));
txt5.setTextColor(Color.parseColor("#ffffff"));
}
});
findViewById(R.id.cardview4).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mVideoView.setSpeed(1.5f);
speed = "1.5";
txt4.setTextColor(Color.parseColor("#FF39C5BC"));
txt2.setTextColor(Color.parseColor("#ffffff"));
txt3.setTextColor(Color.parseColor("#ffffff"));
txt1.setTextColor(Color.parseColor("#ffffff"));
txt5.setTextColor(Color.parseColor("#ffffff"));
}
});
findViewById(R.id.cardview5).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mVideoView.setSpeed(2.0f);
speed = "2.0";
txt5.setTextColor(Color.parseColor("#FF39C5BC"));
txt2.setTextColor(Color.parseColor("#ffffff"));
txt3.setTextColor(Color.parseColor("#ffffff"));
txt4.setTextColor(Color.parseColor("#ffffff"));
txt1.setTextColor(Color.parseColor("#ffffff"));
}
});
}
}
{
}
public class CustomDrawerPopupView1 extends com.lxj.xpopup.core.DrawerPopupView {
public CustomDrawerPopupView1(@androidx.annotation.NonNull Context context) {
super(context);
}
@Override
protected int getImplLayoutId() {
return R.layout.proportion;
}
@Override
protected void onCreate() {
super.onCreate();
final TextView txt1 = findViewById(R.id.textview1);
final TextView txt2 = findViewById(R.id.textview2);
final TextView txt3 = findViewById(R.id.textview3);
final TextView txt4 = findViewById(R.id.textview4);
final TextView txt5 = findViewById(R.id.textview5);
if (proportion.equals("默认")) {
txt1.setTextColor(Color.parseColor("#FF39C5BC"));
}
if (proportion.equals("16:9")) {
txt2.setTextColor(Color.parseColor("#FF39C5BC"));
}
if (proportion.equals("原始大小")) {
txt3.setTextColor(Color.parseColor("#FF39C5BC"));
}
if (proportion.equals("填充")) {
txt4.setTextColor(Color.parseColor("#FF39C5BC"));
}
if (proportion.equals("居中裁剪")) {
txt5.setTextColor(Color.parseColor("#FF39C5BC"));
}
findViewById(R.id.cardview1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mVideoView.setScreenScaleType(VideoView.SCREEN_SCALE_DEFAULT);
proportion = "默认";
txt1.setTextColor(Color.parseColor("#FF39C5BC"));
txt2.setTextColor(Color.parseColor("#ffffff"));
txt3.setTextColor(Color.parseColor("#ffffff"));
txt4.setTextColor(Color.parseColor("#ffffff"));
txt5.setTextColor(Color.parseColor("#ffffff"));
}
});
findViewById(R.id.cardview2).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mVideoView.setScreenScaleType(VideoView.SCREEN_SCALE_16_9);
proportion = "16:9";
txt2.setTextColor(Color.parseColor("#FF39C5BC"));
txt1.setTextColor(Color.parseColor("#ffffff"));
txt3.setTextColor(Color.parseColor("#ffffff"));
txt4.setTextColor(Color.parseColor("#ffffff"));
txt5.setTextColor(Color.parseColor("#ffffff"));
}
});
findViewById(R.id.cardview3).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mVideoView.setScreenScaleType(VideoView.SCREEN_SCALE_ORIGINAL);
proportion = "原始大小";
txt3.setTextColor(Color.parseColor("#FF39C5BC"));
txt2.setTextColor(Color.parseColor("#ffffff"));
txt1.setTextColor(Color.parseColor("#ffffff"));
txt4.setTextColor(Color.parseColor("#ffffff"));
txt5.setTextColor(Color.parseColor("#ffffff"));
}
});
findViewById(R.id.cardview4).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mVideoView.setScreenScaleType(VideoView.SCREEN_SCALE_MATCH_PARENT);
proportion = "填充";
txt4.setTextColor(Color.parseColor("#FF39C5BC"));
txt2.setTextColor(Color.parseColor("#ffffff"));
txt3.setTextColor(Color.parseColor("#ffffff"));
txt1.setTextColor(Color.parseColor("#ffffff"));
txt5.setTextColor(Color.parseColor("#ffffff"));
}
});
findViewById(R.id.cardview5).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mVideoView.setScreenScaleType(VideoView.SCREEN_SCALE_CENTER_CROP);
proportion = "居中裁剪";
txt5.setTextColor(Color.parseColor("#FF39C5BC"));
txt2.setTextColor(Color.parseColor("#ffffff"));
txt3.setTextColor(Color.parseColor("#ffffff"));
txt4.setTextColor(Color.parseColor("#ffffff"));
txt1.setTextColor(Color.parseColor("#ffffff"));
}
});
}
}
{
________________________________________________
________________________________________________
👇Custom Variable Names👇
final PictureInPictureParams.Builder mPictureInPictureParamsBuilder = new PictureInPictureParams.Builder()
private static final String ACTION_MEDIA_CONTROL = "media_control"
private static final String EXTRA_CONTROL_TYPE = "control_type"
private static final int REQUEST_PLAY = 1
private static final int REQUEST_PAUSE = 2
private static final int CONTROL_TYPE_PLAY = 1
private static final int CONTROL_TYPE_PAUSE = 2
private static final int CONTROL_TYPE_REPLAY = 3
private BroadcastReceiver mReceiver
private StandardVideoController mController
private int mWidthPixels
private static final int REQUEST_REPLAY = 3
private xyz.doikki.videoplayer.player.VideoView mVideoView
________________________________________________
________________________________________________
Import name
android.app.PendingIntent
android.app.PictureInPictureParams
android.app.RemoteAction
android.content.BroadcastReceiver
xyz.doikki.videoplayer.player.VideoView
android.content.Context
android.content.IntentFilter
android.content.Intent
android.content.res.Configuration
android.graphics.drawable.Icon
java.util.ArrayList
android.view.View
androidx.annotation.DrawableRes
androidx.annotation.Nullable
androidx.annotation.RequiresApi
androidx.appcompat.app.AppCompatActivity
com.package.name.component.CompleteView
com.package.name.component.ErrorView
com.package.name.component.GestureView
com.package.name.component.PrepareView
com.package.name.component.TitleView
com.package.name.component.VodControlView
________________________________________________
________________________________________________
On Back Pressed Codes
if (!mVideoView.onBackPressed()) { super.onBackPressed(); }
}
@Override
public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode, Configuration newConfig) {
super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
Log.d("pip", "onPictureInPictureModeChanged: " + isInPictureInPictureMode);
if (isInPictureInPictureMode) {
mVideoView.setVideoController(null);
mVideoView.setLayoutParams(new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
// Starts receiving events from action items in PiP mode.
mReceiver =
new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent == null
|| !ACTION_MEDIA_CONTROL.equals(intent.getAction())) {
return;
}
// This is where we are called mBack from Picture-in-Picture action
// items.
final int controlType = intent.getIntExtra(EXTRA_CONTROL_TYPE, 0);
switch (controlType) {
case CONTROL_TYPE_PLAY:
mVideoView.start();
break;
case CONTROL_TYPE_PAUSE:
mVideoView.pause();
break;
case CONTROL_TYPE_REPLAY:
mVideoView.replay(true);
break;
}
}
};
registerReceiver(mReceiver, new IntentFilter(ACTION_MEDIA_CONTROL));
} else {
// We are out of PiP mode. We can stop receiving events from it.
unregisterReceiver(mReceiver);
mReceiver = null;
Log.d("pip", "onPictureInPictureModeChanged: " + mVideoView);
mVideoView.setLayoutParams(new LinearLayout.LayoutParams(
mWidthPixels,
mWidthPixels * 9 / 16 + 1));
mVideoView.setVideoController(mController);
mVideoView.requestLayout();
}
________________________________________________
________________________________________________
👇On Stop Codes👇
mVideoView.pause();
________________________________________________
_______________________________________________
👇On Destroy Codes👇
mVideoView.release();
________________________________________________
End
________________________________________________