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]

日志解读

  1. EXECUTE playback: playback 指令被执行
  2. last dialplan instruction: 已经执行了最后一条 dialplan 指令
  3. hanging up: FreeSWITCH 自动挂断
  4. CS_EXECUTE: 通道处于执行状态
  5. 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 中检查
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"

# 如果返回 false,加载模块
fs_cli -x "load mod_shout"

# 设置开机自动加载
# 编辑 autoload_configs/modules.conf.xml
<load module="mod_shout"/>

验证:

1
2
fs_cli -x "reloadxml"
fs_cli -x "module_exists mod_shout"

方案 3: 使用 WAV 格式(推荐)

WAV 格式原生支持,无需额外模块:

1
2
3
4
<!-- 转换 MP3 到 WAV -->
<!-- ffmpeg -i 11.mp3 -ar 8000 -ac 1 11.wav -->

<action application="playback" data="/usr/local/freeswitch/sounds/11.wav"/>

优势:

  • 无需 mod_shout
  • 性能更好
  • 兼容性更高

完整排查步骤

步骤 1: 检查音频文件

1
2
3
4
5
6
7
8
9
10
# 1. 确认文件存在
ls -lh /usr/local/freeswitch/sounds/11.mp3

# 2. 检查文件权限
chmod 644 /usr/local/freeswitch/sounds/11.mp3
chown freeswitch:freeswitch /usr/local/freeswitch/sounds/11.mp3

# 3. 验证文件完整性
file /usr/local/freeswitch/sounds/11.mp3
# 输出: Audio file with ID3 version 2.3.0, contains: MPEG ADTS, layer III

步骤 2: 检查模块加载

1
2
3
4
5
6
7
8
# 检查 mod_shout
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
# 查看当前 dialplan
fs_cli -x "dialplan_extension show <extension_name>"

# 或查看 XML 配置
cat /usr/local/freeswitch/conf/dialplan/*.xml | grep -A 10 "playback"

步骤 4: 测试播放

使用 fs_cli 直接测试:

1
2
3
4
5
6
# originate 测试
fs_cli -x "originate user/1000 &playback(/usr/local/freeswitch/sounds/11.mp3)"

# 或使用 uuid_park
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) 无 中等 大 ⭐⭐⭐⭐⭐
WAV (16kHz) 无 高 很大 ⭐⭐⭐⭐
MP3 (128kbps) mod_shout 中等 小 ⭐⭐⭐
OGG mod_shout 高 小 ⭐⭐

推荐: 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=*#"/>

参考资源


相关文档


© 2018–2026 小周周 · 使用 Stellar 构建

站内搜索

没有找到内容!