Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions android/src/main/java/com/audiowaveform/AudioPlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class AudioPlayer(
private var isPlayerPrepared: Boolean = false
private var finishMode = FinishMode.Stop
private val key = playerKey
private var updateFrequency = UpdateFrequency.Low
private var updateFrequency = UpdateFrequency.Medium
private lateinit var audioPlaybackListener: CountDownTimer
private var isComponentMounted = true // Flag to track mounting status
private var isAudioFocusGranted=false
Expand Down Expand Up @@ -298,7 +298,7 @@ class AudioPlayer(

private fun startListening(promise: Promise) {
try {
audioPlaybackListener = object : CountDownTimer(player.duration, UpdateFrequency.Low.value) {
audioPlaybackListener = object : CountDownTimer(player.duration, updateFrequency.value) {
override fun onTick(millisUntilFinished: Long) {
emitCurrentDuration()
}
Expand Down
32 changes: 22 additions & 10 deletions android/src/main/java/com/audiowaveform/AudioWaveformModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class AudioWaveformModule(context: ReactApplicationContext): ReactContextBaseJav
private var bitRate: Int = 128000
private val handler = Handler(Looper.getMainLooper())
private var startTime: Long = 0
private var recordingUpdateFrequency: UpdateFrequency = UpdateFrequency.Medium

companion object {
const val NAME = "AudioWaveform"
Expand Down Expand Up @@ -74,6 +75,12 @@ class AudioWaveformModule(context: ReactApplicationContext): ReactContextBaseJav

@ReactMethod
fun startRecording(obj: ReadableMap?, promise: Promise) {
val frequencyValue = if (obj != null && obj.hasKey(Constants.updateFrequency) && !obj.isNull(Constants.updateFrequency)) {
obj.getInt(Constants.updateFrequency)
} else {
null
}
recordingUpdateFrequency = getUpdateFrequency(frequencyValue)
initRecorder(obj, promise)
val useLegacyNormalization = true
audioRecorder.startRecorder(recorder, useLegacyNormalization, promise)
Expand Down Expand Up @@ -131,7 +138,12 @@ class AudioWaveformModule(context: ReactApplicationContext): ReactContextBaseJav

val path = obj.getString(Constants.path)
val key = obj.getString(Constants.playerKey)
val frequency = obj.getInt(Constants.updateFrequency)
val frequencyValue = if (obj.hasKey(Constants.updateFrequency) && !obj.isNull(Constants.updateFrequency)) {
obj.getInt(Constants.updateFrequency)
} else {
null
}
val updateFrequency = getUpdateFrequency(frequencyValue)
val volume = obj.getInt(Constants.volume)
val progress = if (!obj.hasKey(Constants.progress) || obj.isNull(Constants.progress)) {
0 // Set default progress to zero if null, undefined, or missing
Expand All @@ -144,7 +156,7 @@ class AudioWaveformModule(context: ReactApplicationContext): ReactContextBaseJav
audioPlayers[key]?.preparePlayer(
path,
volume,
getUpdateFrequency(frequency),
updateFrequency,
progress,
promise
)
Expand Down Expand Up @@ -337,13 +349,13 @@ class AudioWaveformModule(context: ReactApplicationContext): ReactContextBaseJav
}
}

private fun getUpdateFrequency(frequency: Int?): UpdateFrequency {
if (frequency == 2) {
return UpdateFrequency.High
} else if (frequency == 1) {
return UpdateFrequency.Medium
private fun getUpdateFrequency(value: Int?): UpdateFrequency {
return when (value) {
250 -> UpdateFrequency.High
500 -> UpdateFrequency.Medium
1000 -> UpdateFrequency.Low
else -> UpdateFrequency.Medium
}
return UpdateFrequency.Low
}

private fun checkPathAndInitialiseRecorder(
Expand Down Expand Up @@ -417,13 +429,13 @@ class AudioWaveformModule(context: ReactApplicationContext): ReactContextBaseJav
args.putDouble(Constants.currentDecibel, currentDecibel/1000)
}
}
handler.postDelayed(this, UpdateFrequency.Low.value)
handler.postDelayed(this, recordingUpdateFrequency.value)
reactApplicationContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)?.emit(Constants.onCurrentRecordingWaveformData, args)
}
}

private fun startEmittingRecorderValue() {
handler.postDelayed(emitLiveRecordValue, UpdateFrequency.Low.value)
handler.postDelayed(emitLiveRecordValue, recordingUpdateFrequency.value)
}

private fun stopEmittingRecorderValue() {
Expand Down
8 changes: 4 additions & 4 deletions android/src/main/java/com/audiowaveform/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ enum class FinishMode(val value:Int) {
}

enum class UpdateFrequency(val value:Long) {
High(50),
Medium(100),
Low(200),
}
High(250),
Medium(500),
Low(1000)
}
4 changes: 2 additions & 2 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"@react-native/metro-config": "0.77.0",
"@react-native/typescript-config": "0.77.0",
"@types/jest": "^29.5.13",
"@types/react": "^18.2.6",
"@types/react": "^19.2.3",
"@types/react-test-renderer": "^18.0.0",
"eslint": "^8.19.0",
"jest": "^29.6.3",
Expand All @@ -42,4 +42,4 @@
"engines": {
"node": ">=18"
}
}
}
4 changes: 2 additions & 2 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import {
import stylesheet from './styles';
import { Colors } from './theme';

let currentPlayingRef: React.RefObject<IWaveformRef> | undefined;
let currentPlayingRef: React.RefObject<IWaveformRef | null> | undefined;
const RenderListItem = React.memo(
({
item,
Expand Down Expand Up @@ -169,7 +169,7 @@ const RenderListItem = React.memo(
}}
onCurrentProgressChange={(_currentProgress, _songDuration) => {
// console.log(
// `currentProgress ${currentProgress}, songDuration ${songDuration}`
// `currentProgress ${_currentProgress}, songDuration ${_songDuration}`
// );
}}
onChangeWaveformLoadState={state => {
Expand Down