add pip backward/forward btns

Signed-off-by: dom <githubaccount56556@proton.me>
This commit is contained in:
dom
2026-06-01 10:04:11 +08:00
parent 6661332316
commit 350a52510c
7 changed files with 225 additions and 23 deletions

View File

@@ -3,6 +3,8 @@ package com.example.piliplus;
import android.app.Activity;
import android.app.PendingIntent;
import android.app.PictureInPictureParams;
import android.app.RemoteAction;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
@@ -11,16 +13,22 @@ import android.content.pm.ShortcutManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Icon;
import android.media.session.PlaybackState;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.util.Rational;
import androidx.annotation.DrawableRes;
import androidx.annotation.Keep;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import com.github.dart_lang.jni_flutter.JniFlutterPlugin;
import java.util.ArrayList;
import java.util.Objects;
@Keep
public final class AndroidHelper {
public static volatile boolean isPipMode = false;
@@ -94,13 +102,52 @@ public final class AndroidHelper {
}
}
public static void enterPip(long engineId, int width, int height) {
public static void enterPip(long engineId, int width, int height, boolean isLive, boolean isPlaying) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Activity activity = JniFlutterPlugin.getActivity(engineId);
assert activity != null;
PictureInPictureParams.Builder builder = new PictureInPictureParams.Builder()
.setAspectRatio(new Rational(width, height));
setPipActions(activity, builder, isLive, isPlaying);
activity.enterPictureInPictureMode(builder.build());
}
}
}
@RequiresApi(api = Build.VERSION_CODES.O)
public static void updatePipActions(long engineId, boolean isLive, boolean isPlaying) {
Activity activity = JniFlutterPlugin.getActivity(engineId);
assert activity != null;
PictureInPictureParams.Builder builder = new PictureInPictureParams.Builder();
setPipActions(activity, builder, isLive, isPlaying);
activity.setPictureInPictureParams(builder.build());
}
@RequiresApi(api = Build.VERSION_CODES.O)
private static void setPipActions(Activity activity, PictureInPictureParams.Builder builder, boolean isLive, boolean isPlaying) {
ComponentName mbrComponent = MediaHelper.getMediaButtonReceiverComponent(activity);
if (mbrComponent == null) return;
ArrayList<RemoteAction> actionList = new ArrayList<>();
if (!isLive) {
actionList.add(getRemoteAction(mbrComponent, activity, R.drawable.ic_baseline_replay_10_24, "ACTION_REWIND", PlaybackState.ACTION_REWIND));
}
if (isPlaying) {
actionList.add(getRemoteAction(mbrComponent, activity, android.R.drawable.ic_media_pause, "ACTION_PAUSE", PlaybackState.ACTION_PAUSE));
} else {
actionList.add(getRemoteAction(mbrComponent, activity, android.R.drawable.ic_media_play, "ACTION_PLAY", PlaybackState.ACTION_PLAY));
}
if (!isLive) {
actionList.add(getRemoteAction(mbrComponent, activity, R.drawable.ic_baseline_forward_10_24, "ACTION_FAST_FORWARD", PlaybackState.ACTION_FAST_FORWARD));
}
builder.setActions(actionList);
}
@RequiresApi(api = Build.VERSION_CODES.O)
private static RemoteAction getRemoteAction(@NonNull ComponentName mbrComponent, Activity activity, @DrawableRes int resId, String title, long action) {
return new RemoteAction(
Icon.createWithResource(activity, resId),
title,
title,
Objects.requireNonNull(MediaHelper.buildMediaButtonPendingIntent(activity, mbrComponent, action))
);
}
}

View File

