88 lines
2.8 KiB
Python
88 lines
2.8 KiB
Python
import subprocess
|
||
import os
|
||
import logging
|
||
|
||
|
||
def push_audio_to_phone(device_id, local_audio_path, phone_audio_path): # 移除默认值
|
||
"""
|
||
将本地音频文件推送到手机指定路径
|
||
"""
|
||
# 检查本地文件是否存在
|
||
if not os.path.exists(local_audio_path):
|
||
logging.error(f"本地音频文件不存在: {local_audio_path}")
|
||
return False
|
||
|
||
# 执行ADB推送命令
|
||
cmd = ["adb", "-s", device_id, "push", local_audio_path, phone_audio_path]
|
||
try:
|
||
result = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
|
||
|
||
if result.returncode == 0:
|
||
logging.info(f"音频文件已推送到手机: {phone_audio_path}")
|
||
return True
|
||
else:
|
||
logging.error(f"推送音频文件失败: {result.stderr.strip()}")
|
||
return False
|
||
except subprocess.TimeoutExpired:
|
||
logging.error("推送音频文件超时")
|
||
return False
|
||
|
||
def play_audio_on_phone(device_id, phone_audio_path):
|
||
"""
|
||
控制手机播放指定路径的音频文件
|
||
"""
|
||
# 检查文件是否存在于手机
|
||
check_cmd = ["adb", "-s", device_id, "shell", "ls", phone_audio_path]
|
||
check_result = subprocess.run(check_cmd, capture_output=True, text=True)
|
||
|
||
if check_result.returncode != 0:
|
||
logging.error(f"音频文件在手机上不存在: {phone_audio_path}")
|
||
return False
|
||
|
||
# 使用am命令播放音频
|
||
cmd = [
|
||
"adb", "-s", device_id, "shell",
|
||
"am", "start",
|
||
"-a", "android.intent.action.VIEW",
|
||
"-t", "audio/*",
|
||
"-d", f"file://{phone_audio_path}"
|
||
]
|
||
|
||
try:
|
||
result = subprocess.run(cmd, capture_output=True, text=True, timeout=10)
|
||
|
||
if result.returncode == 0:
|
||
logging.info(f"已成功触发手机播放音频: {phone_audio_path}")
|
||
return True
|
||
else:
|
||
logging.error(f"播放音频失败: {result.stderr.strip()}")
|
||
return False
|
||
except subprocess.TimeoutExpired:
|
||
logging.error("播放音频命令超时")
|
||
return False
|
||
|
||
def play_system_alarm(device_id):
|
||
"""
|
||
播放系统内置的警报声
|
||
"""
|
||
try:
|
||
# 方法1:使用系统铃声URI
|
||
cmd = [
|
||
"adb", "-s", device_id, "shell",
|
||
"am", "start",
|
||
"-a", "android.intent.action.VIEW",
|
||
"-d", "content://settings/system/alarm_alert"
|
||
]
|
||
|
||
result = subprocess.run(cmd, capture_output=True, text=True, timeout=10)
|
||
|
||
if result.returncode == 0:
|
||
logging.info("成功触发系统警报声")
|
||
return True
|
||
else:
|
||
logging.error(f"播放系统警报失败: {result.stderr.strip()}")
|
||
return False
|
||
|
||
except Exception as e:
|
||
logging.error(f"播放系统警报时出错: {str(e)}")
|
||
return False |