Skip to content

Commit

Permalink
Recorder: Use File instead of Path
Browse files Browse the repository at this point in the history
We always end up converting to File anyways

Change-Id: I92c48ca93baf87e778fc98794381bfaacaa263e1
  • Loading branch information
luca020400 committed Nov 5, 2024
1 parent 229da7c commit 09b8722
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ import android.util.Log
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.lineageos.recorder.flow.RecordingsFlow
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.nio.file.Files
import java.nio.file.Path

object RecordingsRepository {
private val LOG_TAG = this::class.simpleName!!
Expand All @@ -31,15 +30,15 @@ object RecordingsRepository {
fun recordings(context: Context) = RecordingsFlow(context).flowData()

suspend fun addRecordingToContentProvider(
context: Context, path: Path, mimeType: String
context: Context, file: File, mimeType: String
) = withContext(Dispatchers.IO) {
val contentResolver = context.contentResolver

val uri = contentResolver.insert(
MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY),
buildCv(path, mimeType)
buildCv(file, mimeType)
) ?: run {
Log.e(LOG_TAG, "Failed to insert ${path.toAbsolutePath()}")
Log.e(LOG_TAG, "Failed to insert ${file.absoluteFile}")

return@withContext null
}
Expand All @@ -49,14 +48,16 @@ object RecordingsRepository {
uri, "w", null
)?.use { pfd ->
FileOutputStream(pfd.fileDescriptor).use { oStream ->
Files.copy(path, oStream)
file.inputStream().use { iStream ->
iStream.copyTo(oStream)
}
}
val values = ContentValues().apply {
put(MediaStore.MediaColumns.IS_PENDING, 0)
}
contentResolver.update(uri, values, null, null)
try {
Files.delete(path)
file.delete()
} catch (e: IOException) {
Log.w(LOG_TAG, "Failed to delete tmp file")
}
Expand All @@ -70,8 +71,8 @@ object RecordingsRepository {
}
}

private fun buildCv(path: Path, mimeType: String) = ContentValues().apply {
val name = path.fileName.toString()
private fun buildCv(file: File, mimeType: String) = ContentValues().apply {
val name = file.name

put(MediaStore.Audio.Media.DISPLAY_NAME, name)
put(MediaStore.Audio.Media.TITLE, name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ import android.content.Context
import android.media.MediaRecorder
import android.os.Build
import androidx.annotation.RequiresPermission
import java.io.File
import java.io.IOException
import java.nio.file.Path

class GoodQualityRecorder(private val context: Context) : SoundRecording {
private var recorder: MediaRecorder? = null
private var isPaused = false

@RequiresPermission(permission.RECORD_AUDIO)
@Throws(IOException::class)
override fun startRecording(path: Path) {
override fun startRecording(file: File) {
recorder = (if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
MediaRecorder(context)
} else {
@Suppress("Deprecation")
MediaRecorder()
}).apply {
setOutputFile(path.toFile())
setOutputFile(file)
setAudioSource(MediaRecorder.AudioSource.DEFAULT)
setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
setAudioEncoder(MediaRecorder.AudioEncoder.AAC)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import java.io.FileOutputStream
import java.io.IOException
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.nio.file.Path
import kotlin.math.abs

class HighQualityRecorder : SoundRecording {
Expand All @@ -27,8 +26,8 @@ class HighQualityRecorder : SoundRecording {
private var isRecording = false

@RequiresPermission(permission.RECORD_AUDIO)
override fun startRecording(path: Path) {
this.file = path.toFile()
override fun startRecording(file: File) {
this.file = file

val audioFormat = AudioFormat.Builder()
.setSampleRate(SAMPLING_RATE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ import org.lineageos.recorder.models.UiStatus
import org.lineageos.recorder.repository.RecordingsRepository
import org.lineageos.recorder.utils.PreferencesManager
import org.lineageos.recorder.utils.RecordIntentHelper
import java.io.File
import java.io.IOException
import java.lang.ref.WeakReference
import java.nio.file.Files
import java.nio.file.Path
import java.util.Timer
import java.util.TimerTask

Expand Down Expand Up @@ -82,7 +81,7 @@ class SoundRecorderService : LifecycleService() {
}
private val messenger = Messenger(handler)
private var recorder: SoundRecording? = null
private var recordPath: Path? = null
private var recordFile: File? = null
private var amplitudeTimer: Timer? = null
private var elapsedTimeTimer: Timer? = null
private var isPaused = false
Expand Down Expand Up @@ -163,17 +162,17 @@ class SoundRecorderService : LifecycleService() {
}

return recorder?.let { recorder ->
val optPath = createNewAudioFile(fileName, recorder.fileExtension) ?: run {
val file = createNewAudioFile(fileName, recorder.fileExtension) ?: run {
Log.e(TAG, "Failed to prepare output file")
return@let false
}

this.recordPath = optPath
this.recordFile = file

isPaused = false
elapsedTime = 0
try {
recorder.startRecording(optPath)
recorder.startRecording(file)
} catch (e: IOException) {
Log.e(TAG, "Error while starting the recorder", e)
return@let false
Expand Down Expand Up @@ -202,7 +201,7 @@ class SoundRecorderService : LifecycleService() {

val success = recorder.stopRecording()

return recordPath?.takeIf { success }?.let {
return recordFile?.takeIf { success }?.let {
lifecycleScope.launch {
RecordingsRepository.addRecordingToContentProvider(
this@SoundRecorderService,
Expand Down Expand Up @@ -299,26 +298,26 @@ class SoundRecorderService : LifecycleService() {
private fun createNewAudioFile(
fileName: String,
extension: String
): Path? {
): File? {
val recordingDir = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
getExternalFilesDir(Environment.DIRECTORY_RECORDINGS)?.toPath()
getExternalFilesDir(Environment.DIRECTORY_RECORDINGS)
} else {
getExternalFilesDir(Environment.DIRECTORY_MUSIC)?.toPath()
getExternalFilesDir(Environment.DIRECTORY_MUSIC)
?.resolve(LEGACY_MUSIC_DIR)
} ?: throw Exception("Null external files dir")

val path = recordingDir.resolve(String.format(fileName, extension))
val file = recordingDir.resolve(String.format(fileName, extension))

if (!Files.exists(recordingDir)) {
if (!recordingDir.exists()) {
try {
Files.createDirectories(recordingDir)
recordingDir.mkdirs()
} catch (e: IOException) {
Log.e(TAG, "Failed to create parent directories for output")
return null
}
}

return path
return file
}

/* Timers */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ package org.lineageos.recorder.service

import android.Manifest.permission
import androidx.annotation.RequiresPermission
import java.io.File
import java.io.IOException
import java.nio.file.Path

interface SoundRecording {
@RequiresPermission(permission.RECORD_AUDIO)
@Throws(IOException::class)
fun startRecording(path: Path)
fun startRecording(file: File)

fun stopRecording(): Boolean

Expand Down

0 comments on commit 09b8722

Please sign in to comment.