From 6445ee94bf803bd9ab8091900441ca8dec5e29dd Mon Sep 17 00:00:00 2001 From: lxh2875931338 Date: Thu, 22 Oct 2026 22:57:34 +0800 Subject: [PATCH] =?UTF-8?q?fix(playback):=20=E6=92=AD=E6=94=BE=E4=B8=AD?= =?UTF-8?q?=E4=B8=8D=E5=86=8D=E5=90=91=E7=B3=BB=E7=BB=9F=E4=B8=8A=E6=8A=A5?= =?UTF-8?q?=20idle=EF=BC=8C=E4=BF=AE=E5=A4=8D=E6=92=AD=E6=94=BE=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E9=A1=B5=E6=98=BE=E7=A4=BA=E6=9C=AA=E5=9C=A8=E6=92=AD?= =?UTF-8?q?=E6=94=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit audio_service 把 idle 映射成 PlaybackState.STATE_NONE,之前用 duration>0 判断, 时长尚未解析出来时会一直上报 NONE。改为 playing || duration>0 即视为 ready。 设备验证(MEIZU 21 Pro / Android 16): 播放 -> state=PLAYING(3),暂停 -> state=PAUSED(2),不再出现 NONE(0)。 同时加固 device-check.ps1:确认进程真正退出后再抓冷启动日志,最多重试两次。 --- lib/services/audio_player_handler.dart | 6 +++++- tools/device-check.ps1 | 29 +++++++++++++++++--------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/lib/services/audio_player_handler.dart b/lib/services/audio_player_handler.dart index a993daf..339da65 100644 --- a/lib/services/audio_player_handler.dart +++ b/lib/services/audio_player_handler.dart @@ -116,9 +116,13 @@ class AudioPlayerHandler extends audio_service.BaseAudioHandler { try { final controls = _buildControls(playing); + // audio_service 侧映射:idle -> STATE_NONE,ready -> PLAYING/PAUSED。 + // 之前只按 duration>0 判断,直播流/时长未解析出来时会被系统判定为"未在播放" + // (播放设备选择页、部分车机与耳机都会读到这个状态)。 + final prepared = playing || duration.inMilliseconds > 0; final state = audio_service.PlaybackState( controls: controls, - processingState: duration.inMilliseconds > 0 + processingState: prepared ? audio_service.AudioProcessingState.ready : audio_service.AudioProcessingState.idle, playing: playing, diff --git a/tools/device-check.ps1 b/tools/device-check.ps1 index dc68991..96eb07f 100644 --- a/tools/device-check.ps1 +++ b/tools/device-check.ps1 @@ -72,22 +72,31 @@ Write-Ok '安装成功' # ---------- 冷启动 ---------- Write-Step '冷启动并抓取日志' -& adb -s $DeviceId shell am force-stop $pkg | Out-Null -& adb -s $DeviceId logcat -c -& adb -s $DeviceId shell monkey -p $pkg -c android.intent.category.LAUNCHER 1 2>&1 | Out-Null -Start-Sleep -Seconds $WaitSeconds -& adb -s $DeviceId logcat -d -v time > (Join-Path $root 'build\device-check-last.log') - $logPath = Join-Path $root 'build\device-check-last.log' -# 注意:PS 5.1 的 > 重定向写出的是 UTF-16LE(带 BOM),这里不要强制 utf8 -$log = Get-Content $logPath -$text = $log -join "`n" +$creates = 0 +# Flyme 有时不会立刻杀掉带前台服务的进程,导致抓不到启动日志:确认进程消失 + 最多重试两次 +for ($attempt = 1; $attempt -le 2; $attempt++) { + & adb -s $DeviceId shell am force-stop $pkg | Out-Null + $deadline = (Get-Date).AddSeconds(6) + while ((& adb -s $DeviceId shell pidof $pkg) -and (Get-Date) -lt $deadline) { + Start-Sleep -Milliseconds 300 + } + & adb -s $DeviceId logcat -c + & adb -s $DeviceId shell am start -W -n "$pkg/.MainActivity" 2>&1 | Out-Null + Start-Sleep -Seconds $WaitSeconds + & adb -s $DeviceId logcat -d -v time > $logPath + # 注意:PS 5.1 的 > 重定向写出的是 UTF-16LE(带 BOM),读取时不要强制 utf8 + $log = Get-Content $logPath + $text = $log -join "`n" + $creates = ([regex]::Matches($text, '\[PlaybackService\] 单例创建')).Count + if ($creates -ge 1) { break } + Write-Host " (第 $attempt 次没抓到冷启动日志,进程可能没退干净,重试)" -ForegroundColor DarkYellow +} # ---------- 判定 ---------- Write-Step '实例检查' $serviceIds = @([regex]::Matches($text, 'serviceId=(\d+)') | ForEach-Object { $_.Groups[1].Value } | Sort-Object -Unique) $players = @([regex]::Matches($text, 'Player 已创建.*?player=(\d+)') | ForEach-Object { $_.Groups[1].Value } | Sort-Object -Unique) -$creates = ([regex]::Matches($text, '\[PlaybackService\] 单例创建')).Count $failed = $false if ($creates -eq 1) { Write-Ok "PlaybackService 单例创建 x$creates" } else { Write-Bad "PlaybackService 单例创建 x$creates(应为 1)"; $failed = $true }