fix: Fixing audio recorder

This commit is contained in:
Myzel394 2023-11-18 19:27:54 +01:00
parent 6adff096d2
commit 79ba18630c
No known key found for this signature in database
GPG Key ID: 79CC92F37B3E1A2B
5 changed files with 59 additions and 46 deletions

View File

@ -22,7 +22,11 @@ data class BatchesFolder(
fun initFolders() { fun initFolders() {
when (type) { when (type) {
BatchType.INTERNAL -> getFolder(context).mkdirs() BatchType.INTERNAL -> getFolder(context).mkdirs()
BatchType.CUSTOM -> customFolder?.createDirectory(subfolderName) BatchType.CUSTOM -> {
if (customFolder!!.findFile(subfolderName) == null) {
customFolder.createDirectory(subfolderName)
}
}
} }
} }
@ -93,7 +97,9 @@ data class BatchesFolder(
fun deleteRecordings() { fun deleteRecordings() {
when (type) { when (type) {
BatchType.INTERNAL -> getInternalFolder().deleteRecursively() BatchType.INTERNAL -> getInternalFolder().deleteRecursively()
BatchType.CUSTOM -> getCustomDefinedFolder().delete() BatchType.CUSTOM -> getCustomDefinedFolder().listFiles().forEach {
it.delete()
}
} }
} }
@ -120,7 +126,7 @@ data class BatchesFolder(
fun checkIfFolderIsAccessible(): Boolean { fun checkIfFolderIsAccessible(): Boolean {
return when (type) { return when (type) {
BatchType.INTERNAL -> true BatchType.INTERNAL -> true
BatchType.CUSTOM -> customFolder!!.canWrite() && customFolder.canRead() BatchType.CUSTOM -> getCustomDefinedFolder().canWrite() && getCustomDefinedFolder().canRead()
} }
} }
@ -132,7 +138,8 @@ data class BatchesFolder(
counter: Long, counter: Long,
fileExtension: String, fileExtension: String,
): FileDescriptor { ): FileDescriptor {
val file = customFolder!!.createFile("audio/$fileExtension", "$counter.$fileExtension")!! val file =
getCustomDefinedFolder().createFile("audio/$fileExtension", "$counter.$fileExtension")!!
customFileFileDescriptor = context.contentResolver.openFileDescriptor(file.uri, "w")!! customFileFileDescriptor = context.contentResolver.openFileDescriptor(file.uri, "w")!!

View File

@ -69,22 +69,22 @@ class AudioRecorderService : IntervalRecorderService() {
when (batchesFolder.type) { when (batchesFolder.type) {
BatchesFolder.BatchType.INTERNAL -> { BatchesFolder.BatchType.INTERNAL -> {
setOutputFile( setOutputFile(
batchesFolder.asInternalGetOutputPath(counter, settings!!.fileExtension) batchesFolder.asInternalGetOutputPath(counter, settings.fileExtension)
) )
} }
BatchesFolder.BatchType.CUSTOM -> { BatchesFolder.BatchType.CUSTOM -> {
setOutputFile( setOutputFile(
batchesFolder.asCustomGetFileDescriptor(counter, settings!!.fileExtension) batchesFolder.asCustomGetFileDescriptor(counter, settings.fileExtension)
) )
} }
} }
setOutputFormat(settings!!.outputFormat) setOutputFormat(settings.outputFormat)
setAudioEncoder(settings!!.encoder) setAudioEncoder(settings.encoder)
setAudioEncodingBitRate(settings!!.bitRate) setAudioEncodingBitRate(settings.bitRate)
setAudioSamplingRate(settings!!.samplingRate) setAudioSamplingRate(settings.samplingRate)
setOnErrorListener(OnErrorListener { _, _, _ -> setOnErrorListener(OnErrorListener { _, _, _ ->
onError() onError()
}) })

View File

@ -26,14 +26,10 @@ abstract class IntervalRecorderService : ExtraRecorderInformationService() {
protected var counter = 0L protected var counter = 0L
private set private set
var settings: Settings? = null lateinit var settings: Settings
protected set
private lateinit var cycleTimer: ScheduledExecutorService private lateinit var cycleTimer: ScheduledExecutorService
protected val defaultOutputFolder: File
get() = AudioRecorderExporter.getFolder(this)
var batchesFolder: BatchesFolder = BatchesFolder.viaInternalFolder(this) var batchesFolder: BatchesFolder = BatchesFolder.viaInternalFolder(this)
var onCustomOutputFolderNotAccessible: () -> Unit = {} var onCustomOutputFolderNotAccessible: () -> Unit = {}
@ -41,10 +37,10 @@ abstract class IntervalRecorderService : ExtraRecorderInformationService() {
fun getRecordingInformation(): RecordingInformation = RecordingInformation( fun getRecordingInformation(): RecordingInformation = RecordingInformation(
folderPath = batchesFolder.exportFolderForSettings(), folderPath = batchesFolder.exportFolderForSettings(),
recordingStart = recordingStart, recordingStart = recordingStart,
maxDuration = settings!!.maxDuration, maxDuration = settings.maxDuration,
fileExtension = settings!!.fileExtension, fileExtension = settings.fileExtension,
intervalDuration = settings!!.intervalDuration, intervalDuration = settings.intervalDuration,
forceExactMaxDuration = settings!!.forceExactMaxDuration, forceExactMaxDuration = settings.forceExactMaxDuration,
) )
// Make overrideable // Make overrideable
@ -60,7 +56,7 @@ abstract class IntervalRecorderService : ExtraRecorderInformationService() {
startNewCycle() startNewCycle()
}, },
0, 0,
settings!!.intervalDuration, settings.intervalDuration,
TimeUnit.MILLISECONDS TimeUnit.MILLISECONDS
) )
} }
@ -69,12 +65,13 @@ abstract class IntervalRecorderService : ExtraRecorderInformationService() {
override fun start() { override fun start() {
super.start() super.start()
batchesFolder.initFolders()
if (!batchesFolder.checkIfFolderIsAccessible()) { if (!batchesFolder.checkIfFolderIsAccessible()) {
batchesFolder = batchesFolder =
BatchesFolder.viaInternalFolder(this@IntervalRecorderService) BatchesFolder.viaInternalFolder(this@IntervalRecorderService)
batchesFolder.initFolders()
onCustomOutputFolderNotAccessible() onCustomOutputFolderNotAccessible()
} }
batchesFolder.initFolders()
createTimer() createTimer()
} }

View File

@ -77,29 +77,7 @@ fun StartRecording(
if (startRecording) { if (startRecording) {
startRecording = false startRecording = false
audioRecorder.let { recorder -> audioRecorder.startRecording(context, appSettings)
recorder.notificationDetails = appSettings.notificationSettings.let {
if (it == null)
null
else
RecorderNotificationHelper.NotificationDetails.fromNotificationSettings(
context,
it
)
}
recorder.batchesFolder = if (appSettings.audioRecorderSettings.saveFolder == null)
BatchesFolder.viaInternalFolder(context)
else
BatchesFolder.viaCustomFolder(
context,
DocumentFile.fromTreeUri(
context,
Uri.parse(appSettings.audioRecorderSettings.saveFolder)
)!!
)
recorder.startRecording(context)
}
} }
} }

View File

@ -4,6 +4,7 @@ import android.content.ComponentName
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.ServiceConnection import android.content.ServiceConnection
import android.net.Uri
import android.os.IBinder import android.os.IBinder
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
@ -11,11 +12,13 @@ import androidx.compose.runtime.setValue
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.documentfile.provider.DocumentFile import androidx.documentfile.provider.DocumentFile
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import app.myzel394.alibi.db.AppSettings
import app.myzel394.alibi.db.RecordingInformation import app.myzel394.alibi.db.RecordingInformation
import app.myzel394.alibi.enums.RecorderState import app.myzel394.alibi.enums.RecorderState
import app.myzel394.alibi.helpers.AudioRecorderExporter import app.myzel394.alibi.helpers.AudioRecorderExporter
import app.myzel394.alibi.helpers.BatchesFolder import app.myzel394.alibi.helpers.BatchesFolder
import app.myzel394.alibi.services.AudioRecorderService import app.myzel394.alibi.services.AudioRecorderService
import app.myzel394.alibi.services.IntervalRecorderService
import app.myzel394.alibi.services.RecorderNotificationHelper import app.myzel394.alibi.services.RecorderNotificationHelper
import app.myzel394.alibi.services.RecorderService import app.myzel394.alibi.services.RecorderService
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
@ -50,6 +53,8 @@ class AudioRecorderModel : ViewModel() {
var notificationDetails: RecorderNotificationHelper.NotificationDetails? = null var notificationDetails: RecorderNotificationHelper.NotificationDetails? = null
var batchesFolder: BatchesFolder? = null var batchesFolder: BatchesFolder? = null
private lateinit var settings: AppSettings
var microphoneStatus: MicrophoneConnectivityStatus = MicrophoneConnectivityStatus.CONNECTED var microphoneStatus: MicrophoneConnectivityStatus = MicrophoneConnectivityStatus.CONNECTED
private set private set
@ -62,7 +67,9 @@ class AudioRecorderModel : ViewModel() {
override fun onServiceConnected(className: ComponentName, service: IBinder) { override fun onServiceConnected(className: ComponentName, service: IBinder) {
recorderService = recorderService =
((service as RecorderService.RecorderBinder).getService() as AudioRecorderService).also { recorder -> ((service as RecorderService.RecorderBinder).getService() as AudioRecorderService).also { recorder ->
// Update UI when the service changes recorder.clearAllRecordings()
// Init variables from us to the service
recorder.onStateChange = { state -> recorder.onStateChange = { state ->
recorderState = state recorderState = state
} }
@ -86,6 +93,8 @@ class AudioRecorderModel : ViewModel() {
microphoneStatus = MicrophoneConnectivityStatus.CONNECTED microphoneStatus = MicrophoneConnectivityStatus.CONNECTED
} }
recorder.batchesFolder = batchesFolder ?: recorder.batchesFolder recorder.batchesFolder = batchesFolder ?: recorder.batchesFolder
recorder.settings =
IntervalRecorderService.Settings.from(settings.audioRecorderSettings)
}.also { }.also {
// Init UI from the service // Init UI from the service
it.startRecording() it.startRecording()
@ -111,11 +120,33 @@ class AudioRecorderModel : ViewModel() {
microphoneStatus = MicrophoneConnectivityStatus.CONNECTED microphoneStatus = MicrophoneConnectivityStatus.CONNECTED
} }
fun startRecording(context: Context) { fun startRecording(context: Context, settings: AppSettings) {
runCatching { runCatching {
context.unbindService(connection) context.unbindService(connection)
recorderService?.clearAllRecordings()
} }
notificationDetails = settings.notificationSettings.let {
if (it == null)
null
else
RecorderNotificationHelper.NotificationDetails.fromNotificationSettings(
context,
it
)
}
batchesFolder = if (settings.audioRecorderSettings.saveFolder == null)
BatchesFolder.viaInternalFolder(context)
else
BatchesFolder.viaCustomFolder(
context,
DocumentFile.fromTreeUri(
context,
Uri.parse(settings.audioRecorderSettings.saveFolder)
)!!
)
this.settings = settings
val intent = Intent(context, AudioRecorderService::class.java).apply { val intent = Intent(context, AudioRecorderService::class.java).apply {
action = "init" action = "init"