Record and send voice messages (#1596)

---------

Co-authored-by: ElementBot <benoitm+elementbot@element.io>
This commit is contained in:
jonnyandrew 2023-10-23 18:28:00 +01:00 committed by GitHub
parent da345f9afa
commit 5c582bba1b
68 changed files with 2274 additions and 82 deletions

View file

@ -0,0 +1,32 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
plugins {
id("io.element.android-library")
alias(libs.plugins.anvil)
}
android {
namespace = "io.element.android.libraries.voicerecorder.api"
}
anvil {
generateDaggerFactories.set(true)
}
dependencies {
implementation(libs.androidx.annotationjvm)
implementation(libs.coroutines.core)
}

View file

@ -0,0 +1,55 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.libraries.voicerecorder.api
import android.Manifest
import androidx.annotation.RequiresPermission
import kotlinx.coroutines.flow.StateFlow
/**
* Audio recorder which records audio to opus/ogg files.
*/
interface VoiceRecorder {
/**
* Start a recording.
*
* Call [stopRecord] to stop the recording and release resources.
*/
@RequiresPermission(Manifest.permission.RECORD_AUDIO)
suspend fun startRecord()
/**
* Stop the current recording.
*
* Call [deleteRecording] to delete any recorded audio.
*
* @param cancelled If true, the recording is deleted.
*/
suspend fun stopRecord(
cancelled: Boolean = false
)
/**
* Stop the current recording and delete the output file.
*/
suspend fun deleteRecording()
/**
* The current state of the recorder.
*/
val state: StateFlow<VoiceRecorderState>
}

View file

@ -0,0 +1,44 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.libraries.voicerecorder.api
import java.io.File
sealed class VoiceRecorderState {
/**
* The recorder is idle and not recording.
*/
data object Idle : VoiceRecorderState()
/**
* The recorder is currently recording.
*
* @property level The current audio level of the recording as a fraction of 1.
*/
data class Recording(val level: Double) : VoiceRecorderState()
/**
* The recorder has finished recording.
*
* @property file The recorded file.
* @property mimeType The mime type of the file.
*/
data class Finished(
val file: File,
val mimeType: String,
) : VoiceRecorderState()
}