Merged
fix(ohos): register AVSession play/pause commands as a pair; add skip controls to example #68
fix(ohos): register AVSession play/pause commands as a pair; add skip controls to example #68
Merged
xileileiAdd on 23 hours ago
xileilei
xileilei
23 hours ago

变更描述

本 PR 修复 audio_service 鸿蒙适配中 OHOS 播控中心播放/暂停单键在状态切换后失灵 的问题,并补齐 example 的上一曲/下一曲控制、修复收藏按钮点击丢失。核心原因是:OHOS 播控中心(AVSession)的播放/暂停是同一个按钮,由系统按其自身对播放状态的判断发送 play 或 pause 命令;而 Dart 侧惯用的 if (playing) MediaControl.pause else MediaControl.play 条件式 controls 每次广播只声明其中一个,导致每次播放切换后另一个命令未注册、单键点击无响应(真机复现:每次切换注册列表 del play / add pause,点击 no-op)。

  • 鸿蒙原生层(audio_service/ohos/src/main/ets/AudioService.ets,addCommandForAction):
    • play / pause 命令成对注册:声明 MediaControl.play 或 MediaControl.pause 任一个,即同时向 AVSession 注册 play 和 pause,保证播控中心单键在任意状态下都可响应(playPause(9) 原有的双命令映射保持不变)。
  • 鸿蒙原生层(audio_service/ohos/src/main/ets/AudioServicePlugin.ets,onToggleFavorite):
    • toggleFavorite 自定义动作的 extras 由原始 assetId 字符串改为 Map({ assetId: <id> },对齐 Android 端 bundleToMap 行为):Dart 侧 customAction 回调将 extras 强转为 Map<String, dynamic>?,字符串载荷会转换失败,收藏点击被静默丢弃,从未到达 handler。
  • example(audio_service/example/lib/main.dart):
    • controls 声明新增 MediaControl.skipToPrevious / skipToNext(4 → 6 个控制按钮),handler 实现 skipToNext / skipToPrevious(单曲 demo:seek 回起点后重新播放),使上一曲/下一曲按钮在 OHOS 播控中心/通知/锁屏上渲染并可响应;androidCompactActionIndices 重排为 [1, 2, 3] 保持紧凑栏布局不变(OHOS 忽略该字段);
    • stop() 改为 await 后 seek 回起点,使下次 play() 从头播放。
  • 变更日志(CHANGELOG.OpenHarmony.md):新增 3 条 Fixed 条目(均引用 !68);版本小节由 0.18.18-ohos-1.0.1-2026.10 更名为 0.18.18-ohos-1.0.1-2026.11。

影响范围:4 个文件(+58 / −11);插件 Dart 公开 API(lib/)无变更;鸿蒙原生层仅调整 AVSession 命令注册策略与 extras 载荷格式。同一修复已移植 br_3.41_dev(!67)。

变更类型

fix(缺陷修复;example 新增 skip 控制为缺陷修复配套的演示完善)

破坏性更新

本 PR 不包含破坏性变更。
插件 Dart 层 API 签名与调用方式不变(lib/ 无改动)。鸿蒙原生层两处行为修复均是把原先失效的控件恢复可用(play/pause 单键失灵、收藏点击丢失),不存在依赖旧坏行为的应用;play/pause 成对注册与 !64 引入的按需注册(controls ∪ systemActions 决定命令集)机制保持一致,只会使命令集更可用,不会隐藏任何已配置的功能。

关联Issue

https://gitcode.com/CPF-Flutter/fluttertpc_audio_service/issues/74

相关历史 issue:[40](鸿蒙播控中心强制显示收藏/循环按钮,已由 !64 修复关闭)——本次 toggleFavorite extras 修复是其收藏按钮链路的收尾:按钮已按 systemActions 显示,但点击此前无法到达 Dart handler。

测试情况

测试步骤:

  1. 播控中心手工验证(OHOS 真机,example main 入口):
    • 播放↔暂停多次切换后,播控中心/通知/锁屏的播放暂停单键始终可响应(修复前现象已在设备上复现并记录于代码注释:每次切换注册列表 del play / add pause,点击 no-op);
    • 上一曲/下一曲按钮在 OHOS 媒体面板正常渲染、可点击(单曲 demo 重新从头播放);
    • 收藏按钮点击后 toggleFavorite 正常到达 Dart 侧 customAction handler(extras 已为 Map)。
    • 注:example 含多个独立入口(main.dart / example_playlist.dart 等各自带 main()),真机验证时需确认运行的是本次修改的 main 入口(可经 hilog 的 syncEventListeners: desired=[...] 日志反推设备上实际运行的 handler)。
  2. 编译与回归:example/ohos 下 hvigor assembleHar 编译通过;插件 Dart 层(lib/)无改动,存量 Dart 单元测试(audio_service/test/unit)与 example 冒烟(integration_test/app_test.dart)不受本次改动影响。
  3. 单元测试说明:本分支(br_v0.18.18_ohos_dev)无独立 ETS ohosTest 套件;本次修复的用例化覆盖(conditionalPlayControlEnablesBothPlayAndPause 等)在 br_3.41_dev 的姊妹 MR !67 中补充。

