Undo refactoring in ListenService
This commit is contained in:
@@ -34,7 +34,6 @@ 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.IOException
|
||||||
import java.io.InputStream
|
|
||||||
import java.net.Socket
|
import java.net.Socket
|
||||||
|
|
||||||
class ListenService : Service() {
|
class ListenService : Service() {
|
||||||
@@ -134,15 +133,11 @@ class ListenService : Service() {
|
|||||||
private var updateCallback: (() -> Unit)? = null
|
private var updateCallback: (() -> Unit)? = null
|
||||||
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 ->
|
val socket = Socket(address, port)
|
||||||
try {
|
streamAudio(socket).onFailure { e ->
|
||||||
val inputStream = socket.getInputStream()
|
when (e) {
|
||||||
withAudioTrack { audioTrack ->
|
is IOException -> Log.e(TAG, "Failed to stream audio", e)
|
||||||
streamAudio(inputStream, audioTrack)
|
else -> Log.e(TAG, "Failed to stream audio for other reason", e)
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (exception : IOException) {
|
|
||||||
Log.e(TAG, "Failed to stream audio", exception)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!Thread.currentThread().isInterrupted) {
|
if (!Thread.currentThread().isInterrupted) {
|
||||||
@@ -158,7 +153,7 @@ class ListenService : Service() {
|
|||||||
lt.start()
|
lt.start()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun withAudioTrack(block: (AudioTrack) -> Unit) {
|
private fun streamAudio(socket: Socket): Result<Int> {
|
||||||
Log.i(TAG, "Setting up stream")
|
Log.i(TAG, "Setting up stream")
|
||||||
val audioTrack = AudioTrack(AudioManager.STREAM_MUSIC,
|
val audioTrack = AudioTrack(AudioManager.STREAM_MUSIC,
|
||||||
frequency,
|
frequency,
|
||||||
@@ -168,24 +163,25 @@ class ListenService : Service() {
|
|||||||
AudioTrack.MODE_STREAM)
|
AudioTrack.MODE_STREAM)
|
||||||
try {
|
try {
|
||||||
audioTrack.play()
|
audioTrack.play()
|
||||||
block(audioTrack)
|
|
||||||
}
|
|
||||||
catch (e : java.lang.IllegalStateException ) {
|
|
||||||
Log.e(TAG, "Failed to start output due to ", e)
|
|
||||||
}
|
|
||||||
finally {
|
|
||||||
audioTrack.stop()
|
|
||||||
}
|
}
|
||||||
|
catch (e : java.lang.IllegalStateException) {
|
||||||
|
return Result.failure(e)
|
||||||
|
}
|
||||||
|
|
||||||
|
val inputStream = try {
|
||||||
|
socket.getInputStream()
|
||||||
|
}
|
||||||
|
catch (e: IOException) {
|
||||||
|
return Result.failure(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
try {
|
||||||
while (!Thread.currentThread().isInterrupted) {
|
while (!Thread.currentThread().isInterrupted) {
|
||||||
val len = inputStream.read(readBuffer)
|
val len = inputStream.read(readBuffer)
|
||||||
if (len < 0) {
|
if (len < 0) {
|
||||||
return
|
return Result.success(len)
|
||||||
}
|
}
|
||||||
val decoded: Int = AudioCodecDefines.CODEC.decode(decodedBuffer, readBuffer, len, 0)
|
val decoded: Int = AudioCodecDefines.CODEC.decode(decodedBuffer, readBuffer, len, 0)
|
||||||
if (decoded > 0) {
|
if (decoded > 0) {
|
||||||
@@ -196,9 +192,13 @@ class ListenService : Service() {
|
|||||||
updateCallback?.invoke()
|
updateCallback?.invoke()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e: IOException) {
|
} catch (e: Exception) {
|
||||||
Log.e(TAG, "Failed to read from socket due to ", e)
|
Log.e(TAG, "Connection failed", e)
|
||||||
|
} finally {
|
||||||
|
audioTrack.stop()
|
||||||
|
socket.close()
|
||||||
}
|
}
|
||||||
|
return Result.success(0) // Not really a success, but that's how it maps
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun playAlert() {
|
private fun playAlert() {
|
||||||
|
|||||||
Reference in New Issue
Block a user