Sonos Announce:智能音频状态恢复 - Openclaw Skills

作者:互联网

2026-03-30

AI教程

什么是 Sonos Announce?

Sonos Announce 是 Openclaw Skills 生态系统中的一个专业集成,旨在解决智能环境中音频中断的常见问题。当触发通知或音效时,此技能不仅会播放声音,还会捕获每个扬声器的当前状态,包括特定的 URI、播放位置和音量。通知结束后,它会恢复播放,将 Spotify 曲目恢复到暂停时的精确秒数,或重新连接到电视和线路输入。

对于希望在不干扰用户听觉体验的情况下为其项目添加高保真音频反馈的开发人员来说,这项技能至关重要。通过利用此类 Openclaw Skills,您可以确保您的自动通知感觉像是家庭或办公环境的自然组成部分,而不是音乐的干扰中断。

下载入口:https://github.com/openclaw/skills/tree/main/skills/clawdianova/sonos-announce

安装与下载

1. ClawHub CLI

从源直接安装技能的最快方式。

npx clawhub@latest install sonos-announce

2. 手动安装

将技能文件夹复制到以下位置之一

全局模式 ~/.openclaw/skills/ 工作区 /skills/

优先级:工作区 > 本地 > 内置

3. 提示词安装

将此提示词复制到 OpenClaw 即可自动安装。

请帮我使用 Clawhub 安装 sonos-announce。如果尚未安装 Clawhub,请先安装(npm i -g clawhub)。

Sonos Announce 应用场景

  • 创建自定义智能家居语音通知,并能自动恢复音乐。
  • 使用 Openclaw Skills 构建用于团队警报或庆祝音效的办公室声卡。
  • 实施可临时覆盖电视或蓝牙音频的紧急广播系统。
  • 为智能门禁系统自动化个性化音频问候。
Sonos Announce 工作原理
  1. 该技能扫描本地网络以识别所有活跃的 Sonos 协调器。
  2. 它快照当前播放的元数据,确定来源是可定位的(如 Spotify)还是直播的(如广播/电视)。
  3. 动态启动本地 HTTP 服务器,以托管供 Sonos 系统访问的通知文件。
  4. 该技能暂停当前流并播放目标音频文件。
  5. 通过 ffprobe 时长检测确认播放完成后,该技能发送恢复命令,使每个扬声器返回到其原始状态。

Sonos Announce 配置指南

首先,确保您的系统中安装了必要的依赖项:

pip install soco
# 确保已安装 ffprobe (ffmpeg) 并将其添加到您的 PATH 中

要在项目中使用该技能:

from sonos_core import announce

# 播放通知并等待其结束
announce('path/to/your/audio.mp3')

Sonos Announce 数据架构与分类体系

该技能返回一个详细的对象,描述受影响的扬声器及其捕获的状态:

类型 描述
coordinators 整数 识别并管理的 Sonos 组数量。
states 对象 以扬声器 IP 为键的字典,包含播放元数据。
uri 字符串 原始媒体的特定资源标识符。
position 字符串 播放中断时的精确时间戳 (HH:MM:SS)。
was_playing 布尔值 在技能触发前扬声器是否正在活跃串流。
is_external 布尔值 如果来源是 HDMI 或线路输入等物理输入,则为 True。
name: sonos-announce
version: 1.0.2
description: Play audio on Sonos with intelligent state restoration - pauses streaming, skips Line-In/TV/Bluetooth, resumes everything.
metadata:
  {
    "openclaw":
      {
        "emoji": "??",
        "requires":
          {
            "bins": ["python3", "ffprobe"],
            "pip": ["soco"],
          },
      },
  }

Sonos Announce

Play audio files on Sonos speakers with intelligent state restoration.

When to use

  • User wants to play an announcement on Sonos
  • Soundboard effects (airhorn, rimshot, etc.)
  • Any audio playback that should resume previous state

This skill handles playback only - audio generation (TTS, ElevenLabs, etc.) is separate.

Quick Start

import sys
import os
sys.path.insert(0, '/path/to/sonos-announce')
from sonos_core import announce

# Play audio and restore previous state
# Assumes audio is in default media_dir (~/.local/share/openclaw/media/outbound)
result = announce('my_audio.mp3')

