颤振如何滑动audio_service软件包的通知?

时间:2020-10-16 07:16:47

标签: flutter audio notifications swipe

当前,关闭通知的唯一方法是按停止按钮。但是我不知道如何用可滑动的“东西”包裹通知小部件。您能告诉我任何适用于此的程序包或我必须用audio_service程序包包装东西的代码吗?

audio_service软件包的发布者是-ryanheise.com

1 个答案:

答案 0 :(得分:2)

作为有用的提示,搜索audio_service的整个文档以找到某个问题的答案的最简单方法是导航到audio_service.dart文件(包含整个插件),然后搜索单词“刷卡”。您会在AudioService.start上找到此选项:

  /// [androidStopForegroundOnPause] will switch the Android service to a lower
  /// priority state when playback is paused allowing the user to swipe away the
  /// notification. Note that while in this lower priority state, the operating
  /// system will also be able to kill your service at any time to reclaim
  /// resources.

然后在BackgroundAudioTask内:

  /// Called on Android when the user swipes away the notification. The default
  /// implementation (which you may override) calls [onStop]. Note that by
  /// default, the service runs in the foreground state which (despite the name)
  /// allows the service to run at a high priority in the background without the
  /// operating system killing it. While in the foreground state, the
  /// notification cannot be swiped away. You can pass a parameter value of
  /// `true` for `androidStopForegroundOnPause` in the [AudioService.start]
  /// method if you would like the service to exit the foreground state when
  /// playback is paused. This will allow the user to swipe the notification
  /// away while playback is paused (but it will also allow the operating system
  /// to kill your service at any time to free up resources).
  Future<void> onClose() => onStop();

(您可能会发现它比阅读在线HTML文档更容易搜索。)

上面的意思是,您需要将androidStopForegroundOnPause: true作为参数传递给AudioService.start,然后在用户滑动时是否要处理除默认行为以外的任何特殊行为(停止服务)离开通知,您可以选择在自己的背景音频任务中覆盖onClose。没有适用于iOS的版本。

请注意,在播放音频时无法清除Android通知,但上述选项可让您从暂停状态清除通知。