Skip to content

Commit

Permalink
Added a callback to get elapsed recording time in seconds.
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhay Koradiya committed Oct 1, 2020
1 parent c2025a9 commit 86c1653
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 174 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ captures/
*.jks
*.keystore
.externalNativeBuild
google-services.json
google-services.json
.idea/codeStyles/Project.xml
125 changes: 0 additions & 125 deletions .idea/codeStyles/Project.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/copyright/MIT_License.xml

This file was deleted.

3 changes: 0 additions & 3 deletions .idea/copyright/profiles_settings.xml

This file was deleted.

14 changes: 0 additions & 14 deletions .idea/inspectionProfiles/Project_Default.xml

This file was deleted.

10 changes: 0 additions & 10 deletions .idea/modules.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ class WaveRecorder(private var filePath: String) {
*/
var onStateChangeListener: ((RecorderState) -> Unit)? = null

/**
* Register a callback to get elapsed recording time in seconds
*/
var onTimeElapsed: ((Long) -> Unit)? = null

/**
* Activates Noise Suppressor during recording if the device implements noise
* suppression.
Expand Down Expand Up @@ -121,18 +126,22 @@ class WaveRecorder(private var filePath: String) {
waveConfig.audioEncoding
)
val data = ByteArray(bufferSize)
val outputStream = File(filePath).outputStream()
val file = File(filePath)
val outputStream = file.outputStream()
while (isRecording) {
val operationStatus = audioRecorder.read(data, 0, bufferSize)

if (AudioRecord.ERROR_INVALID_OPERATION != operationStatus) {
if (!isPaused) outputStream.write(data)

onAmplitudeListener?.let {

withContext(Dispatchers.Default) {
withContext(Dispatchers.Main) {
onAmplitudeListener?.let {
it(calculateAmplitudeMax(data))
}
onTimeElapsed?.let {
val audioLengthInSeconds: Long = file.length() / (2 * waveConfig.sampleRate)
it(audioLengthInSeconds)
}
}


Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.3.50'
ext.kotlin_version = '1.4.10'
repositories {
google()
jcenter()

}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.1'
classpath 'com.android.tools.build:gradle:4.0.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.util.*
import java.util.concurrent.TimeUnit

class MainActivity : AppCompatActivity() {
private val PERMISSIONS_REQUEST_RECORD_AUDIO = 77
Expand All @@ -63,6 +65,11 @@ class MainActivity : AppCompatActivity() {
RecorderState.PAUSE -> pauseRecording()
}
}
waveRecorder.onTimeElapsed = {
Log.e(TAG, "onCreate: time elapsed $it")
timeTextView.text = formatTimeUnit(it * 1000)
}

startStopRecordingButton.setOnClickListener {

if (!isRecording) {
Expand Down Expand Up @@ -165,4 +172,19 @@ class MainActivity : AppCompatActivity() {
companion object {
private const val TAG = "MainActivity"
}

private fun formatTimeUnit(timeInMilliseconds: Long): String {
return try {
String.format(
Locale.getDefault(),
"%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes(timeInMilliseconds),
TimeUnit.MILLISECONDS.toSeconds(timeInMilliseconds) - TimeUnit.MINUTES.toSeconds(
TimeUnit.MILLISECONDS.toMinutes(timeInMilliseconds)
)
)
} catch (e: Exception) {
"00:00"
}
}
}
14 changes: 12 additions & 2 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
<?xml version="1.0" encoding="utf-8"?><!--
~ MIT License
~
~ Copyright (c) 2019 squti
Expand Down Expand Up @@ -30,6 +29,17 @@
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/timeTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/startStopRecordingButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:text="" />


<Button
android:id="@+id/startStopRecordingButton"
Expand Down

0 comments on commit 86c1653

Please sign in to comment.