@mcfarljw/capacitor-audio 1.1.0 → 1.1.2

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.
@@ -2,12 +2,12 @@ import AVFoundation
2
2
  import Foundation
3
3
 
4
4
  @objc public class CapacitorAudio: NSObject, AVAudioPlayerDelegate {
5
+ private let queue = DispatchQueue(label: "com.capacitor.audio", qos: .userInitiated)
6
+ private var players: [Int: AVAudioPlayer] = [:]
7
+
5
8
  var documentDirectory: String!
6
9
  var wwwDirectory: String!
7
10
  var publicDirectory: String!
8
- var audioPlayer1: AVAudioPlayer!
9
- var audioPlayer2: AVAudioPlayer!
10
- var audioPlayer3: AVAudioPlayer!
11
11
 
12
12
  @objc public func load() {
13
13
  let searchPaths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
@@ -26,13 +26,17 @@ import Foundation
26
26
  }
27
27
  }
28
28
 
29
- @objc public func play(_ path: String, _ track: Int) -> Void {
30
- let trimmedPath = path.trimmingCharacters(in: .whitespaces).trimmingCharacters(in: CharacterSet(charactersIn: "/"))
29
+ @objc public func play(_ path: String, _ track: Int) {
30
+ let trimmedPath = path.trimmingCharacters(in: .whitespaces)
31
+ .trimmingCharacters(in: CharacterSet(charactersIn: "/"))
32
+
31
33
  let documentPath = self.documentDirectory! + "/" + trimmedPath
32
34
  let wwwPath = self.wwwDirectory! + "/" + trimmedPath
33
- let publicPath = self.publicDirectory + "/" + trimmedPath
35
+ let publicPath = self.publicDirectory! + "/" + trimmedPath
34
36
 
35
- DispatchQueue.global(qos: .userInitiated).async {
37
+ queue.async { [weak self] in
38
+ guard let self = self else { return }
39
+
36
40
  var audioUrl: URL?
37
41
 
38
42
  if FileManager.default.fileExists(atPath: publicPath) {
@@ -43,57 +47,44 @@ import Foundation
43
47
  audioUrl = URL(fileURLWithPath: documentPath)
44
48
  }
45
49
 
46
- if let audioUrl = audioUrl {
47
- if track == 0 {
48
- self.audioPlayer1 = try? AVAudioPlayer(contentsOf: audioUrl)
49
- self.audioPlayer1?.delegate = self
50
-
51
- self.audioPlayer1?.play()
52
- } else if track == 1 {
53
- self.audioPlayer2 = try? AVAudioPlayer(contentsOf: audioUrl)
54
- self.audioPlayer2?.delegate = self
55
-
56
- self.audioPlayer2?.play()
57
- } else if track == 2 {
58
- self.audioPlayer3 = try? AVAudioPlayer(contentsOf: audioUrl)
59
- self.audioPlayer3?.delegate = self
60
-
61
- self.audioPlayer3?.play()
62
- } else {
63
- self.audioPlayer1 = try? AVAudioPlayer(contentsOf: audioUrl)
64
- self.audioPlayer1?.delegate = self
65
-
66
- self.audioPlayer1?.play()
67
- }
50
+ guard let audioUrl = audioUrl else { return }
51
+
52
+ do {
53
+ let player = try AVAudioPlayer(contentsOf: audioUrl)
54
+
55
+ player.delegate = self
56
+ self.players[track] = player
57
+
58
+ player.play()
59
+ } catch {
60
+ print("Error creating audio player: \(error)")
68
61
  }
69
62
  }
70
-
71
- return
72
63
  }
73
64
 
74
- @objc public func stop(_ track: Int) -> Void {
75
- DispatchQueue.global(qos: .userInitiated).async {
76
- if track == 0 {
77
- self.audioPlayer1?.stop()
78
- } else if track == 1 {
79
- self.audioPlayer2?.stop()
80
- } else if track == 2 {
81
- self.audioPlayer3?.stop()
82
- } else {
83
- self.audioPlayer1?.stop()
65
+ @objc public func stop(_ track: Int) {
66
+ queue.async { [weak self] in
67
+ self?.players[track]?.stop()
68
+ self?.players.removeValue(forKey: track)
69
+ }
70
+ }
71
+
72
+ @objc public func stopAll() {
73
+ queue.async { [weak self] in
74
+ self?.players.values.forEach { player in
75
+ player.stop()
84
76
  }
77
+ self?.players.removeAll()
85
78
  }
86
-
87
- return
88
79
  }
89
80
 
90
- @objc public func stopAll() -> Void {
91
- DispatchQueue.global(qos: .userInitiated).async {
92
- self.audioPlayer1?.stop()
93
- self.audioPlayer2?.stop()
94
- self.audioPlayer3?.stop()
81
+ public func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
82
+ queue.async { [weak self] in
83
+ guard let self = self else { return }
84
+
85
+ if let index = self.players.firstIndex(where: { $0.value === player }) {
86
+ self.players.removeValue(forKey: self.players[index].key)
87
+ }
95
88
  }
96
-
97
- return
98
89
  }
99
- }
90
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mcfarljw/capacitor-audio",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "description": "A native audio plugin for Ionic Capacitor.",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",
@@ -62,7 +62,7 @@
62
62
  "typescript": "~4.1.5"
63
63
  },
64
64
  "peerDependencies": {
65
- "@capacitor/core": "^5.0.0"
65
+ "@capacitor/core": "^6.0.0"
66
66
  },
67
67
  "prettier": "@ionic/prettier-config",
68
68
  "swiftlint": "@ionic/swiftlint-config",