video_player 按键监听

This commit is contained in:
shikong 2023-11-25 17:59:42 +08:00
parent 6b1b409317
commit a88c9d6298

View File

@ -29,8 +29,8 @@ class _CustomVideoPlayerState extends State<CustomVideoPlayer> {
SystemChrome.setPreferredOrientations(
[DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight]);
// initVideoPlayer();
initFijkPlayer();
initVideoPlayer();
// initFijkPlayer();
}
void initVideoPlayer() {
@ -56,6 +56,31 @@ class _CustomVideoPlayerState extends State<CustomVideoPlayer> {
}
void onKey(RawKeyEvent event){
if(event is RawKeyUpEvent) return;
RawKeyEventDataAndroid e = event.data as RawKeyEventDataAndroid;
switch (e.keyCode) {
case 21:
int pos = _controller!.value.position.inMilliseconds;
_controller!.seekTo(Duration(milliseconds: pos - 30000));
break;
case 22:
int pos = _controller!.value.position.inMilliseconds;
_controller!.seekTo(Duration(milliseconds: pos + 30000));
break;
case 62:
if(_controller!.value.isPlaying){
_controller!.pause();
} else {
_controller!.play();
}
break;
default:
debugPrint("$e");
}
}
void fijkOnKey(RawKeyEvent event) {
if(event is RawKeyUpEvent) return;
switch (event.character) {
case ' ':
if (player?.state == FijkState.paused) {
@ -69,7 +94,7 @@ class _CustomVideoPlayerState extends State<CustomVideoPlayer> {
case 22:
// player!.pause().then((value) {
int pos = min(player!.value.duration.inMilliseconds,
player!.currentPos.inMilliseconds + 10000);
player!.currentPos.inMilliseconds + 30000);
player!.seekTo(pos).then((value) {
if(_timer != null){
_timer!.cancel();
@ -97,14 +122,19 @@ class _CustomVideoPlayerState extends State<CustomVideoPlayer> {
child: _controller!.value.isInitialized
? AspectRatio(
aspectRatio: _controller!.value.aspectRatio,
child: RawKeyboardListener(
onKey: onKey,
autofocus: true,
focusNode: FocusNode(),
child: VideoPlayer(_controller!),
),
)
: Container(),
)
: Container(
alignment: Alignment.center,
child: RawKeyboardListener(
onKey: onKey,
onKey: fijkOnKey,
focusNode: FocusNode(),
autofocus: true,
child: FijkView(
@ -112,7 +142,7 @@ class _CustomVideoPlayerState extends State<CustomVideoPlayer> {
panelBuilder: (FijkPlayer player, FijkData data,
BuildContext context, Size viewSize, Rect texturePos) {
return RawKeyboardListener(
onKey: onKey,
onKey: fijkOnKey,
focusNode: FocusNode(),
autofocus: true,
child: PopScope(