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( SystemChrome.setPreferredOrientations(
[DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight]); [DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight]);
// initVideoPlayer(); initVideoPlayer();
initFijkPlayer(); // initFijkPlayer();
} }
void initVideoPlayer() { void initVideoPlayer() {
@ -55,7 +55,32 @@ class _CustomVideoPlayerState extends State<CustomVideoPlayer> {
}); });
} }
void onKey(RawKeyEvent event) { 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) { switch (event.character) {
case ' ': case ' ':
if (player?.state == FijkState.paused) { if (player?.state == FijkState.paused) {
@ -69,7 +94,7 @@ class _CustomVideoPlayerState extends State<CustomVideoPlayer> {
case 22: case 22:
// player!.pause().then((value) { // player!.pause().then((value) {
int pos = min(player!.value.duration.inMilliseconds, int pos = min(player!.value.duration.inMilliseconds,
player!.currentPos.inMilliseconds + 10000); player!.currentPos.inMilliseconds + 30000);
player!.seekTo(pos).then((value) { player!.seekTo(pos).then((value) {
if(_timer != null){ if(_timer != null){
_timer!.cancel(); _timer!.cancel();
@ -97,14 +122,19 @@ class _CustomVideoPlayerState extends State<CustomVideoPlayer> {
child: _controller!.value.isInitialized child: _controller!.value.isInitialized
? AspectRatio( ? AspectRatio(
aspectRatio: _controller!.value.aspectRatio, aspectRatio: _controller!.value.aspectRatio,
child: VideoPlayer(_controller!), child: RawKeyboardListener(
onKey: onKey,
autofocus: true,
focusNode: FocusNode(),
child: VideoPlayer(_controller!),
),
) )
: Container(), : Container(),
) )
: Container( : Container(
alignment: Alignment.center, alignment: Alignment.center,
child: RawKeyboardListener( child: RawKeyboardListener(
onKey: onKey, onKey: fijkOnKey,
focusNode: FocusNode(), focusNode: FocusNode(),
autofocus: true, autofocus: true,
child: FijkView( child: FijkView(
@ -112,7 +142,7 @@ class _CustomVideoPlayerState extends State<CustomVideoPlayer> {
panelBuilder: (FijkPlayer player, FijkData data, panelBuilder: (FijkPlayer player, FijkData data,
BuildContext context, Size viewSize, Rect texturePos) { BuildContext context, Size viewSize, Rect texturePos) {
return RawKeyboardListener( return RawKeyboardListener(
onKey: onKey, onKey: fijkOnKey,
focusNode: FocusNode(), focusNode: FocusNode(),
autofocus: true, autofocus: true,
child: PopScope( child: PopScope(