Skip to content

Commit

Permalink
fix: run start and stop listeners in the MainThread ... the other fun…
Browse files Browse the repository at this point in the history
…ctionality in IO
  • Loading branch information
brookmg committed May 10, 2021
1 parent c70e0a4 commit 43415e4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
4 changes: 1 addition & 3 deletions app/src/main/java/dev/brookmg/exorecord/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,9 @@ class MainActivity : AppCompatActivity() {
channels = wavFilePath.channelCount,
quality = 1f,
) { progressBar.progress = it.roundToInt() }
withContext(Dispatchers.Main) {
progressBarTextView.text = "Conversion Done"
}
}

progressBarTextView.text = "Conversion Done"
}
}
}
Expand Down
25 changes: 16 additions & 9 deletions exorecord/src/main/java/dev/brookmg/exorecord/lib/ExoRecord.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dev.brookmg.exorecord.lib

import android.app.Application
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

/**
* It's wise to pass the application context here
Expand All @@ -15,16 +17,22 @@ class ExoRecord(private val application: Application) : IExoRecord{
fun onStopRecording(record: IExoRecord.Record)
}

override suspend fun startRecording() : String {
override suspend fun startRecording(): String = withContext(Dispatchers.IO) {
val fileName = exoRecordProcessor.startRecording()
for (listener in _listeners.values) listener.onStartRecording(fileName)
return fileName
withContext(Dispatchers.Main) {
for (listener in _listeners.values)
listener.onStartRecording(fileName)
}
return@withContext fileName
}

override suspend fun stopRecording(): IExoRecord.Record {
override suspend fun stopRecording(): IExoRecord.Record = withContext(Dispatchers.IO){
val record = exoRecordProcessor.stopRecording()
for (listener in _listeners.values) listener.onStopRecording(record)
return record
withContext(Dispatchers.Main) {
for (listener in _listeners.values)
listener.onStopRecording(record)
}
return@withContext record
}

fun addExoRecordListener(tag: String, listener: ExoRecordListener) : Boolean {
Expand All @@ -35,12 +43,11 @@ class ExoRecord(private val application: Application) : IExoRecord{

fun clearListeners() = _listeners.clear()

fun removeExoRecordListener(tag: String) : Boolean{
fun removeExoRecordListener(tag: String) : Boolean {
return if (_listeners.containsKey(tag)) {
_listeners.remove(tag)
true
}
else false
} else false
}

fun removeExoRecordListener(listener: ExoRecordListener) : Boolean {
Expand Down

0 comments on commit 43415e4

Please sign in to comment.