@mcfarljw/capacitor-audio 1.0.1 → 1.0.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.
|
@@ -44,43 +44,47 @@ public class CapacitorAudio {
|
|
|
44
44
|
|
|
45
45
|
public void play(String path, int track) {
|
|
46
46
|
final Context context = this.context;
|
|
47
|
-
final String trimmedPath = path.replaceAll("^/+", "");
|
|
47
|
+
final String trimmedPath = path.trim().replaceAll("^/+", "");
|
|
48
48
|
final String absolutePath = context.getFilesDir().getAbsolutePath() + "/files/" + trimmedPath;
|
|
49
49
|
final String parsedPath = Uri.parse(absolutePath).getPath();
|
|
50
50
|
|
|
51
|
-
|
|
52
|
-
if (!audioTracks.containsKey(track)) {
|
|
53
|
-
audioTracks.put(track, new HashMap<>());
|
|
54
|
-
}
|
|
55
|
-
|
|
56
51
|
this.bridge.getActivity().runOnUiThread(() -> {
|
|
57
|
-
if (audioTracks.
|
|
58
|
-
|
|
59
|
-
}
|
|
60
|
-
final File file = new File(parsedPath);
|
|
61
|
-
int soundId;
|
|
62
|
-
|
|
63
|
-
Log.d(PLUGIN_NAME, "playing file: " + file.getAbsolutePath());
|
|
52
|
+
if (!audioTracks.containsKey(track)) {
|
|
53
|
+
audioTracks.put(track, new HashMap<>());
|
|
54
|
+
}
|
|
64
55
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
audioTracks.get(track).
|
|
56
|
+
try {
|
|
57
|
+
if (audioTracks.get(track).containsKey(trimmedPath)) {
|
|
58
|
+
mSoundPool.play(audioTracks.get(track).get(trimmedPath), mSoundVolume, mSoundVolume, 1, 0, mSoundRate);
|
|
68
59
|
} else {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
audioTracks.get(track).put(trimmedPath, soundId);
|
|
72
|
-
} catch (IOException error) {
|
|
73
|
-
Log.i(PLUGIN_NAME, "public file not found: " + error.getMessage());
|
|
74
|
-
}
|
|
60
|
+
final File file = new File(parsedPath);
|
|
61
|
+
int soundId;
|
|
75
62
|
|
|
76
|
-
|
|
77
|
-
|
|
63
|
+
Log.d(PLUGIN_NAME, "playing file: " + file.getAbsolutePath());
|
|
64
|
+
|
|
65
|
+
if (file.exists()) {
|
|
66
|
+
soundId = mSoundPool.load(file.getPath(), 1);
|
|
78
67
|
audioTracks.get(track).put(trimmedPath, soundId);
|
|
79
|
-
}
|
|
80
|
-
|
|
68
|
+
} else {
|
|
69
|
+
try {
|
|
70
|
+
soundId = mSoundPool.load(context.getAssets().openFd("public/" + trimmedPath), 1);
|
|
71
|
+
audioTracks.get(track).put(trimmedPath, soundId);
|
|
72
|
+
} catch (IOException error) {
|
|
73
|
+
Log.i(PLUGIN_NAME, "public file not found: " + error.getMessage());
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
soundId = mSoundPool.load(context.getAssets().openFd("www/" + trimmedPath), 1);
|
|
78
|
+
audioTracks.get(track).put(trimmedPath, soundId);
|
|
79
|
+
} catch (IOException error) {
|
|
80
|
+
Log.d(PLUGIN_NAME, "www file not found: " + error.getMessage());
|
|
81
|
+
}
|
|
81
82
|
}
|
|
82
83
|
}
|
|
84
|
+
} catch (NullPointerException error) {
|
|
85
|
+
Log.i(PLUGIN_NAME, "Unable to play audio!");
|
|
83
86
|
}
|
|
87
|
+
|
|
84
88
|
});
|
|
85
89
|
}
|
|
86
90
|
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
package com.mcfarljw.capacitor.audio;
|
|
2
2
|
|
|
3
|
+
import androidx.annotation.NonNull;
|
|
4
|
+
|
|
3
5
|
import com.getcapacitor.Plugin;
|
|
4
6
|
import com.getcapacitor.PluginCall;
|
|
5
7
|
import com.getcapacitor.PluginMethod;
|
|
@@ -20,7 +22,9 @@ public class CapacitorAudioPlugin extends Plugin {
|
|
|
20
22
|
String path = call.getString("path");
|
|
21
23
|
int track = call.getInt("track", 0);
|
|
22
24
|
|
|
23
|
-
|
|
25
|
+
if (path != null) {
|
|
26
|
+
implementation.play(path, track);
|
|
27
|
+
}
|
|
24
28
|
|
|
25
29
|
call.resolve();
|
|
26
30
|
}
|
|
@@ -27,7 +27,7 @@ import Foundation
|
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
@objc public func play(_ path: String, _ track: Int) -> Void {
|
|
30
|
-
let trimmedPath = path.trimmingCharacters(in: .whitespaces)
|
|
30
|
+
let trimmedPath = path.trimmingCharacters(in: .whitespaces).trimmingCharacters(in: CharacterSet(charactersIn: "/"))
|
|
31
31
|
let documentPath = "\(self.documentDirectory!)\(trimmedPath)"
|
|
32
32
|
let wwwPath = "\(self.wwwDirectory!)\(trimmedPath)"
|
|
33
33
|
let publicPath = "\(self.publicDirectory!)\(trimmedPath)"
|