Installation

pip install soco

Requirements:

  • python3 - Python 3
  • ffprobe - Part of ffmpeg, for audio duration detection
  • soco - Python Sonos library

Core Function

announce(audio_file_path, wait_for_audio=True, media_dir=None)

Parameters

Parameter Type Default Description
audio_file_path str required Filename (if using media_dir) or full path to audio file
wait_for_audio bool True Wait for audio to finish playing before returning
media_dir str None Directory where audio file is located (HTTP server will serve from here)

Returns

{
  'coordinators': 2,
  'states': {
    '192.168.1.120': {
      'uri': 'x-sonos-spotify:spotify%3atrack%3a...',
      'position': '0:01:23',
      'queue_position': 5,
      'was_playing': True,
      'is_external': False,
      'transport_state': 'PLAYING',
      'speaker_name': 'Bedroom'
    }
  }
}

Usage Examples

Simple (file in default media directory)

from sonos_core import announce

# File served from default media_dir
result = announce('announcement.mp3')

With custom media directory

from sonos_core import announce

# Full path to audio file
result = announce(
    'my_audio.mp3', 
    media_dir='/home/user/audio/announcements'
)

Full path (no media_dir)

from sonos_core import announce

# Uses directory of file as media_dir
result = announce('/full/path/to/audio.mp3')

Environment Variables

Configure the HTTP server for streaming to Sonos:

Variable Default Description
SONOS_HTTP_HOST auto-detected LAN IP address (auto-detected)
SONOS_HTTP_PORT 8888 HTTP server port
# Set before running (optional)
export SONOS_HTTP_HOST=192.168.1.100  # Override auto-detected IP
export SONOS_HTTP_PORT=8888           # Override port

# Or set in code before importing
import os
os.environ['SONOS_HTTP_HOST'] = '192.168.1.100'

from sonos_core import announce
announce('audio.mp3')

Supported Platforms

Platform Status Notes
macOS ? Supported Full support
Linux ? Supported Full support
Windows ? Supported Uses taskkill and start /b

The module automatically detects your platform and uses appropriate commands for:

  • Killing the HTTP server
  • Starting the HTTP server in background

State Restoration

The module intelligently restores previous playback state:

Source Type Behavior
Spotify Track Resumed at exact position (seek)
Spotify Playlist Resumed at exact position (seek)
Spotify Radio Resumed from start (no seek)
Internet Radio Resumed from start (no seek)
Line-In Re-connected to Line-In input
TV/HDMI Re-connected to TV audio
Bluetooth Re-connected to Bluetooth
Paused content Left paused

Seeking Behavior

Some streaming services don't support seeking to a specific position:

  • Can seek: Spotify tracks, Spotify playlists, local files, queue items
  • Cannot seek: Spotify Radio, TuneIn radio, Pandora, Tidal radio

The module automatically detects these and handles accordingly.

External Input Detection

Automatically detects inputs that cannot be paused:

  • x-rincon:RINCON_* - Line-In
  • x-rincon-stream:RINCON_* - Line-In stream
  • x-sonos-htastream:* - TV/HDMI (Sonos Home Theater)
  • x-sonos-vanished:* - Vanished device
  • x-rincon-bt:* - Bluetooth

Soundboard Example

from sonos_core import announce

SOUNDS = {
    'airhorn': '/path/to/sounds/airhorn.mp3',
    'rimshot': '/path/to/sounds/rimshot.mp3',
    'victory': '/path/to/sounds/victory.mp3',
}

def play_sound(name):
    """Play a sound effect."""
    if name in SOUNDS:
        announce(SOUNDS[name])
    else:
        print(f"Unknown sound: {name}")

Troubleshooting

Issue Solution
No speakers found Ensure on same network as Sonos speakers
Resume not working Check speakers were playing (not paused) before announcement
HTTP server failed Check port 8888 is available, or set SONOS_HTTP_PORT
Module import error Run: pip install soco
Duration detection fails Ensure ffprobe is installed (part of ffmpeg)

Files

  • sonos_core.py - Main module with announce() function
  • SKILL.md - This documentation