@capgo/native-audio 6.4.14 → 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
182
+ }
183
+ let asset = self.audioList[audioId]
184
+ if asset == nil || !(asset is AudioAsset) {
185
+ call.reject(Constant.ErrorAssetNotFound + " - " + audioId)
186
+ return nil
180
187
  }
181
- call.reject(Constant.ErrorAssetNotFound + " - " + audioId)
182
- return nil
188
+ return asset as? AudioAsset
183
189
  }
184
190
 
185
191
  @objc func getDuration(_ call: CAPPluginCall) {
@@ -224,6 +230,10 @@ 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()
@@ -244,16 +254,18 @@ public class NativeAudio: CAPPlugin, AVAudioPlayerDelegate {
244
254
 
245
255
  @objc func unload(_ call: CAPPluginCall) {
246
256
  let audioId = call.getString(Constant.AssetIdKey) ?? ""
247
- if self.audioList.count > 0 {
248
- let asset = self.audioList[audioId]
249
- if asset != nil && asset is AudioAsset {
250
- guard let audioAsset = asset as? AudioAsset else {
251
- call.reject("Cannot cast to AudioAsset")
252
- return
253
- }
254
- audioAsset.unload()
255
- 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
256
266
  }
267
+ audioAsset.unload()
268
+ self.audioList[audioId] = nil
257
269
  }
258
270
  call.resolve()
259
271
  }
@@ -295,109 +307,105 @@ public class NativeAudio: CAPPlugin, AVAudioPlayerDelegate {
295
307
  let volume: Float?
296
308
  let delay: Float?
297
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
+ }
298
326
 
299
- if audioId != "" {
300
- var assetPath: String = call.getString(Constant.AssetPathKey) ?? ""
327
+ if audioList.isEmpty {
328
+ audioList = [:]
329
+ }
301
330
 
302
- if complex {
303
- volume = call.getFloat("volume") ?? 1.0
304
- channels = call.getInt("channels") ?? 1
305
- 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])
306
352
  } else {
307
- channels = 0
308
- volume = 0
309
- delay = 0
310
- isLocalUrl = false
311
- }
312
-
313
- if audioList.isEmpty {
314
- audioList = [:]
353
+ // Handle local file URL
354
+ let fileURL = URL(fileURLWithPath: assetPath)
355
+ basePath = fileURL.path
315
356
  }
316
357
 
317
- let asset = audioList[audioId]
318
- let queue = DispatchQueue(label: "ee.forgr.audio.simple.queue", qos: .userInitiated)
319
- queue.async {
320
- if asset == nil {
321
- var basePath: String?
322
- if let url = URL(string: assetPath), url.scheme != nil {
323
- // Handle remote URL
324
- let remoteAudioAsset = RemoteAudioAsset(owner: self, withAssetId: audioId, withPath: assetPath, withChannels: channels, withVolume: volume, withFadeDelay: delay)
325
- self.audioList[audioId] = remoteAudioAsset
326
- call.resolve()
327
- } else if isLocalUrl == false {
328
- // Handle public folder
329
- // if assetPath doesnt start with public/ add it
330
- assetPath = assetPath.starts(with: "public/") ? assetPath : "public/" + assetPath
331
-
332
- let assetPathSplit = assetPath.components(separatedBy: ".")
333
- basePath = Bundle.main.path(forResource: assetPathSplit[0], ofType: assetPathSplit[1])
334
- } else {
335
- // Handle local file URL
336
- let fileURL = URL(fileURLWithPath: assetPath)
337
- basePath = fileURL.path
338
- }
339
-
340
- if let basePath = basePath, FileManager.default.fileExists(atPath: basePath) {
341
- if !complex {
342
- let soundFileUrl = URL(fileURLWithPath: basePath)
343
- var soundId = SystemSoundID()
344
- AudioServicesCreateSystemSoundID(soundFileUrl as CFURL, &soundId)
345
- self.audioList[audioId] = NSNumber(value: Int32(soundId))
346
- call.resolve()
347
- } else {
348
- let audioAsset = AudioAsset(
349
- owner: self,
350
- withAssetId: audioId, withPath: basePath, withChannels: channels,
351
- withVolume: volume, withFadeDelay: delay)
352
- self.audioList[audioId] = audioAsset
353
- call.resolve()
354
- }
355
- } else {
356
- if FileManager.default.fileExists(atPath: assetPath) {
357
- // Use the original assetPath
358
- if !complex {
359
- let soundFileUrl = URL(fileURLWithPath: assetPath)
360
- var soundId = SystemSoundID()
361
- AudioServicesCreateSystemSoundID(soundFileUrl as CFURL, &soundId)
362
- self.audioList[audioId] = NSNumber(value: Int32(soundId))
363
- call.resolve()
364
- } else {
365
- let audioAsset = AudioAsset(
366
- owner: self,
367
- withAssetId: audioId, withPath: assetPath, withChannels: channels,
368
- withVolume: volume, withFadeDelay: delay)
369
- self.audioList[audioId] = audioAsset
370
- call.resolve()
371
- }
372
- } else {
373
- let attributes = try? FileManager.default.attributesOfItem(atPath: assetPath)
374
- call.reject(Constant.ErrorAssetPath + " - " + assetPath)
375
- }
376
- }
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))
377
364
  } else {
378
- call.reject(Constant.ErrorAssetAlreadyLoaded + " - " + audioId)
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))
382
+ } else {
383
+ let audioAsset = AudioAsset(
384
+ owner: self,
385
+ withAssetId: audioId, withPath: assetPath, withChannels: channels,
386
+ withVolume: volume, withFadeDelay: delay)
387
+ self.audioList[audioId] = audioAsset
379
388
  }
380
389
  }
390
+ call.resolve()
381
391
  }
382
392
  }
383
393
 
384
394
  private func stopAudio(audioId: String) throws {
385
- if self.audioList.count > 0 {
386
- let asset = self.audioList[audioId]
395
+ let asset = self.audioList[audioId]
387
396
 
388
- if asset != nil {
389
- if asset is AudioAsset {
390
- 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
391
404
 
392
- if self.fadeMusic {
393
- audioAsset?.playWithFade(time: audioAsset?.getCurrentTime() ?? 0)
394
- } else {
395
- audioAsset?.stop()
396
- }
397
- }
398
- } else {
399
- throw MyError.runtimeError(Constant.ErrorAssetNotFound)
400
- }
405
+ if self.fadeMusic {
406
+ audioAsset?.playWithFade(time: audioAsset?.getCurrentTime() ?? 0)
407
+ } else {
408
+ audioAsset?.stop()
401
409
  }
402
410
  }
403
411
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capgo/native-audio",
3
- "version": "6.4.14",
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",