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
CODEBLOCK0
Installation
CODEBLOCK1
Requirements:
- -
python3 - Python 3 - INLINECODE1 - Part of ffmpeg, for audio duration detection
- INLINECODE2 - Python Sonos library
Core Function
CODEBLOCK2
Parameters
| Parameter | Type | Default | Description |
|---|
| INLINECODE3 | str | required | Filename (if using media_dir) or full path to audio file |
| INLINECODE4 |
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
CODEBLOCK3
Usage Examples
Simple (file in default media directory)
CODEBLOCK4
With custom media directory
CODEBLOCK5
Full path (no media_dir)
CODEBLOCK6
Environment Variables
Configure the HTTP server for streaming to Sonos:
| Variable | Default | Description |
|---|
| INLINECODE6 | auto-detected | LAN IP address (auto-detected) |
| INLINECODE7 |
8888 | HTTP server port |
CODEBLOCK7
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 - INLINECODE11 - Line-In stream
- INLINECODE12 - TV/HDMI (Sonos Home Theater)
- INLINECODE13 - Vanished device
- INLINECODE14 - Bluetooth
Soundboard Example
CODEBLOCK8
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 - INLINECODE19 - This documentation
Sonos Announce
在Sonos扬声器上播放音频文件,并智能恢复播放状态。
使用场景
- - 用户想在Sonos上播放通知
- 音效板效果(气喇叭、鼓点等)
- 任何需要恢复之前状态的音频播放
此技能仅处理播放 - 音频生成(TTS、ElevenLabs等)是独立功能。
快速开始
python
import sys
import os
sys.path.insert(0, /path/to/sonos-announce)
from sonos_core import announce
播放音频并恢复之前状态
假设音频位于默认media_dir目录(~/.local/share/openclaw/media/outbound)
result = announce(my_audio.mp3)
安装
bash
pip install soco
依赖项:
- - python3 - Python 3
- ffprobe - ffmpeg的一部分,用于检测音频时长
- soco - Python Sonos库
核心函数
python
announce(audiofilepath, waitforaudio=True, media_dir=None)
参数
| 参数 | 类型 | 默认值 | 描述 |
|---|
| audiofilepath | str | 必填 | 文件名(使用mediadir时)或音频文件的完整路径 |
| waitfor_audio |
bool | True | 等待音频播放完毕再返回 |
| media_dir | str | None | 音频文件所在目录(HTTP服务器将从此目录提供文件) |
返回值
python
{
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
}
}
}
使用示例
简单用法(文件在默认媒体目录中)
python
from sonos_core import announce
从默认media_dir提供文件
result = announce(announcement.mp3)
自定义媒体目录
python
from sonos_core import announce
音频文件的完整路径
result = announce(
my_audio.mp3,
media_dir=/home/user/audio/announcements
)
完整路径(不使用media_dir)
python
from sonos_core import announce
使用文件所在目录作为media_dir
result = announce(/full/path/to/audio.mp3)
环境变量
配置用于流式传输到Sonos的HTTP服务器:
| 变量 | 默认值 | 描述 |
|---|
| SONOSHTTPHOST | 自动检测 | 局域网IP地址(自动检测) |
| SONOSHTTPPORT |
8888 | HTTP服务器端口 |
bash
运行前设置(可选)
export SONOS
HTTPHOST=192.168.1.100 # 覆盖自动检测的IP
export SONOS
HTTPPORT=8888 # 覆盖端口
或在代码中导入前设置
import os
os.environ[SONOS
HTTPHOST] = 192.168.1.100
from sonos_core import announce
announce(audio.mp3)
支持平台
✅ 支持 | 完全支持 |
| Windows | ✅ 支持 | 使用taskkill和start /b |
模块会自动检测您的平台,并使用相应命令:
状态恢复
模块智能恢复之前的播放状态:
| 源类型 | 行为 |
|---|
| Spotify曲目 | 在精确位置恢复(跳转) |
| Spotify播放列表 |
在精确位置恢复(跳转) |
| Spotify电台 | 从头开始恢复(不跳转) |
| 网络电台 | 从头开始恢复(不跳转) |
| 线路输入 | 重新连接线路输入 |
| 电视/HDMI | 重新连接电视音频 |
| 蓝牙 | 重新连接蓝牙 |
| 已暂停内容 | 保持暂停状态 |
跳转行为
某些流媒体服务不支持跳转到特定位置:
- - 可跳转:Spotify曲目、Spotify播放列表、本地文件、队列项目
- 不可跳转:Spotify电台、TuneIn电台、Pandora、Tidal电台
模块会自动检测这些情况并进行相应处理。
外部输入检测
自动检测无法暂停的输入:
- - x-rincon:RINCON - 线路输入
- x-rincon-stream:RINCON - 线路输入流
- x-sonos-htastream: - 电视/HDMI(Sonos家庭影院)
- x-sonos-vanished: - 已消失的设备
- x-rincon-bt:* - 蓝牙
音效板示例
python
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):
播放音效。
if name in SOUNDS:
announce(SOUNDS[name])
else:
print(f未知音效:{name})
故障排除
| 问题 | 解决方案 |
|---|
| 未找到扬声器 | 确保与Sonos扬声器在同一网络 |
| 恢复失败 |
检查扬声器在通知前是否正在播放(而非暂停) |
| HTTP服务器失败 | 检查端口8888是否可用,或设置SONOS
HTTPPORT |
| 模块导入错误 | 运行:pip install soco |
| 时长检测失败 | 确保已安装ffprobe(ffmpeg的一部分) |
文件
- - sonos_core.py - 包含announce()函数的主模块
- SKILL.md - 本文档