Use try-catch where appropriate

This commit is contained in:
Fabian Wiesel
2024-03-01 18:32:33 +01:00
parent 282753854f
commit ea8357bb3c

View File

@@ -33,6 +33,7 @@ import android.util.Log
import android.widget.Toast import android.widget.Toast
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.app.ServiceCompat import androidx.core.app.ServiceCompat
import java.io.IOException
import java.io.InputStream import java.io.InputStream
import java.net.Socket import java.net.Socket
@@ -134,16 +135,13 @@ class ListenService : Service() {
private fun doListen(address: String?, port: Int) { private fun doListen(address: String?, port: Int) {
val lt = Thread { val lt = Thread {
Socket(address, port).use { socket -> Socket(address, port).use { socket ->
val result = runCatching { socket.getInputStream() } try {
if (result.isSuccess) { val inputStream = socket.getInputStream()
result.getOrNull()?.let {inputStream -> withAudioTrack { audioTrack ->
withAudioTrack { audioTrack -> streamAudio(inputStream, audioTrack)
streamAudio(inputStream, audioTrack)
}
} }
} }
else { catch (exception : IOException) {
val exception = result.exceptionOrNull()
Log.e(TAG, "Failed to stream audio", exception) Log.e(TAG, "Failed to stream audio", exception)
} }
} }
@@ -168,36 +166,38 @@ class ListenService : Service() {
audioEncoding, audioEncoding,
bufferSize, bufferSize,
AudioTrack.MODE_STREAM) AudioTrack.MODE_STREAM)
val playback = runCatching { audioTrack.play() } try {
if (playback.isFailure) { audioTrack.play()
Log.e(TAG, "Failed to start output due to ", playback.exceptionOrNull()) block(audioTrack)
return }
catch (e : java.lang.IllegalStateException ) {
Log.e(TAG, "Failed to start output due to ", e)
}
finally {
audioTrack.stop()
} }
runCatching { block(audioTrack) }
audioTrack.stop()
} }
private fun streamAudio(inputStream: InputStream, audioTrack: AudioTrack) { private fun streamAudio(inputStream: InputStream, audioTrack: AudioTrack) {
val readBuffer = ByteArray(byteBufferSize) val readBuffer = ByteArray(byteBufferSize)
val decodedBuffer = ShortArray(byteBufferSize * 2) val decodedBuffer = ShortArray(byteBufferSize * 2)
while (!Thread.currentThread().isInterrupted) { try {
val read = runCatching { inputStream.read(readBuffer) } while (!Thread.currentThread().isInterrupted) {
if (read.isFailure) { val len = inputStream.read(readBuffer)
return if (len < 0) {
} return
val len = read.getOrDefault(-1) }
if (len < 0) { val decoded: Int = AudioCodecDefines.CODEC.decode(decodedBuffer, readBuffer, len, 0)
return if (decoded > 0) {
} audioTrack.write(decodedBuffer, 0, decoded)
val decoded: Int = AudioCodecDefines.CODEC.decode(decodedBuffer, readBuffer, len, 0) val decodedBytes = ShortArray(decoded)
if (decoded > 0) { System.arraycopy(decodedBuffer, 0, decodedBytes, 0, decoded)
audioTrack.write(decodedBuffer, 0, decoded) volumeHistory.onAudioData(decodedBytes)
val decodedBytes = ShortArray(decoded) updateCallback?.invoke()
System.arraycopy(decodedBuffer, 0, decodedBytes, 0, decoded) }
volumeHistory.onAudioData(decodedBytes)
updateCallback?.invoke()
} }
} catch (e: IOException) {
Log.e(TAG, "Failed to read from socket due to ")
} }
} }