@@ -4,9 +4,23 @@ import com.facebook.react.bridge.ReactApplicationContext
44import com.facebook.react.bridge.ReactContextBaseJavaModule
55import com.facebook.react.bridge.ReactMethod
66import com.facebook.react.bridge.Promise
7+ import com.facebook.react.bridge.Arguments
78import com.facebook.react.bridge.WritableMap
89import com.facebook.react.modules.core.DeviceEventManagerModule
910
11+ /* *
12+ * Data class for audio resource information returned from native code
13+ */
14+ data class AudioResourceInfo (
15+ val success : Boolean ,
16+ val error : String ,
17+ val key : String ,
18+ val channels : Int ,
19+ val sampleCount : Long ,
20+ val sampleRate : Int ,
21+ val durationMs : Double
22+ )
23+
1024class ElementaryModule (reactContext : ReactApplicationContext ) :
1125 ReactContextBaseJavaModule (reactContext) {
1226
@@ -34,6 +48,51 @@ class ElementaryModule(reactContext: ReactApplicationContext) :
3448 // No-op
3549 }
3650
51+ @ReactMethod
52+ fun loadAudioResource (key : String , filePath : String , promise : Promise ) {
53+ Thread {
54+ try {
55+ val result = nativeLoadAudioResource(key, filePath)
56+ if (result == null ) {
57+ promise.reject(" E_NATIVE_ERROR" , " Native audio engine not initialized" )
58+ return @Thread
59+ }
60+
61+ if (! result.success) {
62+ promise.reject(" E_LOAD_FAILED" , result.error)
63+ return @Thread
64+ }
65+
66+ val info = Arguments .createMap().apply {
67+ putString(" key" , result.key)
68+ putInt(" channels" , result.channels)
69+ putDouble(" sampleCount" , result.sampleCount.toDouble())
70+ putInt(" sampleRate" , result.sampleRate)
71+ putDouble(" durationMs" , result.durationMs)
72+ }
73+ promise.resolve(info)
74+ } catch (e: Exception ) {
75+ promise.reject(" E_LOAD_FAILED" , e.message, e)
76+ }
77+ }.start()
78+ }
79+
80+ @ReactMethod
81+ fun unloadAudioResource (key : String , promise : Promise ) {
82+ try {
83+ val result = nativeUnloadAudioResource(key)
84+ promise.resolve(result)
85+ } catch (e: Exception ) {
86+ promise.reject(" E_UNLOAD_FAILED" , e.message, e)
87+ }
88+ }
89+
90+ @ReactMethod
91+ fun getDocumentsDirectory (promise : Promise ) {
92+ val documentsDir = reactApplicationContext.filesDir.absolutePath
93+ promise.resolve(documentsDir)
94+ }
95+
3796 // Helper to emit events
3897 private fun sendEvent (eventName : String , params : WritableMap ? ) {
3998 reactApplicationContext
@@ -54,7 +113,9 @@ class ElementaryModule(reactContext: ReactApplicationContext) :
54113 nativeStartAudioEngine();
55114 }
56115
57- external fun nativeGetSampleRate (): Int ;
58- external fun nativeApplyInstructions (message : String );
59- external fun nativeStartAudioEngine ();
116+ external fun nativeGetSampleRate (): Int
117+ external fun nativeApplyInstructions (message : String )
118+ external fun nativeStartAudioEngine ()
119+ external fun nativeLoadAudioResource (key : String , filePath : String ): AudioResourceInfo ?
120+ external fun nativeUnloadAudioResource (key : String ): Boolean
60121}
0 commit comments