Use try-catch where appropriate
This commit is contained in:
@@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
catch (exception : IOException) {
|
||||||
else {
|
|
||||||
val exception = result.exceptionOrNull()
|
|
||||||
Log.e(TAG, "Failed to stream audio", exception)
|
Log.e(TAG, "Failed to stream audio", exception)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -168,25 +166,24 @@ 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 ) {
|
||||||
runCatching { block(audioTrack) }
|
Log.e(TAG, "Failed to start output due to ", e)
|
||||||
|
}
|
||||||
|
finally {
|
||||||
audioTrack.stop()
|
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)
|
||||||
|
try {
|
||||||
while (!Thread.currentThread().isInterrupted) {
|
while (!Thread.currentThread().isInterrupted) {
|
||||||
val read = runCatching { inputStream.read(readBuffer) }
|
val len = inputStream.read(readBuffer)
|
||||||
if (read.isFailure) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
val len = read.getOrDefault(-1)
|
|
||||||
if (len < 0) {
|
if (len < 0) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -199,6 +196,9 @@ class ListenService : Service() {
|
|||||||
updateCallback?.invoke()
|
updateCallback?.invoke()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (e: IOException) {
|
||||||
|
Log.e(TAG, "Failed to read from socket due to ")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun playAlert() {
|
private fun playAlert() {
|
||||||
|
|||||||
Reference in New Issue
Block a user