@cometchat/chat-uikit-react-native 5.5.3 → 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/fileChanges/last-build.bin +0 -0
- package/android/.gradle/7.4/fileHashes/fileHashes.lock +0 -0
- package/android/.gradle/7.4/gc.properties +0 -0
- package/android/.gradle/vcs-1/gc.properties +0 -0
- package/android/build.gradle +5 -0
- package/android/src/main/java/com/reactnativecometchatuikit/RichTextEditorView.kt +47 -4
- package/android/src/main/java/com/reactnativecometchatuikit/SoundPlayer.java +209 -119
- package/ios/RichTextEditorView.swift +96 -1
- package/ios/SoundPlayer.m +152 -6
- package/package.json +1 -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
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
File without changes
|
|
File without changes
|
package/android/build.gradle
CHANGED
|
@@ -72,6 +72,11 @@ dependencies {
|
|
|
72
72
|
implementation "com.facebook.react:react-android"
|
|
73
73
|
implementation 'androidx.appcompat:appcompat:1.7.1'
|
|
74
74
|
implementation 'com.google.android.material:material:1.12.0'
|
|
75
|
+
// Voice-note playback: ExoPlayer's own WebM extractor (vendor MediaPlayer extractors truncate web notes).
|
|
76
|
+
// Host apps can pin the version (same key react-native-video reads), e.g. to keep compileSdk 34:
|
|
77
|
+
// ext { RNVideo_media3Version = "1.4.1" } — media3 1.8.0 needs compileSdk 35.
|
|
78
|
+
def media3Version = rootProject.ext.has("RNVideo_media3Version") ? rootProject.ext.get("RNVideo_media3Version") : "1.8.0"
|
|
79
|
+
implementation "androidx.media3:media3-exoplayer:${media3Version}"
|
|
75
80
|
|
|
76
81
|
// Unit tests for the native editor. Robolectric runs real Android framework code off-device,
|
|
77
82
|
// which is the only way to assert on spans: the inline-style rules live in a TextWatcher and
|
|
@@ -165,6 +165,8 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
165
165
|
private var isInitialized = false
|
|
166
166
|
|
|
167
167
|
private var previousText: String = ""
|
|
168
|
+
// Text length at the last onSelectionChanged, for telling typing apart from a caret move.
|
|
169
|
+
private var lastSelectionTextLength = 0
|
|
168
170
|
private var pendingDelta: Map<String, Any>? = null
|
|
169
171
|
private var pendingPrefixDeletion: Pair<Int, Int>? = null // (lineStart, prefixLength) for backspace-in-prefix
|
|
170
172
|
// Saved selected text for URL-paste-over-selection detection in TextWatcher
|
|
@@ -548,6 +550,25 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
548
550
|
inheritedStyles = null
|
|
549
551
|
}
|
|
550
552
|
|
|
553
|
+
// A colour the user switched off at the caret must stay off for what they type
|
|
554
|
+
// next — including inside an existing coloured run. A span that covers the
|
|
555
|
+
// caret on both sides grows over inserted text on its own, whatever its flags,
|
|
556
|
+
// so the clear was ignored the moment the caret sat mid-word: tap into red
|
|
557
|
+
// text, clear, type, and the new text came out red (ENG-40922, found by
|
|
558
|
+
// InlineColourFuzzTest). Strip the colour from the inserted range explicitly.
|
|
559
|
+
if (addedCount > 0 && s != null && changeStart + addedCount <= s.length) {
|
|
560
|
+
val start = changeStart
|
|
561
|
+
val end = changeStart + addedCount
|
|
562
|
+
isInternalChange = true
|
|
563
|
+
if (explicitlyOffStyles.contains("externalColor")) {
|
|
564
|
+
clearInlineStyleSpans(s, "color", start, end)
|
|
565
|
+
}
|
|
566
|
+
if (explicitlyOffStyles.contains("externalBackgroundColor")) {
|
|
567
|
+
clearInlineStyleSpans(s, "backgroundColor", start, end)
|
|
568
|
+
}
|
|
569
|
+
isInternalChange = false
|
|
570
|
+
}
|
|
571
|
+
|
|
551
572
|
// Apply pending styles to newly typed text
|
|
552
573
|
if (addedCount > 0 && removedCount == 0 && s != null) {
|
|
553
574
|
val spanStart = changeStart
|
|
@@ -880,6 +901,14 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
880
901
|
s.getSpans(0, 0, StrikethroughSpan::class.java).forEach { s.removeSpan(it) }
|
|
881
902
|
s.getSpans(0, 0, InlineCodeSpan::class.java).forEach { s.removeSpan(it) }
|
|
882
903
|
s.getSpans(0, 0, ForegroundColorSpan::class.java).forEach { s.removeSpan(it) }
|
|
904
|
+
// ExternalForegroundColorSpan is not a ForegroundColorSpan, so the line above
|
|
905
|
+
// missed it: delete everything and the next character came back in the old
|
|
906
|
+
// colour, while iOS correctly started plain (ENG-40922, found by
|
|
907
|
+
// InlineColourFuzzTest).
|
|
908
|
+
s.getSpans(0, 0, ExternalForegroundColorSpan::class.java).forEach { s.removeSpan(it) }
|
|
909
|
+
s.getSpans(0, 0, ExternalBackgroundColorSpan::class.java).forEach { s.removeSpan(it) }
|
|
910
|
+
pendingExternalColor = null
|
|
911
|
+
pendingExternalBackgroundColor = null
|
|
883
912
|
s.getSpans(0, 0, AbsoluteSizeSpan::class.java).forEach { s.removeSpan(it) }
|
|
884
913
|
s.getSpans(0, 0, CodeBlockBorderSpan::class.java).forEach { s.removeSpan(it) }
|
|
885
914
|
s.getSpans(0, 0, TypefaceSpan::class.java).forEach { s.removeSpan(it) }
|
|
@@ -1387,13 +1416,27 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
1387
1416
|
// Skip if not initialized yet (during construction)
|
|
1388
1417
|
if (!isInitialized) return
|
|
1389
1418
|
|
|
1419
|
+
// How many characters landed since the last selection change, measured against the length
|
|
1420
|
+
// recorded here rather than previousText. A keyboard (Gboard, composing mid-word) can run
|
|
1421
|
+
// afterTextChanged — which refreshes previousText — before this callback, so measuring
|
|
1422
|
+
// against previousText read every keystroke as "nothing typed" and disarmed the colour on
|
|
1423
|
+
// the first character (ENG-40922). Tracked unconditionally, as iOS does (ENG-38248).
|
|
1424
|
+
val currentLen = text?.length ?: 0
|
|
1425
|
+
val insertedCount = currentLen - lastSelectionTextLength
|
|
1426
|
+
lastSelectionTextLength = currentLen
|
|
1427
|
+
|
|
1390
1428
|
// Clear pending/explicitly-off styles when cursor moves WITHOUT text changing
|
|
1391
1429
|
// (i.e., user tapped somewhere else or used arrow keys)
|
|
1392
1430
|
if ((pendingStyles.isNotEmpty() || explicitlyOffStyles.isNotEmpty()) && pendingStylesInsertPos >= 0) {
|
|
1393
1431
|
if (selStart != pendingStylesInsertPos) {
|
|
1394
|
-
|
|
1395
|
-
//
|
|
1396
|
-
|
|
1432
|
+
// Typing advances the caret by however many characters landed: one for a plain
|
|
1433
|
+
// keystroke, more when the keyboard commits an autocorrection or suggestion (which
|
|
1434
|
+
// "." and space both do). A hardcoded +1 read those as a caret move and disarmed
|
|
1435
|
+
// the colour, so text after a full stop sent plain (ENG-40922). Mirrors iOS.
|
|
1436
|
+
// A backspace at the anchor moves the caret back by exactly what it removed — that is
|
|
1437
|
+
// editing, not a caret jump, so the colour follows it (ENG-40922).
|
|
1438
|
+
val isTypingAdvance = insertedCount != 0 && selStart == pendingStylesInsertPos + insertedCount
|
|
1439
|
+
if (!isTypingAdvance) {
|
|
1397
1440
|
// Preserve "codeBlock" across cursor movements — it's a persistent
|
|
1398
1441
|
// block-level style that should survive until explicitly toggled off
|
|
1399
1442
|
// or exited via triple-Enter. Without this, the clear-and-re-add
|
|
@@ -1408,7 +1451,7 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
1408
1451
|
pendingStylesInsertPos = selStart
|
|
1409
1452
|
}
|
|
1410
1453
|
} else {
|
|
1411
|
-
//
|
|
1454
|
+
// Text was typed at the anchor — follow it, however many characters landed
|
|
1412
1455
|
pendingStylesInsertPos = selStart
|
|
1413
1456
|
}
|
|
1414
1457
|
}
|
|
@@ -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
|