nosnia-audio-recorder 0.8.4 → 0.8.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -19,7 +19,7 @@ class NosniaAudioPlayerModule(private val reactContext: ReactApplicationContext)
|
|
|
19
19
|
private var mediaPlayer: MediaPlayer? = null
|
|
20
20
|
private var currentFilePath: String? = null
|
|
21
21
|
private var isPlaying = false
|
|
22
|
-
|
|
22
|
+
|
|
23
23
|
private val progressHandler = Handler(Looper.getMainLooper())
|
|
24
24
|
private val progressRunnable = object : Runnable {
|
|
25
25
|
override fun run() {
|
|
@@ -27,38 +27,37 @@ class NosniaAudioPlayerModule(private val reactContext: ReactApplicationContext)
|
|
|
27
27
|
if (isPlaying && player.isPlaying) {
|
|
28
28
|
val currentTime = player.currentPosition.toLong()
|
|
29
29
|
val duration = player.duration.toLong()
|
|
30
|
-
|
|
30
|
+
|
|
31
31
|
sendEvent("onPlaybackProgress", Arguments.createMap().apply {
|
|
32
32
|
putDouble("currentTime", currentTime.toDouble())
|
|
33
33
|
putDouble("duration", duration.toDouble())
|
|
34
34
|
putBoolean("isPlaying", true)
|
|
35
35
|
})
|
|
36
|
-
|
|
36
|
+
|
|
37
37
|
progressHandler.postDelayed(this, 100)
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
|
-
|
|
42
|
+
|
|
43
43
|
private fun sendEvent(eventName: String, params: WritableMap?) {
|
|
44
|
-
|
|
44
|
+
reactContext
|
|
45
45
|
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
|
|
46
46
|
.emit(eventName, params)
|
|
47
47
|
}
|
|
48
|
-
|
|
48
|
+
|
|
49
49
|
private fun startProgressUpdates() {
|
|
50
50
|
progressHandler.post(progressRunnable)
|
|
51
51
|
}
|
|
52
|
-
|
|
52
|
+
|
|
53
53
|
private fun stopProgressUpdates() {
|
|
54
54
|
progressHandler.removeCallbacks(progressRunnable)
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
-
override fun getName(): String
|
|
58
|
-
return NAME
|
|
59
|
-
}
|
|
57
|
+
override fun getName(): String = NAME
|
|
60
58
|
|
|
61
|
-
|
|
59
|
+
@ReactMethod
|
|
60
|
+
fun startPlaying(options: ReadableMap, promise: Promise) {
|
|
62
61
|
try {
|
|
63
62
|
val filePath = options.getString("filePath")
|
|
64
63
|
if (filePath == null) {
|
|
@@ -78,53 +77,53 @@ class NosniaAudioPlayerModule(private val reactContext: ReactApplicationContext)
|
|
|
78
77
|
false
|
|
79
78
|
}
|
|
80
79
|
|
|
81
|
-
// Check if file exists
|
|
82
80
|
val file = File(filePath)
|
|
83
81
|
if (!file.exists()) {
|
|
84
82
|
promise.reject("FILE_NOT_FOUND", "Audio file not found: $filePath")
|
|
85
83
|
return
|
|
86
84
|
}
|
|
87
85
|
|
|
88
|
-
// Release existing player if any
|
|
89
86
|
mediaPlayer?.release()
|
|
90
87
|
|
|
88
|
+
val module = this
|
|
91
89
|
mediaPlayer = MediaPlayer().apply {
|
|
92
90
|
setDataSource(filePath)
|
|
93
91
|
setVolume(volume, volume)
|
|
94
92
|
isLooping = loop
|
|
95
|
-
|
|
93
|
+
|
|
96
94
|
setOnCompletionListener {
|
|
97
|
-
isPlaying = false
|
|
95
|
+
module.isPlaying = false
|
|
98
96
|
stopProgressUpdates()
|
|
99
97
|
sendEvent("onPlaybackComplete", Arguments.createMap())
|
|
100
98
|
}
|
|
101
|
-
|
|
102
|
-
setOnErrorListener { _,
|
|
103
|
-
isPlaying = false
|
|
99
|
+
|
|
100
|
+
setOnErrorListener { _, _, _ ->
|
|
101
|
+
module.isPlaying = false
|
|
104
102
|
stopProgressUpdates()
|
|
105
103
|
true
|
|
106
104
|
}
|
|
107
|
-
|
|
105
|
+
|
|
108
106
|
prepare()
|
|
109
107
|
start()
|
|
110
108
|
}
|
|
111
109
|
|
|
112
|
-
isPlaying = true
|
|
113
|
-
currentFilePath = filePath
|
|
110
|
+
this.isPlaying = true
|
|
111
|
+
this.currentFilePath = filePath
|
|
114
112
|
startProgressUpdates()
|
|
115
113
|
promise.resolve(null)
|
|
116
114
|
} catch (e: Exception) {
|
|
117
115
|
mediaPlayer?.release()
|
|
118
116
|
mediaPlayer = null
|
|
119
|
-
isPlaying = false
|
|
117
|
+
this.isPlaying = false
|
|
120
118
|
promise.reject("START_PLAYING_ERROR", e.message, e)
|
|
121
119
|
}
|
|
122
120
|
}
|
|
123
121
|
|
|
124
|
-
|
|
122
|
+
@ReactMethod
|
|
123
|
+
fun stopPlaying(promise: Promise) {
|
|
125
124
|
try {
|
|
126
125
|
stopProgressUpdates()
|
|
127
|
-
|
|
126
|
+
|
|
128
127
|
mediaPlayer?.apply {
|
|
129
128
|
if (isPlaying) {
|
|
130
129
|
stop()
|
|
@@ -132,46 +131,49 @@ class NosniaAudioPlayerModule(private val reactContext: ReactApplicationContext)
|
|
|
132
131
|
release()
|
|
133
132
|
}
|
|
134
133
|
mediaPlayer = null
|
|
135
|
-
isPlaying = false
|
|
136
|
-
currentFilePath = null
|
|
137
|
-
|
|
134
|
+
this.isPlaying = false
|
|
135
|
+
this.currentFilePath = null
|
|
136
|
+
|
|
138
137
|
promise.resolve(null)
|
|
139
138
|
} catch (e: Exception) {
|
|
140
139
|
promise.reject("STOP_PLAYING_ERROR", e.message, e)
|
|
141
140
|
}
|
|
142
141
|
}
|
|
143
142
|
|
|
144
|
-
|
|
143
|
+
@ReactMethod
|
|
144
|
+
fun pausePlaying(promise: Promise) {
|
|
145
145
|
try {
|
|
146
|
-
if (!isPlaying || mediaPlayer == null) {
|
|
146
|
+
if (!this.isPlaying || mediaPlayer == null) {
|
|
147
147
|
promise.reject("NOT_PLAYING", "No playback in progress")
|
|
148
148
|
return
|
|
149
149
|
}
|
|
150
150
|
|
|
151
151
|
mediaPlayer?.pause()
|
|
152
|
-
isPlaying = false
|
|
152
|
+
this.isPlaying = false
|
|
153
153
|
promise.resolve(null)
|
|
154
154
|
} catch (e: Exception) {
|
|
155
155
|
promise.reject("PAUSE_ERROR", e.message, e)
|
|
156
156
|
}
|
|
157
157
|
}
|
|
158
158
|
|
|
159
|
-
|
|
159
|
+
@ReactMethod
|
|
160
|
+
fun resumePlaying(promise: Promise) {
|
|
160
161
|
try {
|
|
161
|
-
if (isPlaying || mediaPlayer == null) {
|
|
162
|
+
if (this.isPlaying || mediaPlayer == null) {
|
|
162
163
|
promise.reject("NOT_PAUSED", "Playback is not paused")
|
|
163
164
|
return
|
|
164
165
|
}
|
|
165
166
|
|
|
166
167
|
mediaPlayer?.start()
|
|
167
|
-
isPlaying = true
|
|
168
|
+
this.isPlaying = true
|
|
168
169
|
promise.resolve(null)
|
|
169
170
|
} catch (e: Exception) {
|
|
170
171
|
promise.reject("RESUME_ERROR", e.message, e)
|
|
171
172
|
}
|
|
172
173
|
}
|
|
173
174
|
|
|
174
|
-
|
|
175
|
+
@ReactMethod
|
|
176
|
+
fun seekToTime(time: Double, promise: Promise) {
|
|
175
177
|
try {
|
|
176
178
|
if (mediaPlayer == null) {
|
|
177
179
|
promise.reject("NO_PLAYER", "No audio player initialized")
|
|
@@ -186,7 +188,8 @@ class NosniaAudioPlayerModule(private val reactContext: ReactApplicationContext)
|
|
|
186
188
|
}
|
|
187
189
|
}
|
|
188
190
|
|
|
189
|
-
|
|
191
|
+
@ReactMethod
|
|
192
|
+
fun setVolume(volume: Double, promise: Promise) {
|
|
190
193
|
try {
|
|
191
194
|
if (mediaPlayer == null) {
|
|
192
195
|
promise.reject("NO_PLAYER", "No audio player initialized")
|
|
@@ -201,11 +204,12 @@ class NosniaAudioPlayerModule(private val reactContext: ReactApplicationContext)
|
|
|
201
204
|
}
|
|
202
205
|
}
|
|
203
206
|
|
|
204
|
-
|
|
207
|
+
@ReactMethod
|
|
208
|
+
fun getPlayerStatus(promise: Promise) {
|
|
205
209
|
try {
|
|
206
210
|
val status = WritableNativeMap().apply {
|
|
207
|
-
putBoolean("isPlaying", isPlaying)
|
|
208
|
-
|
|
211
|
+
putBoolean("isPlaying", this@NosniaAudioPlayerModule.isPlaying)
|
|
212
|
+
|
|
209
213
|
if (mediaPlayer != null) {
|
|
210
214
|
putDouble("duration", mediaPlayer!!.duration.toDouble())
|
|
211
215
|
putDouble("currentTime", mediaPlayer!!.currentPosition.toDouble())
|
|
@@ -213,9 +217,9 @@ class NosniaAudioPlayerModule(private val reactContext: ReactApplicationContext)
|
|
|
213
217
|
putDouble("duration", 0.0)
|
|
214
218
|
putDouble("currentTime", 0.0)
|
|
215
219
|
}
|
|
216
|
-
|
|
217
|
-
if (currentFilePath != null) {
|
|
218
|
-
putString("currentFilePath", currentFilePath)
|
|
220
|
+
|
|
221
|
+
if (this@NosniaAudioPlayerModule.currentFilePath != null) {
|
|
222
|
+
putString("currentFilePath", this@NosniaAudioPlayerModule.currentFilePath)
|
|
219
223
|
}
|
|
220
224
|
}
|
|
221
225
|
promise.resolve(status)
|
|
@@ -230,8 +234,4 @@ class NosniaAudioPlayerModule(private val reactContext: ReactApplicationContext)
|
|
|
230
234
|
mediaPlayer?.release()
|
|
231
235
|
mediaPlayer = null
|
|
232
236
|
}
|
|
233
|
-
|
|
234
|
-
companion object {
|
|
235
|
-
const val NAME = "NosniaAudioPlayer"
|
|
236
|
-
}
|
|
237
237
|
}
|
|
@@ -435,8 +435,4 @@ class NosniaAudioRecorderModule(private val reactContext: ReactApplicationContex
|
|
|
435
435
|
val timestamp = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(Date())
|
|
436
436
|
return "recording_$timestamp.m4a"
|
|
437
437
|
}
|
|
438
|
-
|
|
439
|
-
companion object {
|
|
440
|
-
const val NAME = "NosniaAudioRecorder"
|
|
441
|
-
}
|
|
442
438
|
}
|
package/package.json
CHANGED