From 6494d78253a9912f9f9e1fecbeaaca7a2e8fdff5 Mon Sep 17 00:00:00 2001 From: zenord Date: Sun, 11 Oct 2026 19:01:28 +0800 Subject: [PATCH] fix: audio frame size, rotation rebuild, background disconnect reason - AudioCapture: short reads buffered and concatenated, only send when exactly 640B - AndroidManifest: configChanges prevents rotation from recreating Activity - onStop shows explicit disconnect reason instead of silent disconnect - Remote debug log now works (HTTPS endpoint fixed) Version 0.3.0 --- app/build.gradle | 4 ++-- app/src/main/AndroidManifest.xml | 5 ++++- .../java/com/gorios/lanvoice/MainActivity.kt | 5 ++++- .../com/gorios/lanvoice/audio/AudioCapture.kt | 19 +++++++++++++++---- .../com/gorios/lanvoice/audio/VoiceSession.kt | 18 +++++++++++++++--- 5 files changed, 40 insertions(+), 11 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 4c25b2d..6d1819f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -45,8 +45,8 @@ android { applicationId 'com.gorios.lanvoice' minSdk 26 targetSdk 34 - versionCode 3 - versionName '0.2.7' + versionCode 4 + versionName '0.3.0' ndk { abiFilters 'arm64-v8a' } } compileOptions { sourceCompatibility JavaVersion.VERSION_17; targetCompatibility JavaVersion.VERSION_17 } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index fccfbf0..d6d9376 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -12,7 +12,10 @@ android:supportsRtl="true" android:usesCleartextTraffic="false" android:networkSecurityConfig="@xml/network_security_config"> - + diff --git a/app/src/main/java/com/gorios/lanvoice/MainActivity.kt b/app/src/main/java/com/gorios/lanvoice/MainActivity.kt index d7be5ec..918ed50 100644 --- a/app/src/main/java/com/gorios/lanvoice/MainActivity.kt +++ b/app/src/main/java/com/gorios/lanvoice/MainActivity.kt @@ -133,7 +133,10 @@ class MainActivity : ComponentActivity() { pendingCamera = false expressions.stop() if (!permissionInFlight) pendingStart = false - session.background(keepConnectionForPermission = permissionInFlight) + session.background( + keepConnectionForPermission = permissionInFlight, + showDisconnectReason = !permissionInFlight, + ) super.onStop() } diff --git a/app/src/main/java/com/gorios/lanvoice/audio/AudioCapture.kt b/app/src/main/java/com/gorios/lanvoice/audio/AudioCapture.kt index a446519..04c5193 100644 --- a/app/src/main/java/com/gorios/lanvoice/audio/AudioCapture.kt +++ b/app/src/main/java/com/gorios/lanvoice/audio/AudioCapture.kt @@ -59,11 +59,11 @@ class AudioCapture { onDiagnostic("AudioRecord initialization started") Process.setThreadPriority(Process.THREAD_PRIORITY_AUDIO) val minimum = AudioRecord.getMinBufferSize( - 16_000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT + SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT ) check(minimum > 0) { "16k mono PCM16 recording is unavailable" } val device = AudioRecord( - MediaRecorder.AudioSource.VOICE_COMMUNICATION, 16_000, + MediaRecorder.AudioSource.VOICE_COMMUNICATION, SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, maxOf(minimum * 2, FRAME_BYTES * 2) ) @@ -89,19 +89,25 @@ class AudioCapture { val frame = ByteArray(FRAME_BYTES) var filled = 0 while (run.active) { - val read = device.read(frame, filled, frame.size - filled, AudioRecord.READ_BLOCKING) + // READ_BLOCKING can still return a short read. Keep it until a full + // 320-sample frame is ready; never pad or send a partial frame. + val remaining = FRAME_BYTES - filled + val read = device.read(frame, filled, remaining, AudioRecord.READ_BLOCKING) if (!run.active) break check(read >= 0) { "AudioRecord read failed: $read" } + check(read <= remaining) { "AudioRecord read exceeded requested bytes" } if (read == 0) { Thread.sleep(2) continue } filled += read if (filled == frame.size) { + // The callback may queue this asynchronously; do not reuse its bytes. onFrame(frame.copyOf()) filled = 0 } } + // Any unfinished frame belongs to this run and is discarded on stop. } catch (error: Exception) { if (run.active) { onDiagnostic("AudioRecord $stage failed: ${error.javaClass.simpleName}") @@ -121,5 +127,10 @@ class AudioCapture { } } - companion object { const val FRAME_BYTES = 640 } + companion object { + private const val SAMPLE_RATE = 16_000 + private const val FRAME_DURATION_MS = 20 + private const val BYTES_PER_SAMPLE = 2 + const val FRAME_BYTES = SAMPLE_RATE * FRAME_DURATION_MS / 1_000 * BYTES_PER_SAMPLE // 640B + } } diff --git a/app/src/main/java/com/gorios/lanvoice/audio/VoiceSession.kt b/app/src/main/java/com/gorios/lanvoice/audio/VoiceSession.kt index 2d57ab1..c8ce782 100644 --- a/app/src/main/java/com/gorios/lanvoice/audio/VoiceSession.kt +++ b/app/src/main/java/com/gorios/lanvoice/audio/VoiceSession.kt @@ -81,7 +81,7 @@ class VoiceSession( else DebugLogUpload.log("connect", "foreground reused active connection") } - fun background(keepConnectionForPermission: Boolean = false) { + fun background(keepConnectionForPermission: Boolean = false, showDisconnectReason: Boolean = false) { checkMainThread() foreground = false stopVoice(sendControls = false) @@ -95,7 +95,13 @@ class VoiceSession( backgroundTimeout = null if (!foreground && !released) dialogue.disconnect("permission_background_timeout") }.also { main.postDelayed(it, 30_000L) } - } else dialogue.disconnect("session_background") + } else { + if (showDisconnectReason) { + update(state.copy(guidance = BACKGROUND_DISCONNECT_GUIDANCE)) + onConnectionLog(BACKGROUND_DISCONNECT_GUIDANCE) + } + dialogue.disconnect("session_background") + } } fun release() { @@ -157,7 +163,12 @@ class VoiceSession( blockedTurn = null sequence = 0 update(state.copy(connection = connection, voiceActive = false, phase = VoicePhase.IDLE)) - } else update(state.copy(connection = connection, guidance = null)) + } else update(state.copy( + connection = connection, + // onStop runs while the screen is hidden. Keep the reason visible on + // return, including after reconnect; startVoice clears it explicitly. + guidance = state.guidance?.takeIf { it == BACKGROUND_DISCONNECT_GUIDANCE }, + )) } private fun startCapture() { @@ -314,6 +325,7 @@ class VoiceSession( } companion object { + private const val BACKGROUND_DISCONNECT_GUIDANCE = "App 退到后台,连接已断开" private const val TAG = "LanVoice" private const val UINT32_MASK = 0xffff_ffffL }