@@ -0,0 +1,90 @@
/*
* Copyright 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.piliplus;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.media.session.PlaybackState;
import android.os.Build;
import android.util.Log;
import android.view.KeyEvent;
import java.util.List;
public class MediaHelper {
private static final String TAG = "MediaButtonReceiver";
public static PendingIntent buildMediaButtonPendingIntent(Context context, ComponentName mbrComponent, long action) {
if (mbrComponent == null) {
Log.w(TAG, "The component name of media button receiver should be provided.");
return null;
}
int keyCode = PlaybackStateCompat_toKeyCode(action);
if (keyCode == KeyEvent.KEYCODE_UNKNOWN) {
Log.w(TAG,
"Cannot build a media button pending intent with the given action: " + action);
return null;
}
Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
intent.setComponent(mbrComponent);
intent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, keyCode));
intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
return PendingIntent.getBroadcast(context, keyCode, intent,
Build.VERSION.SDK_INT >= 31 ? PendingIntent.FLAG_MUTABLE : 0);
}
public static int PlaybackStateCompat_toKeyCode(long action) {
if (action == PlaybackState.ACTION_PLAY) {
return KeyEvent.KEYCODE_MEDIA_PLAY;
} else if (action == PlaybackState.ACTION_PAUSE) {
return KeyEvent.KEYCODE_MEDIA_PAUSE;
} else if (action == PlaybackState.ACTION_SKIP_TO_NEXT) {
return KeyEvent.KEYCODE_MEDIA_NEXT;
} else if (action == PlaybackState.ACTION_SKIP_TO_PREVIOUS) {
return KeyEvent.KEYCODE_MEDIA_PREVIOUS;
} else if (action == PlaybackState.ACTION_STOP) {
return KeyEvent.KEYCODE_MEDIA_STOP;
} else if (action == PlaybackState.ACTION_FAST_FORWARD) {
return KeyEvent.KEYCODE_MEDIA_FAST_FORWARD;
} else if (action == PlaybackState.ACTION_REWIND) {
return KeyEvent.KEYCODE_MEDIA_REWIND;
} else if (action == PlaybackState.ACTION_PLAY_PAUSE) {
return KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE;
}
return KeyEvent.KEYCODE_UNKNOWN;
}
public static ComponentName getMediaButtonReceiverComponent(Context context) {
Intent queryIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
queryIntent.setPackage(context.getPackageName());
PackageManager pm = context.getPackageManager();
List<ResolveInfo> resolveInfos = pm.queryBroadcastReceivers(queryIntent, 0);
if (resolveInfos.size() == 1) {
ResolveInfo resolveInfo = resolveInfos.get(0);
return new ComponentName(resolveInfo.activityInfo.packageName,
resolveInfo.activityInfo.name);
} else if (resolveInfos.size() > 1) {
Log.w(TAG, "More than one BroadcastReceiver that handles "
+ Intent.ACTION_MEDIA_BUTTON + " was found, returning null.");
}
return null;
}
}

View File

@@ -262,6 +262,8 @@ class PlPlayerController with BlockConfigMixin {
PageUtils.enterPip(
width: state.width == 0 ? width : state.width,
height: state.height == 0 ? height : state.height,
isLive: isLive,
isPlaying: playerStatus.isPlaying,
);
}
}
@@ -798,18 +800,10 @@ class PlPlayerController with BlockConfigMixin {
);
}),
// 媒体通知监听
if (videoPlayerServiceHandler != null) ...[
playerStatus.listen((PlayerStatus event) {
videoPlayerServiceHandler!.onStatusChange(
event,
isBuffering.value,
isLive,
);
}),
if (videoPlayerServiceHandler != null)
positionSeconds.listen((int event) {
videoPlayerServiceHandler!.onPositionChange(Duration(seconds: event));
}),
],
];
}

View File

@@ -1,4 +1,5 @@
import 'dart:io' show File;
import 'dart:io' show File, Platform;
import 'dart:ui' show PlatformDispatcher;
import 'package:PiliPlus/common/constants.dart';
import 'package:PiliPlus/grpc/bilibili/app/listener/v1.pb.dart' show DetailItem;
@@ -9,6 +10,7 @@ import 'package:PiliPlus/models_new/video/video_detail/data.dart';
import 'package:PiliPlus/models_new/video/video_detail/page.dart';
import 'package:PiliPlus/plugin/pl_player/controller.dart';
import 'package:PiliPlus/plugin/pl_player/models/play_status.dart';
import 'package:PiliPlus/utils/android/bindings.g.dart';
import 'package:PiliPlus/utils/image_utils.dart';
import 'package:PiliPlus/utils/path_utils.dart';
import 'package:audio_service/audio_service.dart';
@@ -102,6 +104,13 @@ class VideoPlayerServiceHandler extends BaseAudioHandler with SeekHandler {
},
),
);
if (Platform.isAndroid && AndroidHelper.isPipMode) {
AndroidHelper.updatePipActions(
PlatformDispatcher.instance.engineId!,
isLive,
playing,
);
}
}
void onStatusChange(PlayerStatus status, bool isBuffering, isLive) {

View File

@@ -356,10 +356,65 @@ extension type AndroidHelper._(jni$_.JObject _$this) implements jni$_.JObject {
static final _id_enterPip = _class.staticMethodId(
r'enterPip',
r'(JII)V',
r'(JIIZZ)V',
);
static final _enterPip =
jni$_.ProtectedJniExtensions.lookup<
jni$_.NativeFunction<
jni$_.JThrowablePtr Function(
jni$_.Pointer<jni$_.Void>,
jni$_.JMethodIDPtr,
jni$_.VarArgs<
(
jni$_.Int64,
jni$_.Int32,
jni$_.Int32,
jni$_.Int32,
jni$_.Int32,
)
>,
)
>
>('globalEnv_CallStaticVoidMethod')
.asFunction<
jni$_.JThrowablePtr Function(
jni$_.Pointer<jni$_.Void>,
jni$_.JMethodIDPtr,
core$_.int,
core$_.int,
core$_.int,
core$_.int,
core$_.int,
)
>();
/// from: `static public void enterPip(long engineId, int width, int height, boolean isLive, boolean isPlaying)`
static void enterPip(
core$_.int engineId,
core$_.int width,
core$_.int height,
core$_.bool isLive,
core$_.bool isPlaying,
) {
final _$$classRef = _class.reference;
_enterPip(
_$$classRef.pointer,
_id_enterPip.pointer,
engineId,
width,
height,
isLive ? 1 : 0,
isPlaying ? 1 : 0,
).check();
}
static final _id_updatePipActions = _class.staticMethodId(
r'updatePipActions',
r'(JZZ)V',
);
static final _updatePipActions =
jni$_.ProtectedJniExtensions.lookup<
jni$_.NativeFunction<
jni$_.JThrowablePtr Function(
@@ -379,19 +434,19 @@ extension type AndroidHelper._(jni$_.JObject _$this) implements jni$_.JObject {
)
>();
/// from: `static public void enterPip(long engineId, int width, int height)`
static void enterPip(
/// from: `static public void updatePipActions(long engineId, boolean isLive, boolean isPlaying)`
static void updatePipActions(
core$_.int engineId,
core$_.int width,
core$_.int height,
core$_.bool isLive,
core$_.bool isPlaying,
) {
final _$$classRef = _class.reference;
_enterPip(
_updatePipActions(
_$$classRef.pointer,
_id_enterPip.pointer,
_id_updatePipActions.pointer,
engineId,
width,
height,
isLive ? 1 : 0,
isPlaying ? 1 : 0,
).check();
}
}

View File

@@ -189,7 +189,12 @@ abstract final class PageUtils {
return (min <= aspectRatio) && (aspectRatio <= max);
}
static void enterPip({int? width, int? height}) {
static void enterPip({
int? width,
int? height,
required bool isLive,
required bool isPlaying,
}) {
if (width != null &&
height != null &&
!_fitsInAndroidRequirements(width, height)) {
@@ -205,6 +210,8 @@ abstract final class PageUtils {
PlatformDispatcher.instance.engineId!,
width ?? 16,
height ?? 9,
isLive,
isPlaying,
);
}

View File

@@ -15,7 +15,7 @@ void main(List<String> args) {
androidSdkConfig: AndroidSdkConfig(addGradleDeps: true),
sourcePath: [packageRoot.resolve('android/app/src/main/java')],
classes: [
'com.example.piliplus',
'com.example.piliplus.AndroidHelper',
'java.lang.Runnable',
],
),