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

fix(ohos): register AVSession play/pause commands as a pair; add skip controls to example

变更描述

本 PR 对 audio_service 鸿蒙适配 dev 分支修复 OHOS 播控中心两类播控缺陷(播放/暂停按钮失灵、stop 后播放器无法重新显示),并为 example 补齐上一首/下一首按钮支持,属于鸿蒙原生层缺陷修复与示例增强,不涉及 Dart 层 API 改动。

问题背景一(播放/暂停按钮失灵):OHOS 播控中心的播放/暂停是同一个按钮,由系统按其自身维护的播放状态决定下发 play 还是 pause 命令;而 Dart 侧惯用写法 if (playing) MediaControl.pause else MediaControl.play 每次状态广播只会声明二者之一。原生层按声明集合差量注册 AVSession 命令时(!66 引入的按需注册),每次播放状态切换都会出现 del play / add pause(或反向)翻转,导致播控中心按钮在翻转后的另一状态下点击无响应(真机复现:taps no-op)。

问题背景二(stop 后播放器无法重新显示):插件层 stopService 原实现直接丢弃 AudioService 实例(置 null 并 release()),下次 setState 失败(NO_SERVICE),播放器永远无法重新出现在通知/播控中心,与 Android AudioService.stop() 后可再次播放的行为不一致。

变更内容(5 个文件,+145 / -19):

  • 鸿蒙原生层(audio_service_ohos/ohos/src/main/ets/AudioService.ets):
    • addCommandForAction 中 play / pause 命令强制成对注册——只要声明任一播放控制,play 与 pause 同时保持有效,播控中心播放/暂停按钮在任意播放状态下均可响应;
    • 新增 AudioService.stop()(对齐 Android AudioService.stop() 语义):deactivate + destroy AVSession 并停止后台任务,媒体卡片从通知/播控中心消失;元数据/队列/回调/命令配置保留,下次 play 重建会话;
    • createSession 新增 activateNow 参数:会话重建时先写 metadata → playbackState → 再 activate,否则 stop 后重建的会话不出现在通知/播控中心;
    • idle 状态只令当前媒体卡片消失(此时重建会话会闪空播放器),实际重建推迟到下一个非 idle 状态;OHOS 上对同一会话重复 activate 不会在通知/播控中心恢复显示,故引入 forceRecreateSession 标记强制销毁重建,并在重建后补发 setAVMetadata / setAVQueueItems;
  • 鸿蒙插件层(audio_service_ohos/ohos/src/main/ets/AudioServicePlugin.ets):stopService 改为调用 audioService.stop(),不再丢弃服务实例,修复 stop 后再次播放报 NO_SERVICE、播放器无法重新显示的问题;
  • 示例工程(audio_service_ohos/example/lib/main.dart):
    • controls 增加声明 MediaControl.skipToPrevious / skipToNext,使播控中心/锁屏渲染上一首/下一首按钮;
    • 实现 skipToNext() / skipToPrevious()(示例为单曲播放列表,跳回当前曲目开头重新播放);
    • stop() 补充 seek 回零:just_audio 的 stop() 会保留进度,seek 到起点使下次 play() 从头播放;
    • androidCompactActionIndices 相应调整以保持紧凑布局(OHOS 忽略该字段,对每个声明控件均显示按钮,仅影响 Android 侧布局);
    • 文件头与行内文档注释同步:4 个 media actions → 6 个;
  • 示例构建配置(audio_service_ohos/example/ohos/build-profile.json5):example 的 ohos 工程新增 audio_service_ohos 源码模块(srcPath: ../../ohos),example 直接以源码方式编译插件原生层,避免依赖构建时烤制的 har 产物造成「真机安装的并非当前提交代码」的验证失真;
  • 变更日志(CHANGELOG.OpenHarmony.md):待发布版本条目定稿为 0.18.15-ohos-1.0.0 - 2026.11(原 !66 草稿为 0.18.15-ohos-1.0.1 - 2026.10),新增两条 Fixed 条目并引用本 MR(!69)。

影响范围:5 个文件(+145 / -19);Dart 公开 API 无变更(lib/ 无改动);仅影响 OHOS 播控中心/锁屏的 AVSession 命令注册、会话生命周期与 example 演示行为。

变更类型

fix(缺陷修复;commit 按 fix(ohos) 规范标记,附带 example 示例增强)

破坏性更新

本 PR 不包含破坏性更新。

  • Dart 层 API 签名与调用方式保持不变(lib/ 无改动);
  • 原生层 play/pause 成对注册只是扩大命令注册集合:对按惯用写法(条件声明 play/pause)使用的应用,效果是“按钮状态切换后失灵”修复为“始终可用”,不存在行为回退面;
  • stop 语义对齐 Android AudioService.stop():stop 时仍销毁会话并停止后台任务(媒体卡片消失的行为不变),变化仅是“再次 play 能重新显示”——此前该路径因 NO_SERVICE 直接不可用;
  • !66 引入的收藏/循环按钮需显式配置 systemActions 的约束保持不变。

