Playback 放音秒挂问题排查
问题现象
执行 playback() 应用播放音频文件时,音频文件还未播放就立即挂断。
示例:
1
| <action application="playback" data="/usr/local/freeswitch/sounds/11.mp3"/>
|
预期行为: 播放 11 分钟的音频文件
实际行为: 秒挂,音频未播放
日志分析
关键日志片段
1 2 3
| EXECUTE [depth=0] sofia/internal/13900000002@test.example.internal playback(/usr/local/freeswitch/sounds/11.mp3) [NOTICE] switch_core_state_machine.c:382 sofia/internal/13900000002@test.example.internal has executed the last dialplan instruction, hanging up. [NOTICE] switch_core_state_machine.c:384 Hangup sofia/internal/13900000002@test.example.internal [CS_EXECUTE] [NORMAL_CLEARING]
|
日志解读
- EXECUTE playback: playback 指令被执行
- last dialplan instruction: 已经执行了最后一条 dialplan 指令
- hanging up: FreeSWITCH 自动挂断
- CS_EXECUTE: 通道处于执行状态
- NORMAL_CLEARING: 正常清除挂断原因
问题根源
核心原因: playback 是 dialplan 的最后一条指令,FreeSWITCH 执行完毕后自动挂断,不等待音频播放完成。
常见原因
1. MP3 模块未加载
现象: 播放 MP3 文件时直接挂断
原因: mod_shout 模块未加载(MP3 解码需要此模块)
检查方法:
1 2 3 4 5
| fs_cli -x "module_exists mod_shout"
fs_cli -x "module_exists mod_shout" | grep true
|
预期输出:
1 2
| true # 模块已加载 false # 模块未加载
|
2. 音频文件不存在
现象: playback 执行后立即挂断
检查方法:
1 2 3 4 5
| ls -l /usr/local/freeswitch/sounds/11.mp3
fs_cli -x "file_exists /usr/local/freeswitch/sounds/11.mp3"
|
常见错误路径:
3. Dialplan 配置错误
现象: playback 作为最后一条指令,立即挂断
错误示例:
1 2 3 4 5 6 7
| <extension name="play_audio"> <condition field="destination_number" expression="^1234$"> <action application="answer"/> <action application="playback" data="/sounds/11.mp3"/> </condition> </extension>
|
4. 音频格式不支持
支持的音频格式:
- WAV: 原生支持(推荐)
- MP3: 需要
mod_shout
- OGG: 需要
mod_shout
- 其它: 需要对应编解码器
解决方案
方案 1: 添加等待指令(推荐)
在 playback 后添加 sleep 或 hash 指令:
1 2 3 4 5 6 7 8
| <extension name="play_audio"> <condition field="destination_number" expression="^1234$"> <action application="answer"/> <action application="playback" data="/usr/local/freeswitch/sounds/11.mp3"/> <action application="sleep" data="10000"/> </condition> </extension>
|
更好的方案 - 使用变量:
1 2
| <action application="set" data="playback_terminators=none"/> <action application="playback" data="/usr/local/freeswitch/sounds/11.mp3"/>
|
方案 2: 确保模块加载
检查并加载 mod_shout:
1 2 3 4 5 6 7 8 9
| fs_cli -x "module_exists mod_shout"
fs_cli -x "load mod_shout"
<load module="mod_shout"/>
|
验证:
1 2
| fs_cli -x "reloadxml" fs_cli -x "module_exists mod_shout"
|
方案 3: 使用 WAV 格式(推荐)
WAV 格式原生支持,无需额外模块:
1 2 3 4
|
<action application="playback" data="/usr/local/freeswitch/sounds/11.wav"/>
|
优势:
完整排查步骤
步骤 1: 检查音频文件
1 2 3 4 5 6 7 8 9 10
| ls -lh /usr/local/freeswitch/sounds/11.mp3
chmod 644 /usr/local/freeswitch/sounds/11.mp3 chown freeswitch:freeswitch /usr/local/freeswitch/sounds/11.mp3
file /usr/local/freeswitch/sounds/11.mp3
|
步骤 2: 检查模块加载
1 2 3 4 5 6 7 8
| fs_cli -x "module_exists mod_shout"
fs_cli -x "load mod_shout"
fs_cli -x "show modules"
|
步骤 3: 检查 Dialplan 配置
1 2 3 4 5
| fs_cli -x "dialplan_extension show <extension_name>"
cat /usr/local/freeswitch/conf/dialplan/*.xml | grep -A 10 "playback"
|
步骤 4: 测试播放
使用 fs_cli 直接测试:
1 2 3 4 5 6
| fs_cli -x "originate user/1000 &playback(/usr/local/freeswitch/sounds/11.mp3)"
fs_cli -x "uuid_park <uuid>" fs_cli -x "uuid_broadcast <uuid> /usr/local/freeswitch/sounds/11.mp3"
|
步骤 5: 查看详细日志
1 2 3 4 5 6 7 8
| fs_cli -x "console loglevel 7"
tail -f /usr/local/freeswitch/log/freeswitch.log | grep playback
grep "b7d9c89c-b8d6-448c-81e4-5e20dcd9e84f" /usr/local/freeswitch/log/freeswitch.log.1
|
最佳实践
1. 音频文件管理
推荐目录结构:
1 2 3 4 5 6 7 8 9 10 11
| /usr/local/freeswitch/sounds/ ├── custom/ │ ├── prompts/ │ │ ├── welcome.wav │ │ └── goodbye.wav │ └── music/ │ └── hold_music.wav └── en/ └── us/ └── callie/ └── ...
|
文件命名规范:
- 使用小写字母和下划线
- 包含描述性名称
- 添加采样率信息(如 8000、16000)
2. 音频格式选择
推荐: WAV 8kHz 单声道(电话音质)
常用命令速查
模块管理
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| fs_cli -x "module_exists mod_shout"
fs_cli -x "load mod_shout"
fs_cli -x "unload mod_shout"
fs_cli -x "reload mod_shout"
fs_cli -x "show modules"
|
文件检查
1 2 3 4 5 6 7 8 9
| fs_cli -x "file_exists /path/to/file.wav"
fs_cli -x "originate user/1000 &playback(/path/to/file.wav)"
file /usr/local/freeswitch/sounds/file.wav ffmpeg -i /usr/local/freeswitch/sounds/file.mp3
|
日志调试
1 2 3 4 5 6 7 8
| fs_cli -x "console loglevel 7"
grep "<uuid>" /usr/local/freeswitch/log/freeswitch.log
tail -f /usr/local/freeswitch/log/freeswitch.log | grep -E "(playback|EXECUTE)"
|
相关问题
Q1: 为什么 WAV 文件可以播放,MP3 不行?
答: WAV 是 FreeSWITCH 原生支持的格式,MP3 需要额外的 mod_shout 模块。
Q2: 如何确保音频播放完成后再挂断?
答:
- 使用
sleep 指令等待足够时间
- 或者检测音频播放状态
- 或者在 playback 后添加其他应用(如
hangup)
Q3: 音频文件路径应该使用绝对还是相对路径?
答: 推荐使用绝对路径,或设置 sound_prefix 变量指定基础路径。
Q4: 播放长音频(如 30 分钟)怎么办?
答:
- 考虑分割成多个短文件
- 使用循环播放
- 或使用流式播放(mod_shout)
Q5: 如何在播放期间允许用户打断?
答: 设置 playback_terminators 变量:
1
| <action application="set" data="playback_terminators=*#"/>
|
参考资源
相关文档