feat(android): 新增「启动时切到模拟输出」开关与输出路由诊断
个别 Allwinner 机型(T95MAX / Android 12)三种输出 AUDIO_CODEC / AUDIO_SPDIF / AUDIO_HDMI,只有 CODEC(AV/3.5mm)有声音,但每次插着 HDMI 开机都会被重置成 HDMI。 实测结论(已写进 README): - HAL 只在初始化时读 vendor.audio.output.active;运行期 AudioManager.setParameters(raw_data_output / mediasw.sft.rawdata / audio_output_channel / devices_out_active / routing)与 setPreferredDevice 全被忽略; - 只有「设属性 + 重启 vendor.audio-hal」才切到 open card=0(需 root,App 没有 su)。 因此新增开关仅作“尽量尝试 + 打印当前输出”: - AudioOutput.applyCodec() 用反射调 AudioManager.setParameters 下发厂商参数, 并在不支持时打日志「音频输出切换未生效 当前仍是 XXX」便于现场排查; - 开关默认关闭(多数机型本来就走模拟输出,不需要动它)。 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
parent
b45c8a29f4
commit
aecb85d55b
@ -93,6 +93,16 @@
|
|||||||
- 播放用系统 `MediaPlayer` 读临时拼接的 WAV(这台 rk3288 的 `AudioTrack` 初始化会失败),
|
- 播放用系统 `MediaPlayer` 读临时拼接的 WAV(这台 rk3288 的 `AudioTrack` 初始化会失败),
|
||||||
在 `station-speech` 独立线程完成,不阻塞扫码上报。
|
在 `station-speech` 独立线程完成,不阻塞扫码上报。
|
||||||
- 同一串条码 2.5 秒内不重复播报(连续扫描的重复帧);换了条码立即打断上一句播最新的。
|
- 同一串条码 2.5 秒内不重复播报(连续扫描的重复帧);换了条码立即打断上一句播最新的。
|
||||||
|
- **输出路由(踩过的坑)**:个别 Allwinner 机型(实测 T95MAX / Android 12)有三种输出
|
||||||
|
`AUDIO_CODEC / AUDIO_SPDIF / AUDIO_HDMI`,只有 CODEC(AV/3.5mm)有声音,而**每次插着 HDMI
|
||||||
|
开机都会被重置成 `AUDIO_HDMI`**(属性 `vendor.audio.output.active`)。
|
||||||
|
该 HAL 只在**初始化时**读这个属性:运行期 `AudioManager.setParameters`
|
||||||
|
(`raw_data_output` / `mediasw.sft.rawdata` / `audio_output_channel` / `devices_out_active` / `routing`)
|
||||||
|
和 `MediaPlayer/AudioTrack.setPreferredDevice` **全部被忽略**;只有「设属性 + 重启
|
||||||
|
`vendor.audio-hal`」才会切到 `open card=0`(需要 root,普通应用没有 su)。
|
||||||
|
所以这一层只能靠现场:系统设置里选一次 CODEC(会丢)、HDMI 音频分离器、音箱接电视、
|
||||||
|
或换机型。应用侧保留「启动时把音频输出切到模拟输出」开关(默认关)与当前输出日志,
|
||||||
|
在支持该参数的机型上有效,在这类机器上只会打印「切换未生效」便于排查。
|
||||||
**上报不受影响**:重复条码仍然逐条发送。
|
**上报不受影响**:重复条码仍然逐条发送。
|
||||||
- 语音内容:条码末尾 4 个字母/数字(标点跳过)+ 「开始打包」。
|
- 语音内容:条码末尾 4 个字母/数字(标点跳过)+ 「开始打包」。
|
||||||
- 启动时 `BarcodeSpeaker.maximizeVolume` 把媒体音量设为最大(实测 `11 -> 15`)。
|
- 启动时 `BarcodeSpeaker.maximizeVolume` 把媒体音量设为最大(实测 `11 -> 15`)。
|
||||||
|
|||||||
@ -23,6 +23,7 @@ import androidx.appcompat.widget.SwitchCompat;
|
|||||||
import com.gztbyc.scancenter.core.StationIdentity;
|
import com.gztbyc.scancenter.core.StationIdentity;
|
||||||
import com.gztbyc.scancenter.core.StationSettings;
|
import com.gztbyc.scancenter.core.StationSettings;
|
||||||
import com.gztbyc.scancenter.core.service.StationService;
|
import com.gztbyc.scancenter.core.service.StationService;
|
||||||
|
import com.gztbyc.scancenter.core.speech.AudioOutput;
|
||||||
|
|
||||||
import static com.gztbyc.scancenter.util.L.e;
|
import static com.gztbyc.scancenter.util.L.e;
|
||||||
import static com.gztbyc.scancenter.util.L.i;
|
import static com.gztbyc.scancenter.util.L.i;
|
||||||
@ -47,6 +48,7 @@ public class SettingsActivity extends AppCompatActivity {
|
|||||||
private SwitchCompat serviceSwitch;
|
private SwitchCompat serviceSwitch;
|
||||||
private SwitchCompat discoverySwitch;
|
private SwitchCompat discoverySwitch;
|
||||||
private SwitchCompat speakerSwitch;
|
private SwitchCompat speakerSwitch;
|
||||||
|
private SwitchCompat forceCodecSwitch;
|
||||||
private TextView statusView;
|
private TextView statusView;
|
||||||
private TextView homeStatusView;
|
private TextView homeStatusView;
|
||||||
private Button setHomeButton;
|
private Button setHomeButton;
|
||||||
@ -63,6 +65,7 @@ public class SettingsActivity extends AppCompatActivity {
|
|||||||
serviceSwitch = findViewById(R.id.sw_service_enabled);
|
serviceSwitch = findViewById(R.id.sw_service_enabled);
|
||||||
discoverySwitch = findViewById(R.id.sw_discovery_enabled);
|
discoverySwitch = findViewById(R.id.sw_discovery_enabled);
|
||||||
speakerSwitch = findViewById(R.id.sw_speaker_output);
|
speakerSwitch = findViewById(R.id.sw_speaker_output);
|
||||||
|
forceCodecSwitch = findViewById(R.id.sw_force_codec);
|
||||||
statusView = findViewById(R.id.tv_settings_status);
|
statusView = findViewById(R.id.tv_settings_status);
|
||||||
homeStatusView = findViewById(R.id.tv_home_status);
|
homeStatusView = findViewById(R.id.tv_home_status);
|
||||||
setHomeButton = findViewById(R.id.btn_set_home);
|
setHomeButton = findViewById(R.id.btn_set_home);
|
||||||
@ -74,6 +77,7 @@ public class SettingsActivity extends AppCompatActivity {
|
|||||||
serviceSwitch.setChecked(StationSettings.serviceEnabled(this));
|
serviceSwitch.setChecked(StationSettings.serviceEnabled(this));
|
||||||
discoverySwitch.setChecked(StationSettings.discoveryEnabled(this));
|
discoverySwitch.setChecked(StationSettings.discoveryEnabled(this));
|
||||||
speakerSwitch.setChecked(StationSettings.speakerOutput(this));
|
speakerSwitch.setChecked(StationSettings.speakerOutput(this));
|
||||||
|
forceCodecSwitch.setChecked(StationSettings.forceCodecOutput(this));
|
||||||
|
|
||||||
Button save = findViewById(R.id.btn_save);
|
Button save = findViewById(R.id.btn_save);
|
||||||
save.setOnClickListener(new View.OnClickListener() {
|
save.setOnClickListener(new View.OnClickListener() {
|
||||||
@ -333,8 +337,13 @@ public class SettingsActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
StationSettings.save(this, name, servicePort, discoveryPort,
|
StationSettings.save(this, name, servicePort, discoveryPort,
|
||||||
serviceSwitch.isChecked(), discoverySwitch.isChecked(), speakerSwitch.isChecked());
|
serviceSwitch.isChecked(), discoverySwitch.isChecked(), speakerSwitch.isChecked(),
|
||||||
|
forceCodecSwitch.isChecked());
|
||||||
StationService.instance().restart();
|
StationService.instance().restart();
|
||||||
|
// 勾上就立刻生效一次,不用等下一次重启
|
||||||
|
if (forceCodecSwitch.isChecked()) {
|
||||||
|
AudioOutput.applyCodec(this);
|
||||||
|
}
|
||||||
renderStatus();
|
renderStatus();
|
||||||
|
|
||||||
String error = currentError();
|
String error = currentError();
|
||||||
|
|||||||
@ -20,6 +20,7 @@ public final class StationSettings {
|
|||||||
private static final String KEY_SERVICE_ENABLED = "service_enabled";
|
private static final String KEY_SERVICE_ENABLED = "service_enabled";
|
||||||
private static final String KEY_DISCOVERY_ENABLED = "discovery_enabled";
|
private static final String KEY_DISCOVERY_ENABLED = "discovery_enabled";
|
||||||
private static final String KEY_SPEAKER_OUTPUT = "speaker_output";
|
private static final String KEY_SPEAKER_OUTPUT = "speaker_output";
|
||||||
|
private static final String KEY_FORCE_CODEC_OUTPUT = "force_codec_output";
|
||||||
|
|
||||||
/** 与桌面端 {@code ScanProtocol.TcpPort} 保持一致 */
|
/** 与桌面端 {@code ScanProtocol.TcpPort} 保持一致 */
|
||||||
public static final int DEFAULT_SERVICE_PORT = 8087;
|
public static final int DEFAULT_SERVICE_PORT = 8087;
|
||||||
@ -94,8 +95,20 @@ public final class StationSettings {
|
|||||||
return prefs(context).getBoolean(KEY_SPEAKER_OUTPUT, false);
|
return prefs(context).getBoolean(KEY_SPEAKER_OUTPUT, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 启动时是否把音频输出强制切到**模拟输出(AUDIO_CODEC)**。
|
||||||
|
*
|
||||||
|
* <p>默认关闭:多数机型(rk3288 等)本来就走模拟输出,不需要动。个别 Allwinner 机型
|
||||||
|
* (实测 T95MAX / Android 12)会在**每次重启**后把输出重置成 SPDIF/HDMI,
|
||||||
|
* 导致接 AV/3.5mm 的音箱没声音——这类机器打开本开关,应用启动时自动切回去。
|
||||||
|
*/
|
||||||
|
public static boolean forceCodecOutput(Context context) {
|
||||||
|
return prefs(context).getBoolean(KEY_FORCE_CODEC_OUTPUT, false);
|
||||||
|
}
|
||||||
|
|
||||||
public static void save(Context context, String name, int servicePort, int discoveryPort,
|
public static void save(Context context, String name, int servicePort, int discoveryPort,
|
||||||
boolean serviceEnabled, boolean discoveryEnabled, boolean speakerOutput) {
|
boolean serviceEnabled, boolean discoveryEnabled, boolean speakerOutput,
|
||||||
|
boolean forceCodecOutput) {
|
||||||
prefs(context).edit()
|
prefs(context).edit()
|
||||||
.putString(KEY_DEVICE_NAME, name == null ? "" : name.trim())
|
.putString(KEY_DEVICE_NAME, name == null ? "" : name.trim())
|
||||||
.putInt(KEY_SERVICE_PORT, servicePort)
|
.putInt(KEY_SERVICE_PORT, servicePort)
|
||||||
@ -103,6 +116,7 @@ public final class StationSettings {
|
|||||||
.putBoolean(KEY_SERVICE_ENABLED, serviceEnabled)
|
.putBoolean(KEY_SERVICE_ENABLED, serviceEnabled)
|
||||||
.putBoolean(KEY_DISCOVERY_ENABLED, discoveryEnabled)
|
.putBoolean(KEY_DISCOVERY_ENABLED, discoveryEnabled)
|
||||||
.putBoolean(KEY_SPEAKER_OUTPUT, speakerOutput)
|
.putBoolean(KEY_SPEAKER_OUTPUT, speakerOutput)
|
||||||
|
.putBoolean(KEY_FORCE_CODEC_OUTPUT, forceCodecOutput)
|
||||||
.commit();
|
.commit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,99 @@
|
|||||||
|
package com.gztbyc.scancenter.core.speech;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.media.AudioManager;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
|
import static com.gztbyc.scancenter.util.L.i;
|
||||||
|
import static com.gztbyc.scancenter.util.L.w;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 把音频输出强制切到**模拟输出(AUDIO_CODEC / AV / 3.5mm)**。
|
||||||
|
*
|
||||||
|
* <p>背景(实测 T95MAX / Allwinner Apollo,Android 12):系统有三种输出
|
||||||
|
* `AUDIO_CODEC / AUDIO_SPDIF / AUDIO_HDMI`,只有 CODEC 这一路接的喇叭/AV 有声音;
|
||||||
|
* 系统设置里改过一次,但**重启后又被固件重置**(HAL 启动时按 `vendor.audio.output.active`
|
||||||
|
* 决定,默认给了 SPDIF/HDMI)。所以由应用每次启动时重新下发一次。
|
||||||
|
*
|
||||||
|
* <p>下发方式取自厂商 `AudioManagerEx.setAudioDeviceActive()`(反编译所得):
|
||||||
|
* 走 {@link AudioManager#setParameters(String)},参数键是 HAL 的
|
||||||
|
* `devices_out_active` / `raw_data_output` / `mediasw.sft.rawdata`。
|
||||||
|
* 这些都是隐藏 API,用反射调用;失败只记日志,不影响播报。
|
||||||
|
*/
|
||||||
|
public final class AudioOutput {
|
||||||
|
|
||||||
|
private AudioOutput() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 当前 HAL 认为的活跃输出(读属性,失败返回空串) */
|
||||||
|
public static String currentOutput() {
|
||||||
|
try {
|
||||||
|
Class<?> properties = Class.forName("android.os.SystemProperties");
|
||||||
|
Method get = properties.getMethod("get", String.class);
|
||||||
|
Object value = get.invoke(null, "vendor.audio.output.active");
|
||||||
|
return value == null ? "" : value.toString();
|
||||||
|
}
|
||||||
|
catch (Throwable ignored) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 切到模拟输出并返回是否成功下发。
|
||||||
|
*
|
||||||
|
* <p>顺序按厂商实现:先关 raw 直通(CODEC 走 PCM),再指定活跃输出设备。
|
||||||
|
*/
|
||||||
|
public static boolean applyCodec(Context context) {
|
||||||
|
AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
|
||||||
|
if (audio == null) {
|
||||||
|
w("拿不到 AudioManager,跳过输出切换");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Method setParameters;
|
||||||
|
try {
|
||||||
|
setParameters = AudioManager.class.getMethod("setParameters", String.class);
|
||||||
|
}
|
||||||
|
catch (Throwable failure) {
|
||||||
|
// 隐藏 API 被挡(Android 9+ 的非 SDK 接口限制)
|
||||||
|
w("AudioManager.setParameters 不可用", describe(failure));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean ok = true;
|
||||||
|
ok &= invoke(setParameters, audio, "raw_data_output=0");
|
||||||
|
ok &= invoke(setParameters, audio, "mediasw.sft.rawdata=PCM");
|
||||||
|
// 厂商 AudioManagerEx 真正用来驱动路由的就是这个键(AudioManager.setParameter 下发)
|
||||||
|
ok &= invoke(setParameters, audio, "audio_output_channel=AUDIO_CODEC");
|
||||||
|
ok &= invoke(setParameters, audio, "devices_out_active=AUDIO_CODEC");
|
||||||
|
// sunxi HAL 的 routing 是 AudioDevice 位掩码:0x2 = AUDIO_DEVICE_OUT_SPEAKER
|
||||||
|
ok &= invoke(setParameters, audio, "routing=2");
|
||||||
|
|
||||||
|
String current = currentOutput();
|
||||||
|
if (current.isEmpty() || "AUDIO_CODEC".equals(current)) {
|
||||||
|
i("音频输出已切到模拟输出", current.isEmpty() ? "属性不可读" : current);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
w("音频输出切换未生效", "当前仍是", current);
|
||||||
|
}
|
||||||
|
return ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean invoke(Method setParameters, AudioManager audio, String value) {
|
||||||
|
try {
|
||||||
|
setParameters.invoke(audio, value);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (Throwable failure) {
|
||||||
|
w("下发音频参数失败", value, describe(failure));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String describe(Throwable failure) {
|
||||||
|
Throwable cause = failure.getCause() == null ? failure : failure.getCause();
|
||||||
|
String message = cause.getMessage();
|
||||||
|
return cause.getClass().getSimpleName() + (message == null ? "" : ": " + message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -136,6 +136,11 @@ public final class BarcodeSpeaker {
|
|||||||
cleanupCache();
|
cleanupCache();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// 个别机型重启后会把输出重置成 SPDIF/HDMI(AV/3.5mm 就没声音了),这里在每次
|
||||||
|
// 启动时按设置切回模拟输出;多数机型不需要,开关默认关。
|
||||||
|
if (StationSettings.forceCodecOutput(this.context)) {
|
||||||
|
AudioOutput.applyCodec(this.context);
|
||||||
|
}
|
||||||
maximizeVolume(this.context);
|
maximizeVolume(this.context);
|
||||||
scheduleWelcome();
|
scheduleWelcome();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -151,6 +151,15 @@
|
|||||||
android:textColor="@color/text_primary"
|
android:textColor="@color/text_primary"
|
||||||
android:textSize="16sp" />
|
android:textSize="16sp" />
|
||||||
|
|
||||||
|
<androidx.appcompat.widget.SwitchCompat
|
||||||
|
android:id="@+id/sw_force_codec"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="8dp"
|
||||||
|
android:text="@string/settings_force_codec"
|
||||||
|
android:textColor="@color/text_primary"
|
||||||
|
android:textSize="16sp" />
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
|
|||||||
@ -48,6 +48,7 @@
|
|||||||
<string name="settings_service_enabled">启用扫码服务</string>
|
<string name="settings_service_enabled">启用扫码服务</string>
|
||||||
<string name="settings_discovery_enabled">允许被桌面端发现</string>
|
<string name="settings_discovery_enabled">允许被桌面端发现</string>
|
||||||
<string name="settings_speaker_output">播报走本机扬声器(AV / 3.5mm)</string>
|
<string name="settings_speaker_output">播报走本机扬声器(AV / 3.5mm)</string>
|
||||||
|
<string name="settings_force_codec">启动时把音频输出切到模拟输出(AUDIO_CODEC)</string>
|
||||||
<string name="settings_note">桌面端会在局域网内自动发现本机,并连接上面的扫码服务端口读取 USB 扫码枪结果。\n默认:TCP 8087 / UDP 48899。修改后请与桌面端约定保持一致。</string>
|
<string name="settings_note">桌面端会在局域网内自动发现本机,并连接上面的扫码服务端口读取 USB 扫码枪结果。\n默认:TCP 8087 / UDP 48899。修改后请与桌面端约定保持一致。</string>
|
||||||
<string name="settings_section_status">当前状态</string>
|
<string name="settings_section_status">当前状态</string>
|
||||||
<string name="settings_section_home">开机 / 桌面</string>
|
<string name="settings_section_home">开机 / 桌面</string>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user