119 lines
3.8 KiB
Python
119 lines
3.8 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
音频播放测试脚本
|
||
用于测试树莓派的音频播放功能
|
||
"""
|
||
|
||
import subprocess
|
||
import time
|
||
import sys
|
||
import os
|
||
|
||
def test_audio_playback():
|
||
"""测试音频播放功能"""
|
||
print("=== 音频播放测试 ===")
|
||
|
||
# 检查音频设备
|
||
print("\n1. 检查音频设备...")
|
||
try:
|
||
result = subprocess.run(['aplay', '-l'], capture_output=True, text=True)
|
||
if result.returncode == 0:
|
||
print("音频设备列表:")
|
||
print(result.stdout)
|
||
else:
|
||
print("错误: 无法获取音频设备列表")
|
||
return False
|
||
except FileNotFoundError:
|
||
print("错误: aplay 命令未找到,请安装 alsa-utils")
|
||
return False
|
||
|
||
# 测试播放系统声音
|
||
print("\n2. 测试播放系统提示音...")
|
||
try:
|
||
# 使用系统内置的测试声音
|
||
result = subprocess.run(['speaker-test', '-t', 'sine', '-f', '440', '-l', '1'],
|
||
capture_output=True, text=True, timeout=5)
|
||
if result.returncode == 0:
|
||
print("✓ 系统提示音播放成功")
|
||
else:
|
||
print("✗ 系统提示音播放失败")
|
||
return False
|
||
except (subprocess.TimeoutExpired, FileNotFoundError):
|
||
print("提示: speaker-test 测试跳过,尝试直接播放音频文件")
|
||
|
||
# 创建测试音频文件并播放
|
||
print("\n3. 创建并播放测试音频文件...")
|
||
test_audio_file = "/tmp/test_audio.wav"
|
||
|
||
# 使用sox生成测试音频(如果可用)
|
||
if os.path.exists("/usr/bin/sox"):
|
||
try:
|
||
subprocess.run(['sox', '-n', '-r', '44100', '-c', '2', test_audio_file,
|
||
'synth', '3', 'sine', '440'], check=True)
|
||
print("✓ 测试音频文件创建成功")
|
||
except (subprocess.CalledProcessError, FileNotFoundError):
|
||
print("无法创建测试音频文件,跳过文件播放测试")
|
||
return True
|
||
else:
|
||
print("sox 未安装,跳过文件播放测试")
|
||
return True
|
||
|
||
# 播放测试音频文件
|
||
try:
|
||
result = subprocess.run(['aplay', test_audio_file], capture_output=True, text=True)
|
||
if result.returncode == 0:
|
||
print("✓ 音频文件播放成功")
|
||
return True
|
||
else:
|
||
print("✗ 音频文件播放失败")
|
||
print(f"错误信息: {result.stderr}")
|
||
return False
|
||
except FileNotFoundError:
|
||
print("错误: aplay 命令未找到")
|
||
return False
|
||
finally:
|
||
# 清理测试文件
|
||
if os.path.exists(test_audio_file):
|
||
os.remove(test_audio_file)
|
||
|
||
def check_volume():
|
||
"""检查并设置音量"""
|
||
print("\n4. 检查音量设置...")
|
||
try:
|
||
result = subprocess.run(['amixer', 'sget', 'Master'], capture_output=True, text=True)
|
||
if result.returncode == 0:
|
||
print("当前音量设置:")
|
||
print(result.stdout)
|
||
|
||
# 设置音量到80%
|
||
subprocess.run(['amixer', 'sset', 'Master', '80%'], check=True)
|
||
print("✓ 音量已设置为80%")
|
||
return True
|
||
else:
|
||
print("无法获取音量信息")
|
||
return False
|
||
except (subprocess.CalledProcessError, FileNotFoundError):
|
||
print("amixer 命令未找到或执行失败")
|
||
return False
|
||
|
||
if __name__ == "__main__":
|
||
print("树莓派音频播放功能测试")
|
||
print("=" * 40)
|
||
|
||
success = True
|
||
|
||
# 检查音量
|
||
if not check_volume():
|
||
success = False
|
||
|
||
# 测试音频播放
|
||
if not test_audio_playback():
|
||
success = False
|
||
|
||
print("\n" + "=" * 40)
|
||
if success:
|
||
print("✓ 所有音频播放测试通过")
|
||
sys.exit(0)
|
||
else:
|
||
print("✗ 部分音频播放测试失败")
|
||
sys.exit(1) |