关联 Issue

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

测试情况

测试步骤:

  1. 在 audio_service_ohos/example 目录执行 flutter test,存量 widget 测试(示例 App 骨架渲染,直接依赖本次修改的 main.dart)全部通过(1/1,2026-11-26 实测)。
  2. 真机(SN 4ZB0225710017457,Flutter 3.22.4-ohos)编译安装 example,复现修复前症状:hilog 观察到每次播放/暂停切换出现 del play / add pause 差量翻转,翻转后播控中心播放/暂停按钮点击无响应。
  3. 修复后真机验证:播放中/暂停中任意状态下,播控中心与锁屏的播放/暂停单按钮均可正常切换播放状态。
  4. 真机验证 stop 生命周期:stop 后媒体卡片从通知/播控中心消失;再次 play 时 AVSession 重建(metadata → playbackState → activate 顺序)并重新显示播放器,播控按钮可用。
  5. 验证上一首/下一首按钮在播控中心/锁屏正常渲染,点击触发 skipToNext / skipToPrevious(示例单曲列表,跳回开头重播)。
  6. 回归说明:本 PR 未改动 Dart 层(lib/),example/integration_test/ 集成测试(3 个测试文件、8 例,覆盖 Dart 层 API 面,含 stop() 调用路径)所覆盖的行为不受影响;该套件此前真机运行 8/8 通过。

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

集成测试:
图片.png

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

start build

likedislike
openharmony_ci
openharmony_ciCollaborators
22 hours ago Comments:

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

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

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

likedislike
cpf-managercpf-managerCollaborators
22 hours ago Removed label: AI检视中
cpf-managercpf-managerCollaborators
22 hours ago Added label: AI已检视
openharmony_ciopenharmony_ciCollaborators
21 hours ago Added label: 编译成功
openharmony_ciopenharmony_ciCollaborators
21 hours ago Added label: 静态检查成功
openharmony_ciopenharmony_ciCollaborators
21 hours ago Added label: 冒烟测试成功
openharmony_ciopenharmony_ciCollaborators
21 hours ago Test passed
openharmony_ci
openharmony_ciCollaborators
21 hours ago Comments:

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

静态检查:

# check type result report
1 codeCheck pass >>>

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

likedislike
wuleileiwuleilei
18 hours ago Reset test status, including openharmony_ci
wuleileiwuleilei
18 hours ago Force-pushed 1 commits:f835231d-fix(ohos): register AVSession play/pause commands as a pair; add skip controls to example
cpf-managercpf-managerCollaborators
18 hours ago Removed label: AI已检视
cpf-managercpf-managerCollaborators
18 hours ago Added label: AI检视中
xileilei
xileilei
18 hours ago Comments:

start build

likedislike
openharmony_ciopenharmony_ciCollaborators
18 hours ago Removed label: 冒烟测试成功
openharmony_ciopenharmony_ciCollaborators
18 hours ago Removed label: 编译成功
openharmony_ciopenharmony_ciCollaborators
18 hours ago Removed label: 静态检查成功
openharmony_ci
openharmony_ciCollaborators
18 hours ago Comments:

代码有更新,重置PR验证状态

likedislike
openharmony_ci
openharmony_ciCollaborators
18 hours ago Comments:

本地或库上代码有更新,全量重新构建,重置所有关联PR的验证状态
门禁构建开始,包含静态检查、代码编译和测试【ftpc_all_3.22_dev编译, ftpc_all_3.22_dev测试】,预计在60分钟内完成,门禁结果会同步发送到注册邮箱。您可以通过如下链接跟踪门禁进展:http://dcp.openharmony.cn/workbench/cicd/detail/6b06def864650f998bd762f7/runlist

likedislike
openharmony_ci
openharmony_ciCollaborators
18 hours ago Comments:

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

likedislike
cpf-managercpf-managerCollaborators
18 hours ago Removed label: AI检视中
cpf-managercpf-managerCollaborators
18 hours ago Added label: AI已检视
openharmony_ciopenharmony_ciCollaborators
18 hours ago Added label: 编译成功
openharmony_ciopenharmony_ciCollaborators
18 hours ago Added label: 静态检查成功
openharmony_ciopenharmony_ciCollaborators
18 hours ago Added label: 冒烟测试成功
openharmony_ciopenharmony_ciCollaborators
18 hours ago Test passed
openharmony_ci
openharmony_ciCollaborators
18 hours ago Comments:

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

静态检查:

# check type result report
1 codeCheck pass >>>

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

likedislike
xileileixileilei
6 hours ago Modify description of pull request
5 event messages are collapsed here。 See more
cpf-managercpf-managerCollaborators
38 minutes ago Reset test status
cpf-manager
cpf-managerCollaborators
35 minutes ago Comments:

【AI-Review复核报告】

结论: ✅ 通过

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

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

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