Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ability to stop all extractors #119

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from
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
29 changes: 25 additions & 4 deletions android/src/main/java/com/audiowaveform/AudioWaveformModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.facebook.react.modules.core.DeviceEventManagerModule
import java.io.File
import java.io.IOException
import java.text.SimpleDateFormat
import java.util.Collections
import java.util.Date
import java.util.Locale

Expand Down Expand Up @@ -234,11 +235,28 @@ class AudioWaveformModule(context: ReactApplicationContext): ReactContextBaseJav

@ReactMethod
fun stopAllPlayers(promise: Promise) {
for ((key, _) in audioPlayers) {
audioPlayers[key]?.stop()
audioPlayers[key] = null
try {
audioPlayers.values.forEach{
player -> player?.stop()
}
audioPlayers.clear()
promise.resolve(true)
} catch (err: Exception) {
promise.reject("stopAllPlayers Error", "Error while stopping all players")
}
}

@ReactMethod
fun stopAllWaveFormExtractors(promise: Promise) {
try {
extractors.values.forEach{
extractor -> extractor?.forceStop()
}
extractors.clear()
promise.resolve(true)
} catch (err: Exception) {
promise.reject("stopAllExtractors Error", "Error while stopping all extractors")
}
promise.resolve(true)
}

@ReactMethod
Expand Down Expand Up @@ -305,6 +323,9 @@ class AudioWaveformModule(context: ReactApplicationContext): ReactContextBaseJav
override fun onResolve(value: MutableList<MutableList<Float>>) {
promise.resolve(Arguments.fromList(value))
}
override fun onForceStop() {
promise.resolve(Arguments.fromList(mutableListOf(emptyList<Float>())))
}
}
)
extractors[playerKey]?.startDecode();
Expand Down
11 changes: 7 additions & 4 deletions android/src/main/java/com/audiowaveform/WaveformExtractor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.WritableMap
import com.facebook.react.modules.core.DeviceEventManagerModule
import java.nio.ByteBuffer
import java.util.concurrent.CountDownLatch
import kotlin.math.pow
import kotlin.math.sqrt
import java.io.File
Expand All @@ -32,7 +31,6 @@ class WaveformExtractor(

@Volatile
private var inProgress = false
private val finishCount = CountDownLatch(1)
private var inputEof = false
private var sampleRate = 0
private var channels = 1
Expand Down Expand Up @@ -122,7 +120,6 @@ class WaveformExtractor(
Constants.LOG_TAG + " " + e.message,
"An error is thrown while decoding the audio file"
)
finishCount.countDown()
}

override fun onOutputBufferAvailable(
Expand Down Expand Up @@ -256,13 +253,18 @@ class WaveformExtractor(
}
}

fun forceStop() {
stop()
// When stopped by outside we must notify to resolved the hanging promises
extractorCallBack.onForceStop()
}

private fun stop() {
if (!inProgress) return
inProgress = false
decoder?.stop()
decoder?.release()
extractor?.release()
finishCount.countDown()
}
}

Expand All @@ -272,4 +274,5 @@ interface ExtractorCallBack {
fun onProgress(value: Float)
fun onReject(error: String?, message: String?)
fun onResolve(value: MutableList<MutableList<Float>>)
fun onForceStop()
}
36 changes: 2 additions & 34 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,6 @@ PODS:
- hermes-engine/Pre-built (= 0.72.7)
- hermes-engine/Pre-built (0.72.7)
- libevent (2.1.12)
- libwebp (1.3.2):
- libwebp/demux (= 1.3.2)
- libwebp/mux (= 1.3.2)
- libwebp/sharpyuv (= 1.3.2)
- libwebp/webp (= 1.3.2)
- libwebp/demux (1.3.2):
- libwebp/webp
- libwebp/mux (1.3.2):
- libwebp/demux
- libwebp/sharpyuv (1.3.2)
- libwebp/webp (1.3.2):
- libwebp/sharpyuv
- OpenSSL-Universal (1.1.1100)
- RCT-Folly (2021.07.22.00):
- boost
Expand Down Expand Up @@ -387,7 +375,7 @@ PODS:
- React-jsinspector (0.72.7)
- React-logger (0.72.7):
- glog
- react-native-audio-waveform (1.0.0):
- react-native-audio-waveform (2.1.2):
- RCT-Folly (= 2021.07.22.00)
- React-Core
- react-native-safe-area-context (4.11.0):
Expand Down Expand Up @@ -504,21 +492,11 @@ PODS:
- React-perflogger (= 0.72.7)
- rn-fetch-blob (0.12.0):
- React-Core
- RNFastImage (8.6.3):
- React-Core
- SDWebImage (~> 5.11.1)
- SDWebImageWebPCoder (~> 0.8.4)
- RNFS (2.20.0):
- React-Core
- RNGestureHandler (2.19.0):
- RCT-Folly (= 2021.07.22.00)
- React-Core
- SDWebImage (5.11.1):
- SDWebImage/Core (= 5.11.1)
- SDWebImage/Core (5.11.1)
- SDWebImageWebPCoder (0.8.5):
- libwebp (~> 1.0)
- SDWebImage/Core (~> 5.10)
- SocketRocket (0.6.1)
- Yoga (1.14.0)
- YogaKit (1.18.1):
Expand Down Expand Up @@ -590,7 +568,6 @@ DEPENDENCIES:
- React-utils (from `../node_modules/react-native/ReactCommon/react/utils`)
- ReactCommon/turbomodule/core (from `../node_modules/react-native/ReactCommon`)
- rn-fetch-blob (from `../node_modules/rn-fetch-blob`)
- RNFastImage (from `../node_modules/react-native-fast-image`)
- RNFS (from `../node_modules/react-native-fs`)
- RNGestureHandler (from `../node_modules/react-native-gesture-handler`)
- Yoga (from `../node_modules/react-native/ReactCommon/yoga`)
Expand All @@ -608,10 +585,7 @@ SPEC REPOS:
- FlipperKit
- fmt
- libevent
- libwebp
- OpenSSL-Universal
- SDWebImage
- SDWebImageWebPCoder
- SocketRocket
- YogaKit

