play alert at the end + english comments

This commit is contained in:
pi
2025-10-27 12:42:45 +01:00
parent cb7e9ac8ac
commit 62f44a1654
2 changed files with 27 additions and 23 deletions

View File

@@ -37,6 +37,8 @@ class ListenActivity : Activity() {
volumeView.volumeHistory = bs.volumeHistory volumeView.volumeHistory = bs.volumeHistory
bs.onUpdate = { volumeView.postInvalidate() } bs.onUpdate = { volumeView.postInvalidate() }
bs.onError = { postErrorMessage() } bs.onError = { postErrorMessage() }
statusText.text = "Connected and listening..."
} }
override fun onServiceDisconnected(className: ComponentName) { override fun onServiceDisconnected(className: ComponentName) {
@@ -44,8 +46,8 @@ class ListenActivity : Activity() {
// unexpectedly disconnected -- that is, its process crashed. // unexpectedly disconnected -- that is, its process crashed.
// Because it is running in our same process, we should never // Because it is running in our same process, we should never
// see this happen. // see this happen.
Toast.makeText(this@ListenActivity, R.string.disconnected, Toast.makeText(this@ListenActivity, R.string.disconnected, Toast.LENGTH_SHORT).show()
Toast.LENGTH_SHORT).show() statusText.text = "Disconnected"
} }
} }
@@ -65,10 +67,10 @@ class ListenActivity : Activity() {
// applications). // applications).
if (bindService(intent, connection, BIND_AUTO_CREATE)) { if (bindService(intent, connection, BIND_AUTO_CREATE)) {
shouldUnbind = true shouldUnbind = true
Log.i(TAG, "Bound listen service") Log.i(TAG, "Bound to ListenService")
} else { } else {
Log.e(TAG, "Error: The requested service doesn't " + Log.e(TAG, "Error: Could not bind to ListenService.")
"exist, or this client isn't allowed access to it.") statusText.text = "Failed to bind service."
} }
} }
@@ -82,7 +84,7 @@ class ListenActivity : Activity() {
fun postErrorMessage() { fun postErrorMessage() {
statusText.post { statusText.post {
statusText.text = "Verbindung getrennt (3 Fehlversuche)" statusText.text = "Connection failed after 3 attempts."
} }
} }
@@ -90,7 +92,7 @@ class ListenActivity : Activity() {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_listen) setContentView(R.layout.activity_listen)
statusText = findViewById(R.id.textStatus) statusText = findViewById(R.id.textStatus)
statusText.text = "Starte Verbindung…" statusText.text = "Attempting to connect..."
volumeControlStream = AudioManager.STREAM_MUSIC volumeControlStream = AudioManager.STREAM_MUSIC
ensureServiceRunningAndBind(intent.extras) ensureServiceRunningAndBind(intent.extras)
} }

View File

@@ -65,9 +65,11 @@ class ListenService : Service() {
val name = it.getString("name") val name = it.getString("name")
childDeviceName = name childDeviceName = name
val n = buildNotification(name) val n = buildNotification(name)
val foregroundServiceType = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) val foregroundServiceType =
ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK else 0 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R)
ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK else 0
ServiceCompat.startForeground(this, ID, n, foregroundServiceType) ServiceCompat.startForeground(this, ID, n, foregroundServiceType)
val address = it.getString("address") val address = it.getString("address")
val port = it.getInt("port") val port = it.getInt("port")
doListenWithRetries(address, port) doListenWithRetries(address, port)
@@ -122,7 +124,7 @@ class ListenService : Service() {
} }
} }
/** Neuer Wrapper mit 3 automatischen Reconnect-Versuchen **/ /** New logic: automatically retry connection up to 3 times **/
private fun doListenWithRetries(address: String?, port: Int) { private fun doListenWithRetries(address: String?, port: Int) {
val lt = Thread { val lt = Thread {
var attempts = 0 var attempts = 0
@@ -130,29 +132,28 @@ class ListenService : Service() {
while (attempts < 3 && !connected && !Thread.currentThread().isInterrupted) { while (attempts < 3 && !connected && !Thread.currentThread().isInterrupted) {
try { try {
Log.i(TAG, "Verbindungsversuch ${attempts + 1} zu $address:$port ") Log.i(TAG, "Connection attempt ${attempts + 1} to $address:$port ...")
val socket = Socket(address, port) val socket = Socket(address, port)
socket.soTimeout = 30_000 socket.soTimeout = 30_000
connected = streamAudio(socket) connected = streamAudio(socket)
if (!connected) { if (!connected) {
Log.w(TAG, "Streaming fehlgeschlagen, Versuch ${attempts + 1}") Log.w(TAG, "Streaming failed, attempt ${attempts + 1}")
playAlert()
attempts++ attempts++
Thread.sleep(2000) Thread.sleep(2000)
} }
} catch (e: IOException) { } catch (e: IOException) {
attempts++ attempts++
Log.e(TAG, "Fehler beim Verbindungsaufbau (Versuch $attempts von 3)", e) Log.e(TAG, "Error while connecting (attempt $attempts of 3)", e)
if (attempts < 3) Thread.sleep(2000) if (attempts < 3) Thread.sleep(2000)
} }
} }
if (!connected) { if (!connected) {
Log.e(TAG, "Nach 3 Versuchen keine Verbindung möglich.") Log.e(TAG, "Failed to connect after 3 attempts.")
playAlert() playAlert()
onError?.invoke() onError?.invoke()
} else { } else {
Log.i(TAG, "Verbindung erfolgreich aufgebaut.") Log.i(TAG, "Connection established successfully.")
} }
} }
@@ -161,8 +162,9 @@ class ListenService : Service() {
} }
private fun streamAudio(socket: Socket): Boolean { private fun streamAudio(socket: Socket): Boolean {
Log.i(TAG, "Setting up stream") Log.i(TAG, "Starting audio stream")
val audioTrack = AudioTrack(AudioManager.STREAM_MUSIC, val audioTrack = AudioTrack(
AudioManager.STREAM_MUSIC,
frequency, frequency,
channelConfiguration, channelConfiguration,
audioEncoding, audioEncoding,
@@ -173,14 +175,14 @@ class ListenService : Service() {
try { try {
audioTrack.play() audioTrack.play()
} catch (e: IllegalStateException) { } catch (e: IllegalStateException) {
Log.e(TAG, "Failed to play streamed audio audio for other reason", e) Log.e(TAG, "Failed to start AudioTrack", e)
return false return false
} }
val inputStream = try { val inputStream = try {
socket.getInputStream() socket.getInputStream()
} catch (e: IOException) { } catch (e: IOException) {
Log.e(TAG, "Failed to read audio audio for socket", e) Log.e(TAG, "Failed to open input stream", e)
return false return false
} }
@@ -202,7 +204,7 @@ class ListenService : Service() {
} }
true true
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "Verbindungsfehler im Stream", e) Log.e(TAG, "Connection lost during streaming", e)
false false
} finally { } finally {
try { try {
@@ -216,11 +218,11 @@ class ListenService : Service() {
private fun playAlert() { private fun playAlert() {
val mp = MediaPlayer.create(this, R.raw.upward_beep_chromatic_fifths) val mp = MediaPlayer.create(this, R.raw.upward_beep_chromatic_fifths)
if (mp != null) { if (mp != null) {
Log.i(TAG, "Playing alert") Log.i(TAG, "Playing alert sound")
mp.setOnCompletionListener { it.release() } mp.setOnCompletionListener { it.release() }
mp.start() mp.start()
} else { } else {
Log.e(TAG, "Failed to play alert") Log.e(TAG, "Failed to play alert sound")
} }
} }