@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.
package/ios/Plugin/Plugin.swift
CHANGED
@@ -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
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
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
|
176
|
-
|
177
|
-
|
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
|
-
|
182
|
-
|
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
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
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
|
299
|
-
|
327
|
+
if audioList.isEmpty {
|
328
|
+
audioList = [:]
|
329
|
+
}
|
300
330
|
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
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
|
-
|
307
|
-
|
308
|
-
|
309
|
-
isLocalUrl = false
|
353
|
+
// Handle local file URL
|
354
|
+
let fileURL = URL(fileURLWithPath: assetPath)
|
355
|
+
basePath = fileURL.path
|
310
356
|
}
|
311
357
|
|
312
|
-
if
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
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
|
-
|
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
|
-
|
385
|
-
let asset = self.audioList[audioId]
|
395
|
+
let asset = self.audioList[audioId]
|
386
396
|
|
387
|
-
|
388
|
-
|
389
|
-
|
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
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
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
|
}
|