Flutter应用程序,在用户按下耳机按钮时打印消息

huangapple go评论70阅读模式
英文:

Flutter App that print message when headset button press by user

问题

以下是您要翻译的内容:

"Can any one help me i used so many pub.dev package but i cannot find my answer

I try on this packages

  • audio_service 0.18.9
  • headset_connection_event
  • flutter_headset_detector"
英文:

Can any one help me i used so many pub.dev package but i cannot find my answer

I try on this packages

  • audio_service 0.18.9
  • headset_connection_event
  • flutter_headset_detector

答案1

得分: 0

使用hardware_buttons包(https://pub.dev/packages/hardware_buttons)应该很容易实现,它允许你在Flutter应用中监听硬件按钮事件。

由于耳机上的硬件按钮触发与手机甚至键盘上的按钮相同的事件,所以这应该是可能的:

void _listenToHardwareButtons() {
  hardwareButtons.volumeButtonEvents.listen((event) {
    if (event == hardwareButtons.VolumeButtonEvent.VOLUME_UP) {
      print('耳机上的音量增加按钮被按下');
    } else if (event == hardwareButtons.VolumeButtonEvent.VOLUME_DOWN) {
      print('耳机上的音量减小按钮被按下');
    }
  });
}

你可能需要在androidmanifest.xml中添加所需的权限以监听硬件按钮事件,但出乎意料的是,我并不需要这样做。

现在这应该也可以在iOS上工作,但我无法尝试,因为我目前没有iPhone。

如果你只想监听音量键,你可以查看:https://pub.dev/packages/volume_watcher

对于桌面/网络应用:###

你可以使用Focus widget或RawKeyboardListener widget来监听键盘事件:

Focus(
  autofocus: true,
  focusNode: FocusNode(),
  onKeyEvent: (node, event) {
    if (event.physicalKey == PhysicalKeyboardKey.audioVolumeUp) {
      return KeyEventResult.handled;
    }
    return KeyEventResult.ignored;
  },
  child: Container(),
),
英文:

It should be easily possible with the hardware_buttons package (https://pub.dev/packages/hardware_buttons), which allows you to listen to hardware button events in your Flutter app.

Since the Hardwarebuttons on a headset fire the same event as the buttons on your phone or even keyboard this should be possible:

void _listenToHardwareButtons() {
  hardwareButtons.volumeButtonEvents.listen((event) {
    if (event == hardwareButtons.VolumeButtonEvent.VOLUME_UP) {
      print('Volume Up button pressed on headset');
    } else if (event == hardwareButtons.VolumeButtonEvent.VOLUME_DOWN) {
      print('Volume Down button pressed on headset');
    }
  });
}

You might need to add the required permissions for listening to hardware button-events in the androidmanifest.xml. I did not need to surprisingly.

This should even work on iOS now. But i could not try this out since i don't have an iPhone with me currently.

If you only want to listen to the VolumeKeys you might want to check out: https://pub.dev/packages/volume_watcher

For Desktop/Web:

You can use Focus widget or RawKeyboardListener widget to listen to keyboard events:

  Focus(
    autofocus: true,
    focusNode: FocusNode(),
    onKeyEvent: (node, event) {
      if (event.physicalKey == PhysicalKeyboardKey.audioVolumeUp) {
        return KeyEventResult.handled;
      }
      return KeyEventResult.ignored;
    },
    child: Container(),
  ),

huangapple
  • 本文由 发表于 2023年4月1日 01:01:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75901013.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定