@cometchat/chat-uikit-react-native 5.5.2 → 5.5.4
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/android/.gradle/7.4/checksums/checksums.lock +0 -0
- package/android/.gradle/7.4/fileHashes/fileHashes.lock +0 -0
- package/android/build.gradle +17 -0
- package/android/src/main/java/com/reactnativecometchatuikit/RichTextEditorView.kt +265 -4
- package/android/src/main/java/com/reactnativecometchatuikit/SoundPlayer.java +209 -119
- package/ios/RichTextEditorView.swift +321 -24
- package/ios/SoundPlayer.m +152 -6
- package/package.json +4 -1
- package/react-native-cometchat-ui-kit.podspec +1 -1
- package/src/CometChatCompactMessageComposer/CometChatCompactMessageComposer.tsx +43 -24
- package/src/CometChatMessageComposer/CometChatMessageComposer.tsx +27 -19
- package/src/CometChatMessageList/CometChatMessageList.tsx +60 -25
- package/src/extensions/LinkPreview/LinkPreviewBubble.tsx +48 -47
|
@@ -1,8 +1,16 @@
|
|
|
1
1
|
package com.reactnativecometchatuikit;
|
|
2
2
|
|
|
3
|
-
import android.media.AudioManager;
|
|
4
3
|
import android.media.MediaPlayer;
|
|
5
4
|
import android.net.Uri;
|
|
5
|
+
import android.os.Handler;
|
|
6
|
+
import android.os.HandlerThread;
|
|
7
|
+
|
|
8
|
+
import androidx.media3.common.AudioAttributes;
|
|
9
|
+
import androidx.media3.common.C;
|
|
10
|
+
import androidx.media3.common.MediaItem;
|
|
11
|
+
import androidx.media3.common.PlaybackException;
|
|
12
|
+
import androidx.media3.common.Player;
|
|
13
|
+
import androidx.media3.exoplayer.ExoPlayer;
|
|
6
14
|
|
|
7
15
|
import androidx.annotation.NonNull;
|
|
8
16
|
|
|
@@ -17,7 +25,15 @@ import com.facebook.react.modules.core.DeviceEventManagerModule;
|
|
|
17
25
|
import org.json.JSONException;
|
|
18
26
|
import org.json.JSONObject;
|
|
19
27
|
|
|
28
|
+
import java.io.ByteArrayOutputStream;
|
|
20
29
|
import java.io.IOException;
|
|
30
|
+
import java.io.InputStream;
|
|
31
|
+
import java.net.URL;
|
|
32
|
+
import java.net.URLConnection;
|
|
33
|
+
import java.util.Map;
|
|
34
|
+
import java.util.concurrent.ConcurrentHashMap;
|
|
35
|
+
import java.util.concurrent.ExecutorService;
|
|
36
|
+
import java.util.concurrent.Executors;
|
|
21
37
|
|
|
22
38
|
// NOTE: every MediaPlayer call below is wrapped so a bad player state can never crash the RN app.
|
|
23
39
|
// The player is a single shared instance (WhatsApp-style, one clip at a time), so with several audio
|
|
@@ -26,12 +42,8 @@ import java.io.IOException;
|
|
|
26
42
|
// (or NPE when null) for a method invoked in the wrong state — those are RuntimeExceptions that would
|
|
27
43
|
// otherwise red-screen. We catch broadly and degrade gracefully (no-op / report an error) instead.
|
|
28
44
|
public class SoundPlayer extends ReactContextBaseJavaModule {
|
|
29
|
-
MediaPlayer mediaPlayer = null;
|
|
30
|
-
JSONObject obj = new JSONObject();
|
|
31
45
|
public static final String MODULE_NAME = "SoundPlayer";
|
|
32
46
|
public static final String EVENT_NAME = "soundPlayStatus";
|
|
33
|
-
private static String currentUrl = "";
|
|
34
|
-
private static String prevUrl = "";
|
|
35
47
|
DeviceEventManagerModule.RCTDeviceEventEmitter eventEmitter = null;
|
|
36
48
|
|
|
37
49
|
SoundPlayer(ReactApplicationContext context) {
|
|
@@ -45,159 +57,237 @@ public class SoundPlayer extends ReactContextBaseJavaModule {
|
|
|
45
57
|
eventEmitter.emit(EVENT_NAME, params);
|
|
46
58
|
}
|
|
47
59
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
60
|
+
// Seconds. Browser-recorded WebM (the web UI Kit's voice notes) has no duration header, so
|
|
61
|
+
// MediaPlayer reports 0/-1. Fall back to the timestamps in the file itself: the last cluster's
|
|
62
|
+
// timecode plus its last block's offset. MediaExtractor isn't enough — some vendor extractors
|
|
63
|
+
// (Xiaomi's MMParserExtractor) stop after the first cluster of a MediaRecorder WebM, so a 3 s
|
|
64
|
+
// note came back as 0.24 s → 00:00.
|
|
65
|
+
private static final int PROBE_TIMEOUT_MS = 10_000;
|
|
66
|
+
private static final int PROBE_MAX_BYTES = 10 * 1024 * 1024; // ponytail: voice notes are KBs; bigger files skip the fallback
|
|
67
|
+
// Keyed by the URL without its query: signed media URLs change their signature on every fetch.
|
|
68
|
+
private static final Map<String, Integer> durationCache = new ConcurrentHashMap<>();
|
|
69
|
+
// Probes (network + MediaPlayer.create) run here, never on the shared native-modules thread.
|
|
70
|
+
private static final ExecutorService probes = Executors.newSingleThreadExecutor();
|
|
52
71
|
|
|
53
|
-
|
|
72
|
+
private int durationSeconds(MediaPlayer mp, String url) {
|
|
73
|
+
int ms = mp.getDuration();
|
|
74
|
+
if (ms > 0) return ms / 1000;
|
|
75
|
+
// Remote URLs only: a bare local path throws MalformedURLException below and returns 0, which
|
|
76
|
+
// is fine because local recordings carry a real duration header.
|
|
77
|
+
String key = url.split("\\?", 2)[0];
|
|
78
|
+
Integer cached = durationCache.get(key);
|
|
79
|
+
if (cached != null) return cached;
|
|
54
80
|
try {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
81
|
+
URLConnection c = new URL(url).openConnection();
|
|
82
|
+
c.setConnectTimeout(PROBE_TIMEOUT_MS);
|
|
83
|
+
c.setReadTimeout(PROBE_TIMEOUT_MS);
|
|
84
|
+
try (InputStream in = c.getInputStream()) {
|
|
85
|
+
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
|
86
|
+
byte[] buf = new byte[16384];
|
|
87
|
+
for (int n; (n = in.read(buf)) > 0; ) {
|
|
88
|
+
bytes.write(buf, 0, n);
|
|
89
|
+
if (bytes.size() > PROBE_MAX_BYTES) return 0;
|
|
90
|
+
}
|
|
91
|
+
int seconds = (int) Math.round(webmDurationMs(bytes.toByteArray()) / 1000.0);
|
|
92
|
+
if (seconds > 0) durationCache.put(key, seconds);
|
|
93
|
+
return seconds;
|
|
94
|
+
}
|
|
61
95
|
} catch (Exception e) {
|
|
62
|
-
|
|
63
|
-
e.printStackTrace();
|
|
64
|
-
return;
|
|
96
|
+
return 0;
|
|
65
97
|
}
|
|
66
|
-
resolve.invoke(obj.toString());
|
|
67
98
|
}
|
|
68
99
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
100
|
+
// ponytail: walks clusters only and assumes the default 1 ms TimecodeScale, which is what
|
|
101
|
+
// browser MediaRecorder writes. Parse TimecodeScale (0x2AD7B1) if another encoder needs it.
|
|
102
|
+
static long webmDurationMs(byte[] d) {
|
|
103
|
+
long end = 0;
|
|
104
|
+
for (int i = 0; i + 4 <= d.length; i++) {
|
|
105
|
+
if ((d[i] & 0xFF) != 0x1F || (d[i + 1] & 0xFF) != 0x43 || (d[i + 2] & 0xFF) != 0xB6 || (d[i + 3] & 0xFF) != 0x75) continue;
|
|
106
|
+
int p = i + 4;
|
|
107
|
+
p += vintLength(d, p); // cluster size, often "unknown" for MediaRecorder
|
|
108
|
+
long clusterTc = 0;
|
|
109
|
+
while (p < d.length) {
|
|
110
|
+
int id = d[p] & 0xFF;
|
|
111
|
+
if (id != 0xE7 && id != 0xA3 && id != 0xA0) break; // Timecode, SimpleBlock, BlockGroup
|
|
112
|
+
int sizeLen = vintLength(d, p + 1);
|
|
113
|
+
long size = vintValue(d, p + 1, sizeLen);
|
|
114
|
+
int body = p + 1 + sizeLen;
|
|
115
|
+
if (size < 0 || body + size > d.length) break;
|
|
116
|
+
if (id == 0xE7) {
|
|
117
|
+
clusterTc = 0;
|
|
118
|
+
for (int k = 0; k < size; k++) clusterTc = (clusterTc << 8) | (d[body + k] & 0xFF);
|
|
119
|
+
} else if (id == 0xA3) {
|
|
120
|
+
int tcAt = body + vintLength(d, body); // skip track number
|
|
121
|
+
short rel = (short) (((d[tcAt] & 0xFF) << 8) | (d[tcAt + 1] & 0xFF));
|
|
122
|
+
end = Math.max(end, clusterTc + rel);
|
|
123
|
+
}
|
|
124
|
+
p = (int) (body + size);
|
|
125
|
+
}
|
|
126
|
+
end = Math.max(end, clusterTc);
|
|
74
127
|
}
|
|
128
|
+
return end;
|
|
129
|
+
}
|
|
75
130
|
|
|
76
|
-
|
|
77
|
-
|
|
131
|
+
private static int vintLength(byte[] d, int p) {
|
|
132
|
+
int first = d[p] & 0xFF;
|
|
133
|
+
for (int len = 1; len <= 8; len++) if ((first & (0x80 >> (len - 1))) != 0) return len;
|
|
134
|
+
return 8;
|
|
135
|
+
}
|
|
78
136
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
137
|
+
private static long vintValue(byte[] d, int p, int len) {
|
|
138
|
+
long v = d[p] & (0xFF >> len);
|
|
139
|
+
for (int k = 1; k < len; k++) v = (v << 8) | (d[p + k] & 0xFF);
|
|
140
|
+
return v;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
@ReactMethod
|
|
144
|
+
public void prepareMediaPlayer(String url, Callback resolve) {
|
|
145
|
+
eventEmitter = getReactApplicationContext().getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class);
|
|
146
|
+
probes.execute(() -> {
|
|
147
|
+
// Only reads the duration; playback uses its own ExoPlayer, so the probe is released here.
|
|
148
|
+
MediaPlayer probe = null;
|
|
149
|
+
try {
|
|
150
|
+
probe = MediaPlayer.create(getReactApplicationContext(), Uri.parse(url));
|
|
151
|
+
if (probe == null) return;
|
|
152
|
+
JSONObject o = new JSONObject();
|
|
153
|
+
o.put("duration", durationSeconds(probe, url));
|
|
154
|
+
resolve.invoke(o.toString());
|
|
155
|
+
} catch (Exception e) {
|
|
156
|
+
// create() / getDuration() in a bad state → don't crash.
|
|
157
|
+
e.printStackTrace();
|
|
158
|
+
} finally {
|
|
159
|
+
if (probe != null) probe.release();
|
|
86
160
|
}
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// Playback runs on ExoPlayer, not MediaPlayer: MediaPlayer always goes through the device's
|
|
165
|
+
// extractor, and some vendor ones (Xiaomi's MMParserExtractor) stop after the first cluster of
|
|
166
|
+
// a browser WebM, so a web voice note played ~0.4 s and ended. ExoPlayer ships its own Matroska
|
|
167
|
+
// extractor. It must be touched only on its looper thread, so every call below is posted there.
|
|
168
|
+
private static HandlerThread exoThread;
|
|
169
|
+
private static Handler exo;
|
|
170
|
+
private ExoPlayer player = null;
|
|
171
|
+
private String playingUrl = "";
|
|
172
|
+
|
|
173
|
+
private void onExo(Runnable r) {
|
|
174
|
+
if (exoThread == null) {
|
|
175
|
+
exoThread = new HandlerThread("CometChatSoundPlayer");
|
|
176
|
+
exoThread.start();
|
|
177
|
+
exo = new Handler(exoThread.getLooper());
|
|
178
|
+
}
|
|
179
|
+
exo.post(() -> {
|
|
180
|
+
try { r.run(); } catch (Exception e) { e.printStackTrace(); } // never crash the app
|
|
181
|
+
});
|
|
182
|
+
}
|
|
87
183
|
|
|
88
|
-
|
|
184
|
+
private void releasePlayer() { // exo thread only
|
|
185
|
+
if (player != null) { player.release(); player = null; }
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
@ReactMethod
|
|
189
|
+
public void play(String url, Callback resolve) {
|
|
190
|
+
eventEmitter = getReactApplicationContext().getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class);
|
|
191
|
+
onExo(() -> {
|
|
192
|
+
if (player != null && !playingUrl.isEmpty()) emitEvent("complete", playingUrl); // replaced clip
|
|
193
|
+
releasePlayer();
|
|
194
|
+
ExoPlayer p = new ExoPlayer.Builder(getReactApplicationContext()).setLooper(exoThread.getLooper()).build();
|
|
195
|
+
p.setAudioAttributes(new AudioAttributes.Builder()
|
|
196
|
+
.setUsage(C.USAGE_MEDIA).setContentType(C.AUDIO_CONTENT_TYPE_MUSIC).build(), false);
|
|
197
|
+
boolean[] answered = {false}; // a Callback may be invoked only once
|
|
198
|
+
p.addListener(new Player.Listener() {
|
|
89
199
|
@Override
|
|
90
|
-
public void
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
200
|
+
public void onPlaybackStateChanged(int state) {
|
|
201
|
+
if (state == Player.STATE_READY && !answered[0]) {
|
|
202
|
+
answered[0] = true;
|
|
203
|
+
try {
|
|
204
|
+
JSONObject o = new JSONObject();
|
|
205
|
+
o.put("success", true);
|
|
206
|
+
o.put("url", url);
|
|
207
|
+
resolve.invoke(o.toString());
|
|
208
|
+
} catch (JSONException e) {
|
|
209
|
+
resolve.invoke("Error", e.getMessage());
|
|
210
|
+
}
|
|
211
|
+
} else if (state == Player.STATE_ENDED) {
|
|
212
|
+
emitEvent("complete", url);
|
|
213
|
+
if (playingUrl.equals(url)) playingUrl = "";
|
|
94
214
|
}
|
|
95
215
|
}
|
|
96
|
-
});
|
|
97
216
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
}
|
|
217
|
+
@Override
|
|
218
|
+
public void onPlayerError(@NonNull PlaybackException error) {
|
|
219
|
+
// 404, expired signed URL, unsupported file: free the player and reset the bubble.
|
|
220
|
+
if (!answered[0]) {
|
|
221
|
+
answered[0] = true;
|
|
222
|
+
resolve.invoke("Error", error.getMessage());
|
|
223
|
+
}
|
|
224
|
+
emitEvent("complete", url);
|
|
225
|
+
if (player == p) { releasePlayer(); playingUrl = ""; }
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
p.setMediaItem(MediaItem.fromUri(url));
|
|
229
|
+
p.prepare();
|
|
230
|
+
p.play();
|
|
231
|
+
player = p;
|
|
232
|
+
playingUrl = url;
|
|
233
|
+
});
|
|
115
234
|
}
|
|
116
235
|
|
|
117
236
|
@ReactMethod
|
|
118
237
|
public void resume() {
|
|
119
|
-
|
|
120
|
-
if (mediaPlayer != null) {
|
|
121
|
-
mediaPlayer.start();
|
|
122
|
-
}
|
|
123
|
-
} catch (Exception e) {
|
|
124
|
-
// start() on a released/error/idle player throws IllegalStateException — ignore.
|
|
125
|
-
e.printStackTrace();
|
|
126
|
-
}
|
|
238
|
+
onExo(() -> { if (player != null) player.play(); });
|
|
127
239
|
}
|
|
128
240
|
|
|
129
241
|
@ReactMethod
|
|
130
242
|
public void playAt(int atTime, Callback resolve) {
|
|
131
|
-
|
|
132
|
-
if (
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
mediaPlayer.seekTo(atTime * 1000);
|
|
137
|
-
} catch (Exception e) {
|
|
138
|
-
// seekTo() in an invalid state throws IllegalStateException — report, don't crash.
|
|
139
|
-
resolve.invoke("Error", e.getMessage());
|
|
140
|
-
return;
|
|
141
|
-
}
|
|
142
|
-
obj = new JSONObject();
|
|
143
|
-
resolve.invoke(obj.toString());
|
|
243
|
+
onExo(() -> {
|
|
244
|
+
if (player == null) { resolve.invoke("Error", "MediaPlayer not prepared."); return; }
|
|
245
|
+
player.seekTo(atTime * 1000L);
|
|
246
|
+
resolve.invoke(new JSONObject().toString());
|
|
247
|
+
});
|
|
144
248
|
}
|
|
145
249
|
|
|
146
250
|
@ReactMethod
|
|
147
251
|
public void pause(Callback resolve) {
|
|
148
|
-
|
|
149
|
-
return;
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
}
|
|
252
|
+
onExo(() -> {
|
|
253
|
+
if (player == null) return;
|
|
254
|
+
player.pause();
|
|
255
|
+
try {
|
|
256
|
+
JSONObject o = new JSONObject();
|
|
257
|
+
o.put("success", true);
|
|
258
|
+
resolve.invoke(o.toString());
|
|
259
|
+
} catch (JSONException e) {
|
|
260
|
+
resolve.invoke(new JSONObject().toString());
|
|
261
|
+
}
|
|
262
|
+
});
|
|
159
263
|
}
|
|
160
264
|
|
|
161
265
|
@ReactMethod
|
|
162
266
|
public void stop() {
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
} catch (Exception e) {
|
|
169
|
-
// stop() from Idle/Error state throws IllegalStateException — ignore, never crash.
|
|
170
|
-
e.printStackTrace();
|
|
171
|
-
}
|
|
267
|
+
onExo(() -> {
|
|
268
|
+
if (!playingUrl.isEmpty()) emitEvent("complete", playingUrl);
|
|
269
|
+
releasePlayer();
|
|
270
|
+
playingUrl = "";
|
|
271
|
+
});
|
|
172
272
|
}
|
|
173
273
|
|
|
174
274
|
@ReactMethod
|
|
175
275
|
public void releaseMediaPlayer() {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
} catch (Exception e) {
|
|
181
|
-
e.printStackTrace();
|
|
182
|
-
}
|
|
183
|
-
// Null the reference so later calls (getPosition polls, another card's release) see a null
|
|
184
|
-
// player and take the safe path instead of touching a released one (IllegalStateException).
|
|
185
|
-
mediaPlayer = null;
|
|
276
|
+
// Every audio card calls this on unmount, without saying which clip it is, so it must not
|
|
277
|
+
// stop the shared ExoPlayer: scrolling another card out of view would cut the playing clip
|
|
278
|
+
// and leave its card stuck on "playing". The duration probe releases itself, so there is
|
|
279
|
+
// nothing left to free here. Kept for the JS API.
|
|
186
280
|
}
|
|
187
281
|
|
|
188
282
|
@ReactMethod
|
|
189
283
|
public void getPosition(Callback resolve) {
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
} catch (Exception e) {
|
|
198
|
-
try { obj.put("position", 0); } catch (JSONException ignored) {}
|
|
199
|
-
}
|
|
200
|
-
resolve.invoke(obj.toString());
|
|
284
|
+
onExo(() -> {
|
|
285
|
+
try {
|
|
286
|
+
JSONObject o = new JSONObject();
|
|
287
|
+
o.put("position", player != null ? player.getCurrentPosition() / 1000 : 0);
|
|
288
|
+
resolve.invoke(o.toString());
|
|
289
|
+
} catch (JSONException ignored) {}
|
|
290
|
+
});
|
|
201
291
|
}
|
|
202
292
|
|
|
203
293
|
@NonNull
|