Moved playRecording function from Utils to CallDetailsAdapter

This commit is contained in:
Juha Heinanen
2023-03-18 10:51:24 +02:00
parent c0b905713f
commit db50ae1326
2 changed files with 70 additions and 67 deletions

View File

@ -3,6 +3,7 @@ package com.tutpro.baresip
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Typeface
import android.media.AudioAttributes
import android.media.MediaPlayer
import android.text.format.DateUtils
import android.view.LayoutInflater
@ -12,8 +13,10 @@ import android.widget.ArrayAdapter
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.core.net.toUri
import java.io.File
import java.io.IOException
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.*
@ -83,7 +86,7 @@ class CallDetailsAdapter(private val ctx: Context, private val rows: ArrayList<C
if (!decPlayer.isPlaying && !encPlayer.isPlaying) {
decPlayer.reset()
encPlayer.reset()
Utils.playRecording(ctx, viewHolder.durationView, recording, decPlayer, encPlayer)
playRecording(ctx, viewHolder.durationView, recording)
} else if (decPlayer.isPlaying && encPlayer.isPlaying) {
decPlayer.stop()
encPlayer.stop()
@ -96,4 +99,69 @@ class CallDetailsAdapter(private val ctx: Context, private val rows: ArrayList<C
return rowView
}
private fun playRecording(ctx: Context, textView: TextView, recording: Array<String>) {
Log.d(TAG, "Playing recordings ${recording[0]} and ${recording[1]}")
decPlayer.apply {
setAudioAttributes(
AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.setUsage(AudioAttributes.USAGE_MEDIA)
.build()
)
setOnPreparedListener {
encPlayer.apply {
setAudioAttributes(
AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.setUsage(AudioAttributes.USAGE_MEDIA)
.build()
)
setOnPreparedListener {
it.start()
decPlayer.start()
textView.setTextColor(ContextCompat.getColor(ctx, R.color.colorAccent))
Log.d(TAG, "Started players")
}
setOnCompletionListener {
Log.d(TAG, "Stopping encPlayer")
it.stop()
textView.setTextColor(ContextCompat.getColor(ctx, R.color.colorItemText))
}
try {
val file = recording[0]
val encFile = File(file).copyTo(File(BaresipService.filesPath + "/tmp/encode.wav"), true)
val encUri = encFile.toUri()
setDataSource(ctx, encUri)
prepareAsync()
} catch (e: IllegalArgumentException) {
Log.e(TAG, "encPlayer IllegalArgumentException: $e")
} catch (e: IOException) {
Log.e(TAG, "encPlayer IOException: $e")
} catch (e: Exception) {
Log.e(TAG, "encPlayer Exception: $e")
}
}
}
setOnCompletionListener {
Log.d(TAG, "Stopping decPlayer")
it.stop()
}
try {
val file = recording[1]
val decFile = File(file).copyTo(File(BaresipService.filesPath + "/tmp/decode.wav"), true)
val decUri = decFile.toUri()
setDataSource(ctx, decUri)
prepareAsync()
} catch (e: IllegalArgumentException) {
Log.e(TAG, "decPlayer IllegalArgumentException: $e")
} catch (e: IOException) {
Log.e(TAG, "decPlayer IOException: $e")
} catch (e: Exception) {
Log.e(TAG, "decPlayer Exception: $e")
}
}
return
}
}

View File

@ -995,71 +995,6 @@ object Utils {
}
}
fun playRecording(ctx: Context, textView: TextView, recording: Array<String>,
decPlayer: MediaPlayer, encPlayer: MediaPlayer) {
Log.d(TAG, "Playing recordings ${recording[0]} and ${recording[1]}")
decPlayer.apply {
setAudioAttributes(
AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.setUsage(AudioAttributes.USAGE_MEDIA)
.build()
)
setOnPreparedListener {
encPlayer.apply {
setAudioAttributes(
AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.setUsage(AudioAttributes.USAGE_MEDIA)
.build()
)
setOnPreparedListener {
it.start()
decPlayer.start()
textView.setTextColor(ContextCompat.getColor(ctx, R.color.colorAccent))
Log.d(TAG, "Started players")
}
setOnCompletionListener {
Log.d(TAG, "Stopping encPlayer")
it.stop()
textView.setTextColor(ContextCompat.getColor(ctx, R.color.colorItemText))
}
try {
val file = recording[0]
val encFile = File(file).copyTo(File(BaresipService.filesPath + "/tmp/encode.wav"), true)
val encUri = encFile.toUri()
setDataSource(ctx, encUri)
prepareAsync()
} catch (e: IllegalArgumentException) {
Log.e(TAG, "encPlayer IllegalArgumentException: $e")
} catch (e: IOException) {
Log.e(TAG, "encPlayer IOException: $e")
} catch (e: Exception) {
Log.e(TAG, "encPlayer Exception: $e")
}
}
}
setOnCompletionListener {
Log.d(TAG, "Stopping decPlayer")
it.stop()
}
try {
val file = recording[1]
val decFile = File(file).copyTo(File(BaresipService.filesPath + "/tmp/decode.wav"), true)
val decUri = decFile.toUri()
setDataSource(ctx, decUri)
prepareAsync()
} catch (e: IllegalArgumentException) {
Log.e(TAG, "decPlayer IllegalArgumentException: $e")
} catch (e: IOException) {
Log.e(TAG, "decPlayer IOException: $e")
} catch (e: Exception) {
Log.e(TAG, "decPlayer Exception: $e")
}
}
return
}
@Suppress("unused")
fun playFile(ctx: Context, path: String) {
Log.d(TAG, "Playing file $path")