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() {
when (type) {
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() {
when (type) {
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 {
return when (type) {
BatchType.INTERNAL -> true
BatchType.CUSTOM -> customFolder!!.canWrite() && customFolder.canRead()
BatchType.CUSTOM -> getCustomDefinedFolder().canWrite() && getCustomDefinedFolder().canRead()
}
}
@ -132,7 +138,8 @@ data class BatchesFolder(
counter: Long,
fileExtension: String,
): FileDescriptor {
val file = customFolder!!.createFile("audio/$fileExtension", "$counter.$fileExtension")!!
val file =
getCustomDefinedFolder().createFile("audio/$fileExtension", "$counter.$fileExtension")!!
customFileFileDescriptor = context.contentResolver.openFileDescriptor(file.uri, "w")!!

View File

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

View File

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

View File

@ -77,29 +77,7 @@ fun StartRecording(
if (startRecording) {
startRecording = false
audioRecorder.let { recorder ->
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)
}
audioRecorder.startRecording(context, appSettings)
}
}

View File

@ -4,6 +4,7 @@ import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.net.Uri
import android.os.IBinder
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
@ -11,11 +12,13 @@ import androidx.compose.runtime.setValue
import androidx.core.content.ContextCompat
import androidx.documentfile.provider.DocumentFile
import androidx.lifecycle.ViewModel
import app.myzel394.alibi.db.AppSettings
import app.myzel394.alibi.db.RecordingInformation
import app.myzel394.alibi.enums.RecorderState
import app.myzel394.alibi.helpers.AudioRecorderExporter
import app.myzel394.alibi.helpers.BatchesFolder
import app.myzel394.alibi.services.AudioRecorderService
import app.myzel394.alibi.services.IntervalRecorderService
import app.myzel394.alibi.services.RecorderNotificationHelper
import app.myzel394.alibi.services.RecorderService
import kotlinx.serialization.json.Json
@ -50,6 +53,8 @@ class AudioRecorderModel : ViewModel() {
var notificationDetails: RecorderNotificationHelper.NotificationDetails? = null
var batchesFolder: BatchesFolder? = null
private lateinit var settings: AppSettings
var microphoneStatus: MicrophoneConnectivityStatus = MicrophoneConnectivityStatus.CONNECTED
private set
@ -62,7 +67,9 @@ class AudioRecorderModel : ViewModel() {
override fun onServiceConnected(className: ComponentName, service: IBinder) {
recorderService =
((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 ->
recorderState = state
}
@ -86,6 +93,8 @@ class AudioRecorderModel : ViewModel() {
microphoneStatus = MicrophoneConnectivityStatus.CONNECTED
}
recorder.batchesFolder = batchesFolder ?: recorder.batchesFolder
recorder.settings =
IntervalRecorderService.Settings.from(settings.audioRecorderSettings)
}.also {
// Init UI from the service
it.startRecording()
@ -111,11 +120,33 @@ class AudioRecorderModel : ViewModel() {
microphoneStatus = MicrophoneConnectivityStatus.CONNECTED
}
fun startRecording(context: Context) {
fun startRecording(context: Context, settings: AppSettings) {
runCatching {
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 {
action = "init"