@capgo/native-audio 6.4.13 → 6.4.17

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.
@@ -395,6 +395,7 @@ public class NativeAudio
395
395
  if (asset != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
396
396
  asset.setRate(rate);
397
397
  }
398
+ call.resolve();
398
399
  } else {
399
400
  call.reject(ERROR_AUDIO_ASSET_MISSING);
400
401
  }
@@ -135,33 +135,37 @@ public class NativeAudio: CAPPlugin, AVAudioPlayerDelegate {
135
135
  let audioId = call.getString(Constant.AssetIdKey) ?? ""
136
136
  let time = call.getDouble("time") ?? 0
137
137
  let delay = call.getDouble("delay") ?? 0
138
- if audioId != "" {
139
- let queue = DispatchQueue(label: "ee.forgr.audio.complex.queue", qos: .userInitiated)
140
-
141
- queue.async {
142
- if self.audioList.count > 0 {
143
- let asset = self.audioList[audioId]
144
-
145
- if asset != nil {
146
- if asset is AudioAsset {
147
- let audioAsset = asset as? AudioAsset
148
- self.activateSession()
149
- if self.fadeMusic {
150
- audioAsset?.playWithFade(time: time)
151
- } else {
152
- audioAsset?.play(time: time, delay: delay)
153
- }
154
- call.resolve()
155
- } else if asset is Int32 {
156
- let audioAsset = asset as? NSNumber ?? 0
157
- self.activateSession()
158
- AudioServicesPlaySystemSound(SystemSoundID(audioAsset.intValue))
159
- call.resolve()
160
- } else {
161
- call.reject(Constant.ErrorAssetNotFound)
162
- }
163
- }
138
+ if audioId == "" {
139
+ call.reject(Constant.ErrorAssetId)
140
+ return
141
+ }
142
+ if self.audioList.count == 0 {
143
+ call.reject("Audio list is empty")
144
+ return
145
+ }
146
+ let queue = DispatchQueue(label: "ee.forgr.audio.complex.queue", qos: .userInitiated)
147
+ let asset = self.audioList[audioId]
148
+ if asset == nil {
149
+ call.reject(Constant.ErrorAssetNotFound)
150
+ return
151
+ }
152
+ queue.async {
153
+ if asset is AudioAsset {
154
+ let audioAsset = asset as? AudioAsset
155
+ self.activateSession()
156
+ if self.fadeMusic {
157
+ audioAsset?.playWithFade(time: time)
158
+ } else {
159
+ audioAsset?.play(time: time, delay: delay)
164
160
  }
161
+ call.resolve()
162
+ } else if asset is Int32 {
163
+ let audioAsset = asset as? NSNumber ?? 0
164
+ self.activateSession()
165
+ AudioServicesPlaySystemSound(SystemSoundID(audioAsset.intValue))
166
+ call.resolve()
167
+ } else {
168
+ call.reject(Constant.ErrorAssetNotFound)
165
169
  }
166
170
  }
167
171
  }
@@ -172,14 +176,16 @@ public class NativeAudio: CAPPlugin, AVAudioPlayerDelegate {
172
176
  call.reject(Constant.ErrorAssetId)
173
177
  return nil
174
178
  }
175
- if self.audioList.count > 0 {
176
- let asset = self.audioList[audioId]
177
- if asset != nil && asset is AudioAsset {
178
- return asset as? AudioAsset
179
- }
179
+ if self.audioList.count == 0 {
180
+ call.reject("Audio list is empty")
181
+ return nil
180
182
  }
181
- call.reject(Constant.ErrorAssetNotFound + " - " + audioId)
182
- return nil
183
+ let asset = self.audioList[audioId]
184
+ if asset == nil || !(asset is AudioAsset) {
185
+ call.reject(Constant.ErrorAssetNotFound + " - " + audioId)
186
+ return nil
187
+ }
188
+ return asset as? AudioAsset
183
189
  }
184
190
 
185
191
  @objc func getDuration(_ call: CAPPluginCall) {
@@ -224,9 +230,14 @@ public class NativeAudio: CAPPlugin, AVAudioPlayerDelegate {
224
230
  @objc func stop(_ call: CAPPluginCall) {
225
231
  let audioId = call.getString(Constant.AssetIdKey) ?? ""
226
232
 
233
+ if self.audioList.count == 0 {
234
+ call.reject("Audio list is empty")
235
+ return
236
+ }
227
237
  do {
228
238
  try stopAudio(audioId: audioId)
229
239
  self.endSession()
240
+ call.resolve()
230
241
  } catch {
231
242
  call.reject(Constant.ErrorAssetNotFound)
232
243
  }
@@ -243,16 +254,18 @@ public class NativeAudio: CAPPlugin, AVAudioPlayerDelegate {
243
254
 
244
255
  @objc func unload(_ call: CAPPluginCall) {
245
256
  let audioId = call.getString(Constant.AssetIdKey) ?? ""
246
- if self.audioList.count > 0 {
247
- let asset = self.audioList[audioId]
248
- if asset != nil && asset is AudioAsset {
249
- guard let audioAsset = asset as? AudioAsset else {
250
- call.reject("Cannot cast to AudioAsset")
251
- return
252
- }
253
- audioAsset.unload()
254
- self.audioList[audioId] = nil
257
+ if self.audioList.count == 0 {
258
+ call.reject("Audio list is empty")
259
+ return
260
+ }
261
+ let asset = self.audioList[audioId]
262
+ if asset != nil && asset is AudioAsset {
263
+ guard let audioAsset = asset as? AudioAsset else {
264
+ call.reject("Cannot cast to AudioAsset")
265
+ return
255
266
  }
267
+ audioAsset.unload()
268
+ self.audioList[audioId] = nil
256
269
  }
257
270
  call.resolve()
258
271
  }
@@ -294,109 +307,105 @@ public class NativeAudio: CAPPlugin, AVAudioPlayerDelegate {
294
307
  let volume: Float?
295
308
  let delay: Float?
296
309
  var isLocalUrl: Bool = call.getBool("isUrl") ?? false
310
+ if audioId == "" {
311
+ call.reject(Constant.ErrorAssetId)
312
+ return
313
+ }
314
+ var assetPath: String = call.getString(Constant.AssetPathKey) ?? ""
315
+
316
+ if complex {
317
+ volume = call.getFloat("volume") ?? 1.0
318
+ channels = call.getInt("channels") ?? 1
319
+ delay = call.getFloat("delay") ?? 1.0
320
+ } else {
321
+ channels = 0
322
+ volume = 0
323
+ delay = 0
324
+ isLocalUrl = false
325
+ }
297
326
 
298
- if audioId != "" {
299
- var assetPath: String = call.getString(Constant.AssetPathKey) ?? ""
327
+ if audioList.isEmpty {
328
+ audioList = [:]
329
+ }
300
330
 
301
- if complex {
302
- volume = call.getFloat("volume") ?? 1.0
303
- channels = call.getInt("channels") ?? 1
304
- delay = call.getFloat("delay") ?? 1.0
331
+ let asset = audioList[audioId]
332
+ let queue = DispatchQueue(label: "ee.forgr.audio.simple.queue", qos: .userInitiated)
333
+ if asset != nil {
334
+ call.reject(Constant.ErrorAssetAlreadyLoaded + " - " + audioId)
335
+ return
336
+ }
337
+ queue.async {
338
+ var basePath: String?
339
+ if let url = URL(string: assetPath), url.scheme != nil {
340
+ // Handle remote URL
341
+ let remoteAudioAsset = RemoteAudioAsset(owner: self, withAssetId: audioId, withPath: assetPath, withChannels: channels, withVolume: volume, withFadeDelay: delay)
342
+ self.audioList[audioId] = remoteAudioAsset
343
+ call.resolve()
344
+ return
345
+ } else if isLocalUrl == false {
346
+ // Handle public folder
347
+ // if assetPath doesnt start with public/ add it
348
+ assetPath = assetPath.starts(with: "public/") ? assetPath : "public/" + assetPath
349
+
350
+ let assetPathSplit = assetPath.components(separatedBy: ".")
351
+ basePath = Bundle.main.path(forResource: assetPathSplit[0], ofType: assetPathSplit[1])
305
352
  } else {
306
- channels = 0
307
- volume = 0
308
- delay = 0
309
- isLocalUrl = false
353
+ // Handle local file URL
354
+ let fileURL = URL(fileURLWithPath: assetPath)
355
+ basePath = fileURL.path
310
356
  }
311
357
 
312
- if audioList.isEmpty {
313
- audioList = [:]
314
- }
315
-
316
- let asset = audioList[audioId]
317
- let queue = DispatchQueue(label: "ee.forgr.audio.simple.queue", qos: .userInitiated)
318
- queue.async {
319
- if asset == nil {
320
- var basePath: String?
321
- if let url = URL(string: assetPath), url.scheme != nil {
322
- // Handle remote URL
323
- let remoteAudioAsset = RemoteAudioAsset(owner: self, withAssetId: audioId, withPath: assetPath, withChannels: channels, withVolume: volume, withFadeDelay: delay)
324
- self.audioList[audioId] = remoteAudioAsset
325
- call.resolve()
326
- } else if isLocalUrl == false {
327
- // Handle public folder
328
- // if assetPath doesnt start with public/ add it
329
- assetPath = assetPath.starts(with: "public/") ? assetPath : "public/" + assetPath
330
-
331
- let assetPathSplit = assetPath.components(separatedBy: ".")
332
- basePath = Bundle.main.path(forResource: assetPathSplit[0], ofType: assetPathSplit[1])
333
- } else {
334
- // Handle local file URL
335
- let fileURL = URL(fileURLWithPath: assetPath)
336
- basePath = fileURL.path
337
- }
338
-
339
- if let basePath = basePath, FileManager.default.fileExists(atPath: basePath) {
340
- if !complex {
341
- let soundFileUrl = URL(fileURLWithPath: basePath)
342
- var soundId = SystemSoundID()
343
- AudioServicesCreateSystemSoundID(soundFileUrl as CFURL, &soundId)
344
- self.audioList[audioId] = NSNumber(value: Int32(soundId))
345
- call.resolve()
346
- } else {
347
- let audioAsset = AudioAsset(
348
- owner: self,
349
- withAssetId: audioId, withPath: basePath, withChannels: channels,
350
- withVolume: volume, withFadeDelay: delay)
351
- self.audioList[audioId] = audioAsset
352
- call.resolve()
353
- }
354
- } else {
355
- if FileManager.default.fileExists(atPath: assetPath) {
356
- // Use the original assetPath
357
- if !complex {
358
- let soundFileUrl = URL(fileURLWithPath: assetPath)
359
- var soundId = SystemSoundID()
360
- AudioServicesCreateSystemSoundID(soundFileUrl as CFURL, &soundId)
361
- self.audioList[audioId] = NSNumber(value: Int32(soundId))
362
- call.resolve()
363
- } else {
364
- let audioAsset = AudioAsset(
365
- owner: self,
366
- withAssetId: audioId, withPath: assetPath, withChannels: channels,
367
- withVolume: volume, withFadeDelay: delay)
368
- self.audioList[audioId] = audioAsset
369
- call.resolve()
370
- }
371
- } else {
372
- let attributes = try? FileManager.default.attributesOfItem(atPath: assetPath)
373
- call.reject(Constant.ErrorAssetPath + " - " + assetPath)
374
- }
375
- }
358
+ if let basePath = basePath, FileManager.default.fileExists(atPath: basePath) {
359
+ if !complex {
360
+ let soundFileUrl = URL(fileURLWithPath: basePath)
361
+ var soundId = SystemSoundID()
362
+ AudioServicesCreateSystemSoundID(soundFileUrl as CFURL, &soundId)
363
+ self.audioList[audioId] = NSNumber(value: Int32(soundId))
364
+ } else {
365
+ let audioAsset = AudioAsset(
366
+ owner: self,
367
+ withAssetId: audioId, withPath: basePath, withChannels: channels,
368
+ withVolume: volume, withFadeDelay: delay)
369
+ self.audioList[audioId] = audioAsset
370
+ }
371
+ } else {
372
+ if !FileManager.default.fileExists(atPath: assetPath) {
373
+ call.reject(Constant.ErrorAssetPath + " - " + assetPath)
374
+ return
375
+ }
376
+ // Use the original assetPath
377
+ if !complex {
378
+ let soundFileUrl = URL(fileURLWithPath: assetPath)
379
+ var soundId = SystemSoundID()
380
+ AudioServicesCreateSystemSoundID(soundFileUrl as CFURL, &soundId)
381
+ self.audioList[audioId] = NSNumber(value: Int32(soundId))
376
382
  } else {
377
- call.reject(Constant.ErrorAssetAlreadyLoaded + " - " + audioId)
383
+ let audioAsset = AudioAsset(
384
+ owner: self,
385
+ withAssetId: audioId, withPath: assetPath, withChannels: channels,
386
+ withVolume: volume, withFadeDelay: delay)
387
+ self.audioList[audioId] = audioAsset
378
388
  }
379
389
  }
390
+ call.resolve()
380
391
  }
381
392
  }
382
393
 
383
394
  private func stopAudio(audioId: String) throws {
384
- if self.audioList.count > 0 {
385
- let asset = self.audioList[audioId]
395
+ let asset = self.audioList[audioId]
386
396
 
387
- if asset != nil {
388
- if asset is AudioAsset {
389
- let audioAsset = asset as? AudioAsset
397
+ if asset == nil {
398
+ throw MyError.runtimeError(Constant.ErrorAssetNotFound)
399
+ }
400
+ if !(asset is AudioAsset) {
401
+ throw MyError.runtimeError(Constant.ErrorAssetNotFound)
402
+ }
403
+ let audioAsset = asset as? AudioAsset
390
404
 
391
- if self.fadeMusic {
392
- audioAsset?.playWithFade(time: audioAsset?.getCurrentTime() ?? 0)
393
- } else {
394
- audioAsset?.stop()
395
- }
396
- }
397
- } else {
398
- throw MyError.runtimeError(Constant.ErrorAssetNotFound)
399
- }
405
+ if self.fadeMusic {
406
+ audioAsset?.playWithFade(time: audioAsset?.getCurrentTime() ?? 0)
407
+ } else {
408
+ audioAsset?.stop()
400
409
  }
401
410
  }
402
411
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capgo/native-audio",
3
- "version": "6.4.13",
3
+ "version": "6.4.17",
4
4
  "description": "A native plugin for native audio engine",
5
5
  "main": "dist/plugin.js",
6
6
  "module": "dist/esm/index.js",