Changelog 条目

## 0.18.18-ohos-1.0.1-2026.11

### Fixed
* Register the AVSession `play` and `pause` commands as a pair: the media-control-center play/pause is a single button that sends either command according to its own view of the playback state, so declaring only `MediaControl.play` or `MediaControl.pause` (the canonical `if (playing) pause else play` conditional) left the button dead after each play toggle. Declaring either one now keeps both valid. ([!68](https://gitcode.com/CPF-Flutter/fluttertpc_audio_service/merge_requests/68))
* Declare `MediaControl.skipToPrevious` / `skipToNext` in the example's controls and implement `skipToNext` / `skipToPrevious` in the example handler, so the skip buttons render and respond on the OHOS media surfaces. ([!68](https://gitcode.com/CPF-Flutter/fluttertpc_audio_service/merge_requests/68))
* Send the `toggleFavorite` custom action's extras as a map (matching Android's `bundleToMap`) instead of the raw asset id string: the Dart-side `customAction` callback casts extras to `Map<String, dynamic>?`, so the string payload failed the cast and favorite taps never reached the handler. ([!68](https://gitcode.com/CPF-Flutter/fluttertpc_audio_service/merge_requests/68))

单元测试:
图片.png
图片.png

集成测试:
图片.png

likedislike
Pull Request has been successfully merged by openharmony_ci.
(Thank you for xileilei's contribution.)
xileileixileilei
23 hours ago Associated issue: fix(ohos): register AVSession play/pause commands as a pair; add skip controls to example
cpf-managercpf-managerCollaborators
23 hours ago Added label: AI检视中
xileilei
xileilei
23 hours ago Comments:

start build

likedislike
openharmony_ci
openharmony_ciCollaborators
23 hours ago Comments:

感谢提交 Pull Requests!
Thanks for submitting a pull request.

likedislike
openharmony_ciopenharmony_ciCollaborators
23 hours ago Added label: dco检查成功
openharmony_ci
openharmony_ciCollaborators
23 hours ago Comments:

首次触发
门禁构建开始,包含静态检查、代码编译和测试【ftpc_all_3.35_dev测试, ftpc_all_3.35_dev编译】,预计在60分钟内完成,门禁结果会同步发送到注册邮箱。您可以通过如下链接跟踪门禁进展:http://dcp.openharmony.cn/workbench/cicd/detail/6b06a32464650f998bc04154/runlist

likedislike
xileileixileilei
23 hours ago Modify description of pull request
8 event messages are collapsed here。 See more
openharmony_ciopenharmony_ciCollaborators
22 hours ago Test passed
openharmony_ci
openharmony_ciCollaborators
22 hours ago Comments:

代码门禁通过
您可以通过如下链接查看门禁报告:http://dcp.openharmony.cn/workbench/cicd/detail/6b06a32464650f998bc04154/runlist

静态检查:

# check type result report
1 codeCheck pass >>>

编译测试:
# Device build result test result package
1 ftpc_all_3.35_dev success success >>>

likedislike
hezySignhezySignCollaborators
4 hours ago Set hezySign as approver
hezySignhezySignCollaborators
4 hours ago Approval passed
zhouzhixiongzhouzhixiongCollaborators
56 minutes ago Set zzx89757 as approver
zhouzhixiongzhouzhixiongCollaborators
56 minutes ago Approval passed
cpf-managercpf-managerCollaborators
56 minutes ago Added label: AI检视复核中
cpf-managercpf-managerCollaborators
56 minutes ago Reset test status
cpf-manager
cpf-managerCollaborators
54 minutes ago Comments:

【AI-Review复核报告】

结论: ✅ 通过

复核基线: 147a2f02 | 意见总数: 0 | 有效处置: 0 | 无效处置: 0

全部意见均已正确处置(已修复 / 有据非问题 / Issue跟踪),测试通过。

likedislike
cpf-managercpf-managerCollaborators
54 minutes ago Test passed
cpf-managercpf-managerCollaborators
54 minutes ago Removed label: AI检视复核中
cpf-managercpf-managerCollaborators
54 minutes ago Added label: AI检视复核通过
openharmony_ciopenharmony_ciCollaborators
54 minutes ago Closed associated issue
openharmony_ciopenharmony_ciCollaborators
54 minutes ago Merged pull request, merge node SHA: 9ff0f5d521ebf2255d8480fd87d00cb858ad1882