"""Offline Android system-service verifier for Ch 138: AudioManager."""
import json
import xml.etree.ElementTree as ET
VERSION = "CPython 3.12.14 offline Android graphics/animation/media/service verifier (Android SDK/emulator absent)"
AUDIO_LAYOUT_XML = """<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button android:id="@+id/btn_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="播放" />
<Button android:id="@+id/btn_stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:enabled="false" android:text="停止" />
<Button android:id="@+id/btn_higher" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="调高音量" />
<Button android:id="@+id/btn_lower" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="调低音量" />
<Button android:id="@+id/btn_quite" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="静音" />
</LinearLayout>
"""
class AudioManagerSim:
MAX_VOLUMES = {
"STREAM_VOICE_CALL": 5,
"STREAM_SYSTEM": 7,
"STREAM_RING": 7,
"STREAM_MUSIC": 15,
"STREAM_ALARM": 7,
"STREAM_NOTIFICATION": 7,
"STREAM_DTMF": 15,
}
def __init__(self) -> None:
self.volumes = {k: v // 2 for k, v in self.MAX_VOLUMES.items()}
self.muted = {k: False for k in self.MAX_VOLUMES}
self.ringer_mode = "RINGER_MODE_NORMAL"
self.audio_mode = "MODE_NORMAL"
def adjust_stream_volume(self, stream: str, direction: str, flags: int = 0) -> None:
max_v = self.MAX_VOLUMES[stream]
if direction == "ADJUST_RAISE":
self.volumes[stream] = min(max_v, self.volumes[stream] + 1)
self.muted[stream] = False
elif direction == "ADJUST_LOWER":
self.volumes[stream] = max(0, self.volumes[stream] - 1)
if stream == "STREAM_RING" and self.volumes[stream] == 0:
self.ringer_mode = "RINGER_MODE_VIBRATE"
elif direction == "ADJUST_MUTE":
self.muted[stream] = True
elif direction == "ADJUST_UNMUTE":
self.muted[stream] = False
else:
raise ValueError(direction)
def set_ringer_mode(self, mode: str) -> None:
if mode not in ("RINGER_MODE_NORMAL", "RINGER_MODE_SILENT", "RINGER_MODE_VIBRATE"):
raise ValueError(mode)
self.ringer_mode = mode
if mode in ("RINGER_MODE_SILENT", "RINGER_MODE_VIBRATE"):
self.volumes["STREAM_RING"] = 0
def set_mode(self, mode: str) -> None:
if mode not in ("MODE_NORMAL", "MODE_RINGTONE", "MODE_IN_CALL", "MODE_IN_COMMUNICATION"):
raise ValueError(mode)
self.audio_mode = mode
def main() -> None:
# Case 1: 7 audio stream types & per-stream max volumes (STREAM_RING=7 vs STREAM_MUSIC=15)
am = AudioManagerSim()
c1_actual = {
"stream_count": len(am.MAX_VOLUMES),
"ring_max": am.MAX_VOLUMES["STREAM_RING"],
"music_max": am.MAX_VOLUMES["STREAM_MUSIC"],
"initial_music_vol": am.volumes["STREAM_MUSIC"],
}
c1_expected = {
"stream_count": 7,
"ring_max": 7,
"music_max": 15,
"initial_music_vol": 7,
}
# Case 2: adjustStreamVolume RAISE/LOWER + API 23+ ADJUST_MUTE/ADJUST_UNMUTE (2nd parameter direction!)
am.adjust_stream_volume("STREAM_MUSIC", "ADJUST_RAISE", flags=1)
vol_after_raise = am.volumes["STREAM_MUSIC"]
am.adjust_stream_volume("STREAM_MUSIC", "ADJUST_LOWER", flags=4)
am.adjust_stream_volume("STREAM_MUSIC", "ADJUST_LOWER", flags=4)
vol_after_2_lowers = am.volumes["STREAM_MUSIC"]
am.adjust_stream_volume("STREAM_MUSIC", "ADJUST_MUTE", flags=1)
muted_state = am.muted["STREAM_MUSIC"]
am.adjust_stream_volume("STREAM_MUSIC", "ADJUST_UNMUTE", flags=1)
unmuted_state = am.muted["STREAM_MUSIC"]
c2_actual = {
"vol_after_raise": vol_after_raise,
"vol_after_2_lowers": vol_after_2_lowers,
"muted_then_unmuted": [muted_state, unmuted_state],
"mute_param_position": "second_parameter_direction",
}
c2_expected = {
"vol_after_raise": 8,
"vol_after_2_lowers": 6,
"muted_then_unmuted": [True, False],
"mute_param_position": "second_parameter_direction",
}
# Case 3: RingerMode transitions & lowering STREAM_RING to 0 switching to RINGER_MODE_VIBRATE
am2 = AudioManagerSim()
am2.set_mode("MODE_IN_COMMUNICATION")
for _ in range(5):
am2.adjust_stream_volume("STREAM_RING", "ADJUST_LOWER")
ringer_at_zero = am2.ringer_mode
am2.set_ringer_mode("RINGER_MODE_SILENT")
c3_actual = {
"audio_mode": am2.audio_mode,
"ring_vol_at_zero": am2.volumes["STREAM_RING"],
"ringer_mode_at_zero": ringer_at_zero,
"ringer_mode_after_silent": am2.ringer_mode,
}
c3_expected = {
"audio_mode": "MODE_IN_COMMUNICATION",
"ring_vol_at_zero": 0,
"ringer_mode_at_zero": "RINGER_MODE_VIBRATE",
"ringer_mode_after_silent": "RINGER_MODE_SILENT",
}
# Case 4: Demo layout 5 buttons & mute flag toggle + VIBRATE permission for FLAG_VIBRATE
root = ET.fromstring(AUDIO_LAYOUT_XML)
flag = 1
toggle_seq = []
for _ in range(2):
flag *= -1
toggle_seq.append("取消静音" if flag == -1 else "静音")
c4_actual = {
"button_count": len(root.findall("Button")),
"mute_toggle_labels": toggle_seq,
"vibrate_permission_xml": '<uses-permission android:name="android.permission.VIBRATE"/>',
}
c4_expected = {
"button_count": 5,
"mute_toggle_labels": ["取消静音", "静音"],
"vibrate_permission_xml": '<uses-permission android:name="android.permission.VIBRATE"/>',
}
cases = [
{
"name": "audiomanager_streams_and_max_volumes",
"summary": "核验 AudioManager 7 种声音流类型及不同流(STREAM_RING=7 vs STREAM_MUSIC=15)的最大音量差异",
"expected": c1_expected,
"actual": c1_actual,
"passed": c1_actual == c1_expected,
},
{
"name": "audiomanager_adjust_and_api23_mute",
"summary": "核验 adjustStreamVolume 增减音量与 API 23+ 通过第二参数 direction 传入 ADJUST_MUTE/UNMUTE 静音控制",
"expected": c2_expected,
"actual": c2_actual,
"passed": c2_actual == c2_expected,
},
{
"name": "audiomanager_modes_and_ringer_transition",
"summary": "核验 AudioMode 切换、STREAM_RING 降至 0 自动转震动模式及 RINGER_MODE_SILENT 行为",
"expected": c3_expected,
"actual": c3_actual,
"passed": c3_actual == c3_expected,
},
{
"name": "audiomanager_demo_layout_and_vibrate_perm",
"summary": "核验 5 按钮播放调节布局、静音标志翻转及使用 FLAG_VIBRATE 所需的 VIBRATE 权限声明",
"expected": c4_expected,
"actual": c4_actual,
"passed": c4_actual == c4_expected,
},
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/android-tutorial-audiomanager-lab/explore-tutorial-audiomanager.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-tutorial-audiomanager.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1842.zip
课程关联工程包(登记关联156章) · 含当前章节或实例 · mbskycom202612a1829.zip