Expand Down Expand Up @@ -699,8 +673,6 @@ EXTERNAL SOURCES:
:path: "../node_modules/react-native/ReactCommon"
rn-fetch-blob:
:path: "../node_modules/rn-fetch-blob"
RNFastImage:
:path: "../node_modules/react-native-fast-image"
RNFS:
:path: "../node_modules/react-native-fs"
RNGestureHandler:
Expand All @@ -726,7 +698,6 @@ SPEC CHECKSUMS:
glog: 04b94705f318337d7ead9e6d17c019bd9b1f6b1b
hermes-engine: 9180d43df05c1ed658a87cc733dc3044cf90c00a
libevent: 4049cae6c81cdb3654a443be001fb9bdceff7913
libwebp: 1786c9f4ff8a279e4dac1e8f385004d5fc253009
OpenSSL-Universal: ebc357f1e6bc71fa463ccb2fe676756aff50e88c
RCT-Folly: 424b8c9a7a0b9ab2886ffe9c3b041ef628fd4fb1
RCTRequired: 83bca1c184feb4d2e51c72c8369b83d641443f95
Expand All @@ -743,7 +714,7 @@ SPEC CHECKSUMS:
React-jsiexecutor: c49502e5d02112247ee4526bc3ccfc891ae3eb9b
React-jsinspector: 8baadae51f01d867c3921213a25ab78ab4fbcd91
React-logger: 8edc785c47c8686c7962199a307015e2ce9a0e4f
react-native-audio-waveform: 7cdb6e4963eeae907240396975b9c79713591758
react-native-audio-waveform: 99f401dee91ac357ce40cba147a31a18b539d312
react-native-safe-area-context: 851c62c48dce80ccaa5637b6aa5991a1bc36eca9
React-NativeModulesApple: b6868ee904013a7923128892ee4a032498a1024a
React-perflogger: 31ea61077185eb1428baf60c0db6e2886f141a5a
Expand All @@ -763,11 +734,8 @@ SPEC CHECKSUMS:
React-utils: 56838edeaaf651220d1e53cd0b8934fb8ce68415
ReactCommon: 5f704096ccf7733b390f59043b6fa9cc180ee4f6
rn-fetch-blob: f065bb7ab7fb48dd002629f8bdcb0336602d3cba
RNFastImage: 5c9c9fed9c076e521b3f509fe79e790418a544e8
RNFS: 4ac0f0ea233904cb798630b3c077808c06931688
RNGestureHandler: 7ad14a6c7b491add489246611d324f10009083ac
SDWebImage: a7f831e1a65eb5e285e3fb046a23fcfbf08e696d
SDWebImageWebPCoder: 908b83b6adda48effe7667cd2b7f78c897e5111d
SocketRocket: f32cd54efbe0f095c4d7594881e52619cfe80b17
Yoga: 4c3aa327e4a6a23eeacd71f61c81df1bcdf677d5
YogaKit: f782866e155069a2cca2517aafea43200b01fd5a
Expand Down
2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"private": true,
"scripts": {
"android": "react-native run-android",
"ios:pods": "bundle exec pod install --project-directory=ios",
"ios": "react-native run-ios",
"lint": "eslint .",
"start": "react-native start",
Expand All @@ -12,7 +13,6 @@
"dependencies": {
"react": "18.2.0",
"react-native": "0.72.7",
"react-native-fast-image": "^8.6.3",
"react-native-fs": "^2.20.0",
"react-native-gesture-handler": "^2.13.4",
"react-native-safe-area-context": "^4.9.0",
Expand Down
Loading