@capgo/native-audio 6.2.0 → 6.2.2
Sign up to get free protection for your applications and to get access to all the features.
package/ios/Plugin/Plugin.swift
CHANGED
@@ -10,7 +10,7 @@ enum MyError: Error {
|
|
10
10
|
/// Please read the Capacitor iOS Plugin Development Guide
|
11
11
|
/// here: https://capacitor.ionicframework.com/docs/plugins/ios
|
12
12
|
@objc(NativeAudio)
|
13
|
-
public class NativeAudio: CAPPlugin {
|
13
|
+
public class NativeAudio: CAPPlugin, AVAudioPlayerDelegate {
|
14
14
|
|
15
15
|
var audioList: [String: Any] = [:]
|
16
16
|
var fadeMusic = false
|
@@ -101,6 +101,26 @@ public class NativeAudio: CAPPlugin {
|
|
101
101
|
preloadAsset(call, isComplex: true)
|
102
102
|
}
|
103
103
|
|
104
|
+
func activateSession() {
|
105
|
+
do {
|
106
|
+
try self.session.setActive(true)
|
107
|
+
} catch {
|
108
|
+
print("Failed to set session active")
|
109
|
+
}
|
110
|
+
}
|
111
|
+
|
112
|
+
func endSession() {
|
113
|
+
do {
|
114
|
+
try self.session.setActive(false, options: .notifyOthersOnDeactivation)
|
115
|
+
} catch {
|
116
|
+
print("Failed to deactivate audio session")
|
117
|
+
}
|
118
|
+
}
|
119
|
+
|
120
|
+
public func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
|
121
|
+
self.endSession()
|
122
|
+
}
|
123
|
+
|
104
124
|
@objc func play(_ call: CAPPluginCall) {
|
105
125
|
let audioId = call.getString(Constant.AssetIdKey) ?? ""
|
106
126
|
let time = call.getDouble("time") ?? 0
|
@@ -115,7 +135,7 @@ public class NativeAudio: CAPPlugin {
|
|
115
135
|
if asset != nil {
|
116
136
|
if asset is AudioAsset {
|
117
137
|
let audioAsset = asset as? AudioAsset
|
118
|
-
|
138
|
+
self.activateSession()
|
119
139
|
if self.fadeMusic {
|
120
140
|
audioAsset?.playWithFade(time: time)
|
121
141
|
} else {
|
@@ -124,6 +144,7 @@ public class NativeAudio: CAPPlugin {
|
|
124
144
|
call.resolve()
|
125
145
|
} else if asset is Int32 {
|
126
146
|
let audioAsset = asset as? NSNumber ?? 0
|
147
|
+
self.activateSession()
|
127
148
|
AudioServicesPlaySystemSound(SystemSoundID(audioAsset.intValue))
|
128
149
|
call.resolve()
|
129
150
|
} else {
|
@@ -175,7 +196,7 @@ public class NativeAudio: CAPPlugin {
|
|
175
196
|
guard let audioAsset: AudioAsset = self.getAudioAsset(call) else {
|
176
197
|
return
|
177
198
|
}
|
178
|
-
|
199
|
+
self.activateSession()
|
179
200
|
audioAsset.resume()
|
180
201
|
call.resolve()
|
181
202
|
}
|
@@ -186,6 +207,7 @@ public class NativeAudio: CAPPlugin {
|
|
186
207
|
}
|
187
208
|
|
188
209
|
audioAsset.pause()
|
210
|
+
self.endSession()
|
189
211
|
call.resolve()
|
190
212
|
}
|
191
213
|
|
@@ -194,6 +216,7 @@ public class NativeAudio: CAPPlugin {
|
|
194
216
|
|
195
217
|
do {
|
196
218
|
try stopAudio(audioId: audioId)
|
219
|
+
self.endSession()
|
197
220
|
} catch {
|
198
221
|
call.reject(Constant.ErrorAssetNotFound)
|
199
222
|
}
|