@cometchat/chat-uikit-react-native 5.2.11 → 5.3.1
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/build.gradle +3 -0
- package/android/src/main/java/com/reactnativecometchatuikit/CometchatUiKitPackage.java +4 -1
- package/android/src/main/java/com/reactnativecometchatuikit/FileManager.java +909 -10
- package/android/src/main/java/com/reactnativecometchatuikit/RichTextEditorPackage.kt +24 -0
- package/android/src/main/java/com/reactnativecometchatuikit/RichTextEditorView.kt +5382 -0
- package/android/src/main/java/com/reactnativecometchatuikit/RichTextEditorViewManager.kt +452 -0
- package/ios/FileManager.m +430 -3
- package/ios/RichTextEditorView.swift +5504 -0
- package/ios/RichTextEditorViewManager.m +66 -0
- package/ios/RichTextEditorViewManager.swift +291 -0
- package/ios/ToolbarIcons.swift +736 -0
- package/package.json +1 -1
- package/react-native-cometchat-ui-kit.podspec +1 -1
- package/src/CometChatCompactMessageComposer/CometChatCompactMessageComposer.tsx +4602 -0
- package/src/CometChatCompactMessageComposer/SingleLineTextComposerConfiguration.tsx +568 -0
- package/src/CometChatCompactMessageComposer/heightUtils.ts +204 -0
- package/src/CometChatCompactMessageComposer/index.ts +56 -0
- package/src/CometChatCompactMessageComposer/resources/index.ts +5 -0
- package/src/CometChatCompactMessageComposer/styles.ts +291 -0
- package/src/CometChatConversations/CometChatConversations.tsx +141 -14
- package/src/CometChatGroupMembers/CometChatGroupMembers.tsx +6 -0
- package/src/CometChatMessageComposer/CometChatMessageComposer.tsx +1 -20
- package/src/CometChatMessageComposer/styles.tsx +0 -1
- package/src/CometChatMessageList/CometChatMessageList.tsx +76 -30
- package/src/CometChatRichTextEditor/RichTextEditorViewNativeComponent.ts +105 -0
- package/src/CometChatRichTextEditor/index.tsx +385 -0
- package/src/CometChatRichTextEditor/types.ts +162 -0
- package/src/CometChatSearch/CometChatSearch.tsx +35 -29
- package/src/calls/CallUtils.ts +4 -4
- package/src/index.ts +48 -1
- package/src/shared/CometChatUiKit/CometChatUIKit.ts +2 -0
- package/src/shared/constants/UIKitConstants.ts +12 -1
- package/src/shared/events/ListenerInitializer.ts +6 -0
- package/src/shared/events/messages.ts +2 -0
- package/src/shared/formatters/CometChatMentionsFormatter/CometChatMentionsFormatter.tsx +5 -5
- package/src/shared/formatters/CometChatRichTextFormatter/index.tsx +768 -0
- package/src/shared/formatters/index.ts +3 -0
- package/src/shared/framework/MessageDataSource.tsx +93 -2
- package/src/shared/helper/composerHelpers.ts +553 -0
- package/src/shared/helper/useKeyboard.tsx +7 -17
- package/src/shared/icons/components/code-blocks.tsx +13 -0
- package/src/shared/icons/components/code.tsx +11 -0
- package/src/shared/icons/components/format-bold.tsx +11 -0
- package/src/shared/icons/components/format-italic.tsx +11 -0
- package/src/shared/icons/components/format-list-bulleted.tsx +11 -0
- package/src/shared/icons/components/format-list-numbered.tsx +11 -0
- package/src/shared/icons/components/format-quote.tsx +13 -0
- package/src/shared/icons/components/format-strikethrough.tsx +13 -0
- package/src/shared/icons/components/format-underlined.tsx +11 -0
- package/src/shared/icons/components/smileys-&-people.tsx +1 -1
- package/src/shared/icons/icon-mapping.tsx +18 -0
- package/src/shared/index.ts +54 -2
- package/src/shared/resources/CometChatLocalizeNew/resources/de/translation.json +37 -32
- package/src/shared/resources/CometChatLocalizeNew/resources/en/translation.json +5 -0
- package/src/shared/resources/CometChatLocalizeNew/resources/es/translation.json +17 -12
- package/src/shared/resources/CometChatLocalizeNew/resources/fr/translation.json +505 -500
- package/src/shared/resources/CometChatLocalizeNew/resources/hi/translation.json +18 -13
- package/src/shared/resources/CometChatLocalizeNew/resources/hu/translation.json +17 -12
- package/src/shared/resources/CometChatLocalizeNew/resources/it/translation.json +504 -499
- package/src/shared/resources/CometChatLocalizeNew/resources/ja/translation.json +504 -499
- package/src/shared/resources/CometChatLocalizeNew/resources/ko/translation.json +504 -499
- package/src/shared/resources/CometChatLocalizeNew/resources/lt/translation.json +18 -13
- package/src/shared/resources/CometChatLocalizeNew/resources/ms/translation.json +17 -12
- package/src/shared/resources/CometChatLocalizeNew/resources/nl/translation.json +505 -500
- package/src/shared/resources/CometChatLocalizeNew/resources/pt/translation.json +17 -12
- package/src/shared/resources/CometChatLocalizeNew/resources/ru/translation.json +17 -12
- package/src/shared/resources/CometChatLocalizeNew/resources/sv/translation.json +18 -13
- package/src/shared/resources/CometChatLocalizeNew/resources/tr/translation.json +505 -500
- package/src/shared/resources/CometChatLocalizeNew/resources/zh/translation.json +17 -12
- package/src/shared/resources/CometChatLocalizeNew/resources/zh-tw/translation.json +18 -13
- package/src/shared/utils/CometChatMessagePreview/CometChatMessagePreview.tsx +132 -11
- package/src/shared/utils/CometChatMessagePreview/style.ts +1 -3
- package/src/shared/utils/CommonUtils.ts +18 -1
- package/src/shared/utils/MarkdownUtils.ts +143 -0
- package/src/shared/utils/MentionUtils.ts +37 -0
- package/src/shared/utils/MessageReceiptUtils.ts +5 -4
- package/src/shared/utils/MessageUtils.tsx +5 -3
- package/src/shared/utils/index.ts +5 -0
- package/src/shared/views/CometChatAudioBubble/CometChatAudioBubble.tsx +1 -1
- package/src/shared/views/CometChatInlineAudioRecorder/AudioWaveformVisualizer.tsx +417 -0
- package/src/shared/views/CometChatInlineAudioRecorder/CometChatInlineAudioRecorder.tsx +537 -0
- package/src/shared/views/CometChatInlineAudioRecorder/index.ts +34 -0
- package/src/shared/views/CometChatInlineAudioRecorder/style.ts +155 -0
- package/src/shared/views/CometChatInlineAudioRecorder/types.ts +303 -0
- package/src/shared/views/CometChatInlineAudioRecorder/useAudioRecorder.ts +1336 -0
- package/src/shared/views/CometChatLinkConfirmPopup/CometChatLinkConfirmPopup.tsx +183 -0
- package/src/shared/views/CometChatLinkConfirmPopup/index.ts +13 -0
- package/src/shared/views/CometChatLinkConfirmPopup/style.ts +128 -0
- package/src/shared/views/CometChatList/Header.tsx +2 -1
- package/src/shared/views/CometChatListItem/style.ts +1 -0
- package/src/shared/views/CometChatSendButtonView/CometChatSendButtonView.tsx +32 -29
- package/src/shared/views/CometChatTextBubble/CometChatTextBubble.tsx +64 -16
- package/src/shared/views/index.ts +39 -0
|
@@ -54,6 +54,8 @@ import java.io.FileOutputStream;
|
|
|
54
54
|
import java.io.IOException;
|
|
55
55
|
import java.io.InputStream;
|
|
56
56
|
import java.io.OutputStream;
|
|
57
|
+
import java.io.FileInputStream;
|
|
58
|
+
import java.io.RandomAccessFile;
|
|
57
59
|
import java.net.HttpURLConnection;
|
|
58
60
|
import java.net.URL;
|
|
59
61
|
import java.text.SimpleDateFormat;
|
|
@@ -64,9 +66,11 @@ import java.util.UUID;
|
|
|
64
66
|
import java.util.concurrent.ExecutorService;
|
|
65
67
|
import java.util.concurrent.Executors;
|
|
66
68
|
import org.json.JSONObject;
|
|
69
|
+
import org.json.JSONArray;
|
|
67
70
|
import org.json.JSONException;
|
|
68
71
|
import com.facebook.react.modules.core.DeviceEventManagerModule;
|
|
69
72
|
import com.facebook.react.bridge.Promise;
|
|
73
|
+
import com.facebook.react.bridge.ReadableArray;
|
|
70
74
|
|
|
71
75
|
public class FileManager extends ReactContextBaseJavaModule {
|
|
72
76
|
public static final String NAME = "FileManager";
|
|
@@ -478,6 +482,51 @@ public class FileManager extends ReactContextBaseJavaModule {
|
|
|
478
482
|
private String fileName;
|
|
479
483
|
private Context context;
|
|
480
484
|
private Activity activity;
|
|
485
|
+
private Handler amplitudeHandler;
|
|
486
|
+
private Runnable amplitudeRunnable;
|
|
487
|
+
private boolean isAmplitudePolling = false;
|
|
488
|
+
|
|
489
|
+
// Segment-based recording fields
|
|
490
|
+
private List<String> segmentPaths = new ArrayList<>();
|
|
491
|
+
private int currentSegmentIndex = 0;
|
|
492
|
+
|
|
493
|
+
private void startAmplitudePolling() {
|
|
494
|
+
if (amplitudeHandler == null) {
|
|
495
|
+
amplitudeHandler = new Handler(Looper.getMainLooper());
|
|
496
|
+
}
|
|
497
|
+
isAmplitudePolling = true;
|
|
498
|
+
amplitudeRunnable = new Runnable() {
|
|
499
|
+
@Override
|
|
500
|
+
public void run() {
|
|
501
|
+
if (audioRecorder != null && isAmplitudePolling) {
|
|
502
|
+
try {
|
|
503
|
+
int maxAmplitude = audioRecorder.getMaxAmplitude();
|
|
504
|
+
// Normalize to 0.0-1.0 range (max amplitude is ~32767)
|
|
505
|
+
double normalizedAmplitude = Math.min(1.0, maxAmplitude / 32767.0);
|
|
506
|
+
|
|
507
|
+
// Send event to React Native
|
|
508
|
+
if (eventEmitter == null) {
|
|
509
|
+
eventEmitter = getReactApplicationContext().getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class);
|
|
510
|
+
}
|
|
511
|
+
WritableMap params = Arguments.createMap();
|
|
512
|
+
params.putDouble("amplitude", normalizedAmplitude);
|
|
513
|
+
eventEmitter.emit("audioAmplitude", params);
|
|
514
|
+
} catch (Exception e) {
|
|
515
|
+
Log.e(TAG, "Error getting amplitude: " + e.getMessage());
|
|
516
|
+
}
|
|
517
|
+
amplitudeHandler.postDelayed(this, 100); // Poll every 100ms
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
};
|
|
521
|
+
amplitudeHandler.post(amplitudeRunnable);
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
private void stopAmplitudePolling() {
|
|
525
|
+
isAmplitudePolling = false;
|
|
526
|
+
if (amplitudeHandler != null && amplitudeRunnable != null) {
|
|
527
|
+
amplitudeHandler.removeCallbacks(amplitudeRunnable);
|
|
528
|
+
}
|
|
529
|
+
}
|
|
481
530
|
|
|
482
531
|
@ReactMethod
|
|
483
532
|
public void startRecording(final Callback callback) {
|
|
@@ -514,12 +563,16 @@ public class FileManager extends ReactContextBaseJavaModule {
|
|
|
514
563
|
|
|
515
564
|
audioRecorder.prepare();
|
|
516
565
|
audioRecorder.start();
|
|
566
|
+
|
|
567
|
+
// Start amplitude polling for real-time waveform
|
|
568
|
+
startAmplitudePolling();
|
|
517
569
|
|
|
518
570
|
// Your JS just needs a callback to move to "recording"
|
|
519
571
|
callback.invoke("{\"success\": true}");
|
|
520
572
|
} catch (Exception e) {
|
|
521
573
|
try { if (audioRecorder != null) audioRecorder.release(); } catch (Exception ignore) {}
|
|
522
574
|
audioRecorder = null;
|
|
575
|
+
stopAmplitudePolling();
|
|
523
576
|
callback.invoke("{\"success\": false, \"error\": \"" + e.getMessage() + "\"}");
|
|
524
577
|
}
|
|
525
578
|
}
|
|
@@ -538,16 +591,93 @@ public class FileManager extends ReactContextBaseJavaModule {
|
|
|
538
591
|
|
|
539
592
|
@ReactMethod
|
|
540
593
|
public void playAudio(Callback callback) {
|
|
594
|
+
// Check if fileName is valid
|
|
595
|
+
if (fileName == null || fileName.isEmpty()) {
|
|
596
|
+
Log.e("TAG", "playAudio: fileName is null or empty");
|
|
597
|
+
callback.invoke("{\"success\": false, \"error\": \"No audio file to play\"}");
|
|
598
|
+
return;
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
// Stop any existing playback
|
|
602
|
+
if (audioPlayer != null) {
|
|
603
|
+
try {
|
|
604
|
+
if (audioPlayer.isPlaying()) audioPlayer.stop();
|
|
605
|
+
audioPlayer.release();
|
|
606
|
+
} catch (Exception ignore) {}
|
|
607
|
+
audioPlayer = null;
|
|
608
|
+
}
|
|
609
|
+
|
|
541
610
|
audioPlayer = new MediaPlayer();
|
|
542
611
|
try {
|
|
543
|
-
|
|
544
|
-
|
|
612
|
+
String playPath = fileName;
|
|
613
|
+
|
|
614
|
+
// Handle content:// URIs
|
|
615
|
+
if (playPath.startsWith("content://")) {
|
|
616
|
+
Activity activity = getCurrentActivity();
|
|
617
|
+
if (activity != null) {
|
|
618
|
+
Log.e("TAG", "Playing Audio with content URI: " + playPath);
|
|
619
|
+
audioPlayer.setDataSource(activity, Uri.parse(playPath));
|
|
620
|
+
} else {
|
|
621
|
+
// Fallback: try to extract file path from content URI
|
|
622
|
+
String[] parts = playPath.split("/external_files");
|
|
623
|
+
if (parts.length > 1) {
|
|
624
|
+
playPath = "/storage/emulated/0" + parts[1];
|
|
625
|
+
Log.e("TAG", "Converted to file path: " + playPath);
|
|
626
|
+
audioPlayer.setDataSource(playPath);
|
|
627
|
+
} else {
|
|
628
|
+
throw new IOException("Cannot resolve content URI without activity");
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
} else {
|
|
632
|
+
// Handle file:// URIs
|
|
633
|
+
if (playPath.startsWith("file://")) {
|
|
634
|
+
playPath = playPath.substring(7);
|
|
635
|
+
}
|
|
636
|
+
Log.e("TAG", "Playing Audio: " + playPath);
|
|
637
|
+
audioPlayer.setDataSource(playPath);
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
// Set up completion listener to emit event when playback finishes
|
|
641
|
+
audioPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
|
|
642
|
+
@Override
|
|
643
|
+
public void onCompletion(MediaPlayer mp) {
|
|
644
|
+
// Emit playback complete event to JS
|
|
645
|
+
if (eventEmitter == null) {
|
|
646
|
+
eventEmitter = getReactApplicationContext().getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class);
|
|
647
|
+
}
|
|
648
|
+
WritableMap params = Arguments.createMap();
|
|
649
|
+
params.putString("state", "playbackComplete");
|
|
650
|
+
eventEmitter.emit("status", params);
|
|
651
|
+
}
|
|
652
|
+
});
|
|
653
|
+
|
|
545
654
|
audioPlayer.prepare();
|
|
546
655
|
audioPlayer.start();
|
|
547
|
-
Log.e("TAG", "
|
|
548
|
-
|
|
656
|
+
Log.e("TAG", "Audio playback started");
|
|
657
|
+
|
|
658
|
+
// Build response with file URI
|
|
659
|
+
String fileUri = fileName;
|
|
660
|
+
Activity activity = getCurrentActivity();
|
|
661
|
+
if (activity != null && !fileName.startsWith("content://")) {
|
|
662
|
+
try {
|
|
663
|
+
Uri contentUri = FileProvider.getUriForFile(
|
|
664
|
+
activity,
|
|
665
|
+
activity.getApplicationContext().getPackageName() + ".fileprovider",
|
|
666
|
+
new File(fileName));
|
|
667
|
+
fileUri = contentUri.toString();
|
|
668
|
+
} catch (Exception e) {
|
|
669
|
+
Log.e("TAG", "FileProvider error: " + e.getMessage());
|
|
670
|
+
fileUri = "file://" + fileName;
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
callback.invoke("{\"success\": true, \"file\": \"" + fileUri + "\"}");
|
|
549
675
|
} catch (IOException e) {
|
|
550
|
-
Log.e("TAG", "prepare() failed");
|
|
676
|
+
Log.e("TAG", "prepare() failed: " + e.getMessage());
|
|
677
|
+
callback.invoke("{\"success\": false, \"error\": \"" + e.getMessage() + "\"}");
|
|
678
|
+
} catch (Exception e) {
|
|
679
|
+
Log.e("TAG", "playAudio error: " + e.getMessage());
|
|
680
|
+
callback.invoke("{\"success\": false, \"error\": \"" + e.getMessage() + "\"}");
|
|
551
681
|
}
|
|
552
682
|
}
|
|
553
683
|
|
|
@@ -557,6 +687,7 @@ public class FileManager extends ReactContextBaseJavaModule {
|
|
|
557
687
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
558
688
|
try {
|
|
559
689
|
audioRecorder.pause();
|
|
690
|
+
stopAmplitudePolling();
|
|
560
691
|
promise.resolve("Recording paused successfully");
|
|
561
692
|
} catch (Exception e) {
|
|
562
693
|
promise.resolve("Failed to pause recording");
|
|
@@ -576,6 +707,7 @@ public class FileManager extends ReactContextBaseJavaModule {
|
|
|
576
707
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
577
708
|
try {
|
|
578
709
|
audioRecorder.resume();
|
|
710
|
+
startAmplitudePolling();
|
|
579
711
|
promise.resolve("Recording resumed successfully");
|
|
580
712
|
} catch (Exception e) {
|
|
581
713
|
promise.resolve("Failed to pause recording");
|
|
@@ -594,23 +726,118 @@ public class FileManager extends ReactContextBaseJavaModule {
|
|
|
594
726
|
if (audioPlayer != null && audioPlayer.isPlaying()) {
|
|
595
727
|
Log.e("TAG", "Trying to pause Audio: " + fileName);
|
|
596
728
|
audioPlayer.pause();
|
|
597
|
-
|
|
598
|
-
|
|
729
|
+
|
|
730
|
+
// Get current position for accurate resume
|
|
731
|
+
int currentPosition = audioPlayer.getCurrentPosition();
|
|
732
|
+
|
|
733
|
+
// Build response with file URI and position
|
|
734
|
+
String fileUri = fileName != null ? fileName : "";
|
|
735
|
+
Activity activity = getCurrentActivity();
|
|
736
|
+
if (activity != null && fileName != null) {
|
|
737
|
+
try {
|
|
738
|
+
Uri contentUri = FileProvider.getUriForFile(
|
|
739
|
+
activity,
|
|
740
|
+
activity.getApplicationContext().getPackageName() + ".fileprovider",
|
|
741
|
+
new File(fileName));
|
|
742
|
+
fileUri = contentUri.toString();
|
|
743
|
+
} catch (Exception e) {
|
|
744
|
+
Log.e("TAG", "FileProvider error: " + e.getMessage());
|
|
745
|
+
fileUri = "file://" + fileName;
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
callback.invoke("{\"success\": true, \"file\": \"" + fileUri + "\", \"position\": " + currentPosition + "}");
|
|
750
|
+
Log.e("TAG", "Paused Audio at position: " + currentPosition);
|
|
751
|
+
} else {
|
|
752
|
+
callback.invoke("{\"success\": false, \"error\": \"No audio playing\"}");
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
/**
|
|
757
|
+
* Resume playback from the current paused position.
|
|
758
|
+
* This is instant since the MediaPlayer is already prepared.
|
|
759
|
+
*/
|
|
760
|
+
@ReactMethod
|
|
761
|
+
public void resumePlaying(Callback callback) {
|
|
762
|
+
if (audioPlayer != null) {
|
|
763
|
+
try {
|
|
764
|
+
// Check if already playing
|
|
765
|
+
if (audioPlayer.isPlaying()) {
|
|
766
|
+
callback.invoke("{\"success\": true, \"message\": \"Already playing\"}");
|
|
767
|
+
return;
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
// Get current position before resuming
|
|
771
|
+
int currentPosition = audioPlayer.getCurrentPosition();
|
|
772
|
+
|
|
773
|
+
// Resume playback - this is instant since player is already prepared
|
|
774
|
+
audioPlayer.start();
|
|
775
|
+
Log.e("TAG", "Resumed Audio from position: " + currentPosition);
|
|
776
|
+
|
|
777
|
+
// Build response with file URI and position
|
|
778
|
+
String fileUri = fileName != null ? fileName : "";
|
|
779
|
+
Activity activity = getCurrentActivity();
|
|
780
|
+
if (activity != null && fileName != null) {
|
|
781
|
+
try {
|
|
782
|
+
Uri contentUri = FileProvider.getUriForFile(
|
|
783
|
+
activity,
|
|
784
|
+
activity.getApplicationContext().getPackageName() + ".fileprovider",
|
|
785
|
+
new File(fileName));
|
|
786
|
+
fileUri = contentUri.toString();
|
|
787
|
+
} catch (Exception e) {
|
|
788
|
+
Log.e("TAG", "FileProvider error: " + e.getMessage());
|
|
789
|
+
fileUri = "file://" + fileName;
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
callback.invoke("{\"success\": true, \"file\": \"" + fileUri + "\", \"position\": " + currentPosition + "}");
|
|
794
|
+
} catch (Exception e) {
|
|
795
|
+
Log.e("TAG", "resumePlaying error: " + e.getMessage());
|
|
796
|
+
callback.invoke("{\"success\": false, \"error\": \"" + e.getMessage() + "\"}");
|
|
797
|
+
}
|
|
798
|
+
} else {
|
|
799
|
+
callback.invoke("{\"success\": false, \"error\": \"No audio player initialized\"}");
|
|
599
800
|
}
|
|
600
801
|
}
|
|
601
802
|
|
|
602
803
|
@ReactMethod
|
|
603
804
|
private void stopPlaying(Callback callback) {
|
|
604
805
|
if (audioPlayer != null) {
|
|
605
|
-
|
|
606
|
-
|
|
806
|
+
try {
|
|
807
|
+
audioPlayer.stop();
|
|
808
|
+
} catch (Exception ignore) {}
|
|
809
|
+
try {
|
|
810
|
+
audioPlayer.release();
|
|
811
|
+
} catch (Exception ignore) {}
|
|
607
812
|
audioPlayer = null;
|
|
608
|
-
|
|
813
|
+
|
|
814
|
+
// Build response with file URI
|
|
815
|
+
String fileUri = fileName != null ? fileName : "";
|
|
816
|
+
Activity activity = getCurrentActivity();
|
|
817
|
+
if (activity != null && fileName != null) {
|
|
818
|
+
try {
|
|
819
|
+
Uri contentUri = FileProvider.getUriForFile(
|
|
820
|
+
activity,
|
|
821
|
+
activity.getApplicationContext().getPackageName() + ".fileprovider",
|
|
822
|
+
new File(fileName));
|
|
823
|
+
fileUri = contentUri.toString();
|
|
824
|
+
} catch (Exception e) {
|
|
825
|
+
Log.e("TAG", "FileProvider error: " + e.getMessage());
|
|
826
|
+
fileUri = "file://" + fileName;
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
callback.invoke("{\"success\": true, \"file\": \"" + fileUri + "\"}");
|
|
831
|
+
} else {
|
|
832
|
+
callback.invoke("{\"success\": false, \"error\": \"No audio player\"}");
|
|
609
833
|
}
|
|
610
834
|
}
|
|
611
835
|
|
|
612
836
|
@ReactMethod
|
|
613
837
|
public void releaseMediaResources(final Callback callback) {
|
|
838
|
+
// Stop amplitude polling
|
|
839
|
+
stopAmplitudePolling();
|
|
840
|
+
|
|
614
841
|
// Stop active recording
|
|
615
842
|
if (audioRecorder != null) {
|
|
616
843
|
try {
|
|
@@ -674,6 +901,678 @@ public class FileManager extends ReactContextBaseJavaModule {
|
|
|
674
901
|
}
|
|
675
902
|
}
|
|
676
903
|
|
|
904
|
+
// ==================== Segment-Based Recording Methods ====================
|
|
905
|
+
|
|
906
|
+
/**
|
|
907
|
+
* Finalize the current recording segment without stopping the recorder.
|
|
908
|
+
* This allows the user to preview the recorded audio while paused.
|
|
909
|
+
* @validates Requirements 11.1
|
|
910
|
+
*/
|
|
911
|
+
@ReactMethod
|
|
912
|
+
public void finalizeSegment(Callback callback) {
|
|
913
|
+
stopAmplitudePolling();
|
|
914
|
+
|
|
915
|
+
if (audioRecorder == null) {
|
|
916
|
+
callback.invoke("{\"success\": false, \"error\": \"No active recording\"}");
|
|
917
|
+
return;
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
try {
|
|
921
|
+
audioRecorder.stop();
|
|
922
|
+
audioRecorder.release();
|
|
923
|
+
audioRecorder = null;
|
|
924
|
+
|
|
925
|
+
// Add current file to segments (store the actual file path, not content URI)
|
|
926
|
+
if (fileName != null) {
|
|
927
|
+
segmentPaths.add(fileName);
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
// Return the file path (not content URI) so JS can use it for playback
|
|
931
|
+
String jsonString = String.format(
|
|
932
|
+
"{\"success\": true, \"segmentPath\": \"%s\", \"duration\": 0, \"segmentIndex\": %d}",
|
|
933
|
+
fileName,
|
|
934
|
+
segmentPaths.size() - 1
|
|
935
|
+
);
|
|
936
|
+
callback.invoke(jsonString);
|
|
937
|
+
} catch (Exception e) {
|
|
938
|
+
callback.invoke("{\"success\": false, \"error\": \"" + e.getMessage() + "\"}");
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
/**
|
|
943
|
+
* Start recording a new segment after pausing.
|
|
944
|
+
* This creates a new audio file for the next segment.
|
|
945
|
+
* @validates Requirements 11.2
|
|
946
|
+
*/
|
|
947
|
+
@ReactMethod
|
|
948
|
+
public void startNewSegment(Callback callback) {
|
|
949
|
+
Activity activity = getCurrentActivity();
|
|
950
|
+
if (activity == null) {
|
|
951
|
+
callback.invoke("{\"success\": false, \"error\": \"E_NO_ACTIVITY\"}");
|
|
952
|
+
return;
|
|
953
|
+
}
|
|
954
|
+
|
|
955
|
+
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.RECORD_AUDIO)
|
|
956
|
+
!= PackageManager.PERMISSION_GRANTED) {
|
|
957
|
+
callback.invoke("{\"granted\": false}");
|
|
958
|
+
return;
|
|
959
|
+
}
|
|
960
|
+
|
|
961
|
+
try {
|
|
962
|
+
// Create new file for this segment
|
|
963
|
+
File dir = activity.getExternalFilesDir(Environment.DIRECTORY_MUSIC);
|
|
964
|
+
if (dir == null) {
|
|
965
|
+
callback.invoke("{\"success\": false, \"error\": \"E_NO_DIR\"}");
|
|
966
|
+
return;
|
|
967
|
+
}
|
|
968
|
+
String ts = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
|
|
969
|
+
fileName = new File(dir, "audio-segment-" + ts + "-" + segmentPaths.size() + ".m4a").getAbsolutePath();
|
|
970
|
+
|
|
971
|
+
audioRecorder = new MediaRecorder();
|
|
972
|
+
audioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
|
|
973
|
+
audioRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
|
|
974
|
+
audioRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
|
|
975
|
+
audioRecorder.setAudioEncodingBitRate(128000);
|
|
976
|
+
audioRecorder.setAudioSamplingRate(44100);
|
|
977
|
+
audioRecorder.setOutputFile(fileName);
|
|
978
|
+
|
|
979
|
+
audioRecorder.prepare();
|
|
980
|
+
audioRecorder.start();
|
|
981
|
+
|
|
982
|
+
// Start amplitude polling for real-time waveform
|
|
983
|
+
startAmplitudePolling();
|
|
984
|
+
|
|
985
|
+
String jsonString = String.format(
|
|
986
|
+
"{\"success\": true, \"file\": \"%s\", \"segmentIndex\": %d}",
|
|
987
|
+
fileName,
|
|
988
|
+
segmentPaths.size()
|
|
989
|
+
);
|
|
990
|
+
callback.invoke(jsonString);
|
|
991
|
+
} catch (Exception e) {
|
|
992
|
+
try { if (audioRecorder != null) audioRecorder.release(); } catch (Exception ignore) {}
|
|
993
|
+
audioRecorder = null;
|
|
994
|
+
stopAmplitudePolling();
|
|
995
|
+
callback.invoke("{\"success\": false, \"error\": \"" + e.getMessage() + "\"}");
|
|
996
|
+
}
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
/**
|
|
1000
|
+
* Merge all recorded segments into a single audio file.
|
|
1001
|
+
* Uses simple concatenation for M4A files.
|
|
1002
|
+
* @validates Requirements 11.4
|
|
1003
|
+
*/
|
|
1004
|
+
@ReactMethod
|
|
1005
|
+
public void mergeSegments(ReadableArray segmentPathsArray, Callback callback) {
|
|
1006
|
+
if (segmentPathsArray == null || segmentPathsArray.size() == 0) {
|
|
1007
|
+
callback.invoke("{\"success\": false, \"error\": \"No segments to merge\"}");
|
|
1008
|
+
return;
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
// Convert ReadableArray to List
|
|
1012
|
+
List<String> paths = new ArrayList<>();
|
|
1013
|
+
for (int i = 0; i < segmentPathsArray.size(); i++) {
|
|
1014
|
+
paths.add(segmentPathsArray.getString(i));
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
// If only one segment, just return it and update fileName
|
|
1018
|
+
if (paths.size() == 1) {
|
|
1019
|
+
String segmentPath = paths.get(0);
|
|
1020
|
+
// Handle content:// URIs - convert to file path
|
|
1021
|
+
if (segmentPath.startsWith("content://")) {
|
|
1022
|
+
String[] parts = segmentPath.split("/external_files");
|
|
1023
|
+
if (parts.length > 1) {
|
|
1024
|
+
segmentPath = "/storage/emulated/0" + parts[1];
|
|
1025
|
+
}
|
|
1026
|
+
} else if (segmentPath.startsWith("file://")) {
|
|
1027
|
+
segmentPath = segmentPath.substring(7);
|
|
1028
|
+
}
|
|
1029
|
+
|
|
1030
|
+
// Update fileName so playAudio uses this file
|
|
1031
|
+
fileName = segmentPath;
|
|
1032
|
+
|
|
1033
|
+
Activity activity = getCurrentActivity();
|
|
1034
|
+
String fileUri = segmentPath;
|
|
1035
|
+
if (activity != null) {
|
|
1036
|
+
try {
|
|
1037
|
+
Uri contentUri = FileProvider.getUriForFile(
|
|
1038
|
+
activity, activity.getPackageName() + ".fileprovider", new File(segmentPath));
|
|
1039
|
+
fileUri = contentUri.toString();
|
|
1040
|
+
} catch (Exception e) {
|
|
1041
|
+
// Use original path if FileProvider fails
|
|
1042
|
+
fileUri = "file://" + segmentPath;
|
|
1043
|
+
}
|
|
1044
|
+
}
|
|
1045
|
+
callback.invoke(String.format("{\"success\": true, \"mergedPath\": \"%s\", \"totalDuration\": 0}", fileUri));
|
|
1046
|
+
return;
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
// Merge segments in background thread using MediaExtractor/MediaMuxer
|
|
1050
|
+
ExecutorService executor = Executors.newSingleThreadExecutor();
|
|
1051
|
+
executor.execute(() -> {
|
|
1052
|
+
try {
|
|
1053
|
+
Activity activity = getCurrentActivity();
|
|
1054
|
+
if (activity == null) {
|
|
1055
|
+
new Handler(Looper.getMainLooper()).post(() ->
|
|
1056
|
+
callback.invoke("{\"success\": false, \"error\": \"No activity\"}"));
|
|
1057
|
+
return;
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
// Create output file
|
|
1061
|
+
File dir = activity.getExternalFilesDir(Environment.DIRECTORY_MUSIC);
|
|
1062
|
+
String ts = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
|
|
1063
|
+
String mergedPath = new File(dir, "audio-merged-" + ts + ".m4a").getAbsolutePath();
|
|
1064
|
+
|
|
1065
|
+
// Use MediaMuxer for proper AAC/M4A merging
|
|
1066
|
+
android.media.MediaMuxer muxer = new android.media.MediaMuxer(mergedPath, android.media.MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
|
|
1067
|
+
int audioTrackIndex = -1;
|
|
1068
|
+
boolean muxerStarted = false;
|
|
1069
|
+
long totalDurationUs = 0;
|
|
1070
|
+
|
|
1071
|
+
for (String path : paths) {
|
|
1072
|
+
// Handle content:// URIs and file:// URIs
|
|
1073
|
+
String actualPath = path;
|
|
1074
|
+
if (path.startsWith("content://")) {
|
|
1075
|
+
String[] parts = path.split("/external_files");
|
|
1076
|
+
if (parts.length > 1) {
|
|
1077
|
+
actualPath = "/storage/emulated/0" + parts[1];
|
|
1078
|
+
} else {
|
|
1079
|
+
Log.e(TAG, "Cannot resolve content URI: " + path);
|
|
1080
|
+
continue;
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1083
|
+
if (actualPath.startsWith("file://")) {
|
|
1084
|
+
actualPath = actualPath.substring(7);
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
File segmentFile = new File(actualPath);
|
|
1088
|
+
if (!segmentFile.exists()) {
|
|
1089
|
+
Log.e(TAG, "Segment file not found: " + actualPath);
|
|
1090
|
+
continue;
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
android.media.MediaExtractor extractor = new android.media.MediaExtractor();
|
|
1094
|
+
extractor.setDataSource(actualPath);
|
|
1095
|
+
|
|
1096
|
+
// Find audio track
|
|
1097
|
+
int trackIndex = -1;
|
|
1098
|
+
for (int i = 0; i < extractor.getTrackCount(); i++) {
|
|
1099
|
+
android.media.MediaFormat format = extractor.getTrackFormat(i);
|
|
1100
|
+
String mime = format.getString(android.media.MediaFormat.KEY_MIME);
|
|
1101
|
+
if (mime != null && mime.startsWith("audio/")) {
|
|
1102
|
+
trackIndex = i;
|
|
1103
|
+
if (!muxerStarted) {
|
|
1104
|
+
audioTrackIndex = muxer.addTrack(format);
|
|
1105
|
+
muxer.start();
|
|
1106
|
+
muxerStarted = true;
|
|
1107
|
+
}
|
|
1108
|
+
break;
|
|
1109
|
+
}
|
|
1110
|
+
}
|
|
1111
|
+
|
|
1112
|
+
if (trackIndex < 0) {
|
|
1113
|
+
extractor.release();
|
|
1114
|
+
continue;
|
|
1115
|
+
}
|
|
1116
|
+
|
|
1117
|
+
extractor.selectTrack(trackIndex);
|
|
1118
|
+
|
|
1119
|
+
// Copy samples
|
|
1120
|
+
java.nio.ByteBuffer buffer = java.nio.ByteBuffer.allocate(1024 * 1024);
|
|
1121
|
+
android.media.MediaCodec.BufferInfo bufferInfo = new android.media.MediaCodec.BufferInfo();
|
|
1122
|
+
|
|
1123
|
+
while (true) {
|
|
1124
|
+
int sampleSize = extractor.readSampleData(buffer, 0);
|
|
1125
|
+
if (sampleSize < 0) break;
|
|
1126
|
+
|
|
1127
|
+
bufferInfo.offset = 0;
|
|
1128
|
+
bufferInfo.size = sampleSize;
|
|
1129
|
+
bufferInfo.presentationTimeUs = totalDurationUs + extractor.getSampleTime();
|
|
1130
|
+
bufferInfo.flags = extractor.getSampleFlags();
|
|
1131
|
+
|
|
1132
|
+
muxer.writeSampleData(audioTrackIndex, buffer, bufferInfo);
|
|
1133
|
+
extractor.advance();
|
|
1134
|
+
}
|
|
1135
|
+
|
|
1136
|
+
// Get duration of this segment
|
|
1137
|
+
android.media.MediaFormat format = extractor.getTrackFormat(trackIndex);
|
|
1138
|
+
if (format.containsKey(android.media.MediaFormat.KEY_DURATION)) {
|
|
1139
|
+
totalDurationUs += format.getLong(android.media.MediaFormat.KEY_DURATION);
|
|
1140
|
+
}
|
|
1141
|
+
|
|
1142
|
+
extractor.release();
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1145
|
+
if (muxerStarted) {
|
|
1146
|
+
muxer.stop();
|
|
1147
|
+
}
|
|
1148
|
+
muxer.release();
|
|
1149
|
+
|
|
1150
|
+
// Update fileName to merged file so playAudio uses it
|
|
1151
|
+
fileName = mergedPath;
|
|
1152
|
+
|
|
1153
|
+
String fileUri = mergedPath;
|
|
1154
|
+
try {
|
|
1155
|
+
Uri contentUri = FileProvider.getUriForFile(
|
|
1156
|
+
activity, activity.getPackageName() + ".fileprovider", new File(mergedPath));
|
|
1157
|
+
fileUri = contentUri.toString();
|
|
1158
|
+
} catch (Exception e) {
|
|
1159
|
+
// Use original path if FileProvider fails
|
|
1160
|
+
fileUri = "file://" + mergedPath;
|
|
1161
|
+
}
|
|
1162
|
+
|
|
1163
|
+
final String finalUri = fileUri;
|
|
1164
|
+
final double totalDurationSec = totalDurationUs / 1000000.0;
|
|
1165
|
+
new Handler(Looper.getMainLooper()).post(() ->
|
|
1166
|
+
callback.invoke(String.format("{\"success\": true, \"mergedPath\": \"%s\", \"totalDuration\": %f}", finalUri, totalDurationSec)));
|
|
1167
|
+
} catch (Exception e) {
|
|
1168
|
+
Log.e(TAG, "Error merging segments: " + e.getMessage());
|
|
1169
|
+
new Handler(Looper.getMainLooper()).post(() ->
|
|
1170
|
+
callback.invoke("{\"success\": false, \"error\": \"" + e.getMessage() + "\"}"));
|
|
1171
|
+
}
|
|
1172
|
+
});
|
|
1173
|
+
}
|
|
1174
|
+
|
|
1175
|
+
/**
|
|
1176
|
+
* Play multiple segments sequentially.
|
|
1177
|
+
* @validates Requirements 11.4
|
|
1178
|
+
*/
|
|
1179
|
+
@ReactMethod
|
|
1180
|
+
public void playSegments(ReadableArray segmentPathsArray, Callback callback) {
|
|
1181
|
+
if (segmentPathsArray == null || segmentPathsArray.size() == 0) {
|
|
1182
|
+
callback.invoke("{\"success\": false, \"error\": \"No segments to play\"}");
|
|
1183
|
+
return;
|
|
1184
|
+
}
|
|
1185
|
+
|
|
1186
|
+
// Convert ReadableArray to List
|
|
1187
|
+
final List<String> paths = new ArrayList<>();
|
|
1188
|
+
for (int i = 0; i < segmentPathsArray.size(); i++) {
|
|
1189
|
+
paths.add(segmentPathsArray.getString(i));
|
|
1190
|
+
}
|
|
1191
|
+
|
|
1192
|
+
// Stop any existing playback
|
|
1193
|
+
if (audioPlayer != null) {
|
|
1194
|
+
try {
|
|
1195
|
+
if (audioPlayer.isPlaying()) audioPlayer.stop();
|
|
1196
|
+
audioPlayer.release();
|
|
1197
|
+
} catch (Exception ignore) {}
|
|
1198
|
+
audioPlayer = null;
|
|
1199
|
+
}
|
|
1200
|
+
if (nextAudioPlayer != null) {
|
|
1201
|
+
try {
|
|
1202
|
+
nextAudioPlayer.release();
|
|
1203
|
+
} catch (Exception ignore) {}
|
|
1204
|
+
nextAudioPlayer = null;
|
|
1205
|
+
}
|
|
1206
|
+
|
|
1207
|
+
currentSegmentIndex = 0;
|
|
1208
|
+
segmentPathsList = paths;
|
|
1209
|
+
playNextSegmentGapless(callback);
|
|
1210
|
+
}
|
|
1211
|
+
|
|
1212
|
+
// Store segment paths for gapless playback
|
|
1213
|
+
private List<String> segmentPathsList = new ArrayList<>();
|
|
1214
|
+
private MediaPlayer nextAudioPlayer = null;
|
|
1215
|
+
|
|
1216
|
+
private MediaPlayer createMediaPlayerForPath(String path) throws Exception {
|
|
1217
|
+
MediaPlayer mp = new MediaPlayer();
|
|
1218
|
+
|
|
1219
|
+
if (path.startsWith("file://")) {
|
|
1220
|
+
path = path.substring(7);
|
|
1221
|
+
}
|
|
1222
|
+
|
|
1223
|
+
if (path.startsWith("content://")) {
|
|
1224
|
+
Activity activity = getCurrentActivity();
|
|
1225
|
+
if (activity != null) {
|
|
1226
|
+
mp.setDataSource(activity, Uri.parse(path));
|
|
1227
|
+
} else {
|
|
1228
|
+
String[] parts = path.split("/external_files");
|
|
1229
|
+
if (parts.length > 1) {
|
|
1230
|
+
path = "/storage/emulated/0" + parts[1];
|
|
1231
|
+
mp.setDataSource(path);
|
|
1232
|
+
} else {
|
|
1233
|
+
throw new Exception("Cannot resolve content URI: " + path);
|
|
1234
|
+
}
|
|
1235
|
+
}
|
|
1236
|
+
} else {
|
|
1237
|
+
mp.setDataSource(path);
|
|
1238
|
+
}
|
|
1239
|
+
|
|
1240
|
+
mp.prepare();
|
|
1241
|
+
return mp;
|
|
1242
|
+
}
|
|
1243
|
+
|
|
1244
|
+
private void playNextSegmentGapless(final Callback callback) {
|
|
1245
|
+
if (currentSegmentIndex >= segmentPathsList.size()) {
|
|
1246
|
+
// All segments played
|
|
1247
|
+
if (eventEmitter == null) {
|
|
1248
|
+
eventEmitter = getReactApplicationContext().getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class);
|
|
1249
|
+
}
|
|
1250
|
+
WritableMap params = Arguments.createMap();
|
|
1251
|
+
params.putString("state", "playbackComplete");
|
|
1252
|
+
eventEmitter.emit("status", params);
|
|
1253
|
+
return;
|
|
1254
|
+
}
|
|
1255
|
+
|
|
1256
|
+
try {
|
|
1257
|
+
String path = segmentPathsList.get(currentSegmentIndex);
|
|
1258
|
+
|
|
1259
|
+
// Create current player
|
|
1260
|
+
audioPlayer = createMediaPlayerForPath(path);
|
|
1261
|
+
|
|
1262
|
+
// Pre-load next segment for gapless playback
|
|
1263
|
+
if (currentSegmentIndex + 1 < segmentPathsList.size()) {
|
|
1264
|
+
try {
|
|
1265
|
+
String nextPath = segmentPathsList.get(currentSegmentIndex + 1);
|
|
1266
|
+
nextAudioPlayer = createMediaPlayerForPath(nextPath);
|
|
1267
|
+
audioPlayer.setNextMediaPlayer(nextAudioPlayer);
|
|
1268
|
+
} catch (Exception e) {
|
|
1269
|
+
Log.e(TAG, "Error pre-loading next segment: " + e.getMessage());
|
|
1270
|
+
// Continue without gapless - will have small gap
|
|
1271
|
+
}
|
|
1272
|
+
}
|
|
1273
|
+
|
|
1274
|
+
audioPlayer.setOnCompletionListener(mp -> {
|
|
1275
|
+
currentSegmentIndex++;
|
|
1276
|
+
mp.release();
|
|
1277
|
+
|
|
1278
|
+
// Move next player to current
|
|
1279
|
+
if (nextAudioPlayer != null) {
|
|
1280
|
+
audioPlayer = nextAudioPlayer;
|
|
1281
|
+
nextAudioPlayer = null;
|
|
1282
|
+
|
|
1283
|
+
// Pre-load the next-next segment
|
|
1284
|
+
if (currentSegmentIndex + 1 < segmentPathsList.size()) {
|
|
1285
|
+
try {
|
|
1286
|
+
String nextPath = segmentPathsList.get(currentSegmentIndex + 1);
|
|
1287
|
+
nextAudioPlayer = createMediaPlayerForPath(nextPath);
|
|
1288
|
+
audioPlayer.setNextMediaPlayer(nextAudioPlayer);
|
|
1289
|
+
} catch (Exception e) {
|
|
1290
|
+
Log.e(TAG, "Error pre-loading next segment: " + e.getMessage());
|
|
1291
|
+
}
|
|
1292
|
+
}
|
|
1293
|
+
|
|
1294
|
+
// Set completion listener for the new current player
|
|
1295
|
+
audioPlayer.setOnCompletionListener(this::onSegmentComplete);
|
|
1296
|
+
} else {
|
|
1297
|
+
audioPlayer = null;
|
|
1298
|
+
playNextSegmentGapless(callback);
|
|
1299
|
+
}
|
|
1300
|
+
});
|
|
1301
|
+
|
|
1302
|
+
audioPlayer.start();
|
|
1303
|
+
|
|
1304
|
+
if (currentSegmentIndex == 0) {
|
|
1305
|
+
callback.invoke(String.format("{\"success\": true, \"segmentCount\": %d}", segmentPathsList.size()));
|
|
1306
|
+
}
|
|
1307
|
+
} catch (Exception e) {
|
|
1308
|
+
Log.e(TAG, "Error playing segment: " + e.getMessage());
|
|
1309
|
+
if (currentSegmentIndex == 0) {
|
|
1310
|
+
callback.invoke("{\"success\": false, \"error\": \"" + e.getMessage() + "\"}");
|
|
1311
|
+
}
|
|
1312
|
+
}
|
|
1313
|
+
}
|
|
1314
|
+
|
|
1315
|
+
private void onSegmentComplete(MediaPlayer mp) {
|
|
1316
|
+
currentSegmentIndex++;
|
|
1317
|
+
mp.release();
|
|
1318
|
+
|
|
1319
|
+
if (nextAudioPlayer != null) {
|
|
1320
|
+
audioPlayer = nextAudioPlayer;
|
|
1321
|
+
nextAudioPlayer = null;
|
|
1322
|
+
|
|
1323
|
+
// Pre-load the next-next segment
|
|
1324
|
+
if (currentSegmentIndex + 1 < segmentPathsList.size()) {
|
|
1325
|
+
try {
|
|
1326
|
+
String nextPath = segmentPathsList.get(currentSegmentIndex + 1);
|
|
1327
|
+
nextAudioPlayer = createMediaPlayerForPath(nextPath);
|
|
1328
|
+
audioPlayer.setNextMediaPlayer(nextAudioPlayer);
|
|
1329
|
+
} catch (Exception e) {
|
|
1330
|
+
Log.e(TAG, "Error pre-loading next segment: " + e.getMessage());
|
|
1331
|
+
}
|
|
1332
|
+
}
|
|
1333
|
+
|
|
1334
|
+
audioPlayer.setOnCompletionListener(this::onSegmentComplete);
|
|
1335
|
+
} else {
|
|
1336
|
+
audioPlayer = null;
|
|
1337
|
+
// Check if there are more segments
|
|
1338
|
+
if (currentSegmentIndex < segmentPathsList.size()) {
|
|
1339
|
+
playNextSegmentGapless(null);
|
|
1340
|
+
} else {
|
|
1341
|
+
// All done
|
|
1342
|
+
if (eventEmitter == null) {
|
|
1343
|
+
eventEmitter = getReactApplicationContext().getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class);
|
|
1344
|
+
}
|
|
1345
|
+
WritableMap params = Arguments.createMap();
|
|
1346
|
+
params.putString("state", "playbackComplete");
|
|
1347
|
+
eventEmitter.emit("status", params);
|
|
1348
|
+
}
|
|
1349
|
+
}
|
|
1350
|
+
}
|
|
1351
|
+
|
|
1352
|
+
// Keep old method for backward compatibility but redirect to new one
|
|
1353
|
+
private void playNextSegment(final List<String> paths, final Callback callback) {
|
|
1354
|
+
segmentPathsList = paths;
|
|
1355
|
+
playNextSegmentGapless(callback);
|
|
1356
|
+
}
|
|
1357
|
+
|
|
1358
|
+
/**
|
|
1359
|
+
* Seek to a specific position in the audio playback.
|
|
1360
|
+
* Uses OnSeekCompleteListener to wait for the async seek to complete
|
|
1361
|
+
* before invoking the callback, preventing race conditions.
|
|
1362
|
+
* @param positionMs Position in milliseconds
|
|
1363
|
+
*/
|
|
1364
|
+
@ReactMethod
|
|
1365
|
+
public void seekTo(int positionMs, Callback callback) {
|
|
1366
|
+
if (audioPlayer != null) {
|
|
1367
|
+
try {
|
|
1368
|
+
// Set up listener to wait for seek to complete
|
|
1369
|
+
audioPlayer.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
|
|
1370
|
+
@Override
|
|
1371
|
+
public void onSeekComplete(MediaPlayer mp) {
|
|
1372
|
+
// Clear the listener after use
|
|
1373
|
+
mp.setOnSeekCompleteListener(null);
|
|
1374
|
+
// Get actual position after seek completes
|
|
1375
|
+
int actualPosition = mp.getCurrentPosition();
|
|
1376
|
+
callback.invoke("{\"success\": true, \"position\": " + actualPosition + "}");
|
|
1377
|
+
}
|
|
1378
|
+
});
|
|
1379
|
+
audioPlayer.seekTo(positionMs);
|
|
1380
|
+
} catch (Exception e) {
|
|
1381
|
+
callback.invoke("{\"success\": false, \"error\": \"" + e.getMessage() + "\"}");
|
|
1382
|
+
}
|
|
1383
|
+
} else {
|
|
1384
|
+
callback.invoke("{\"success\": false, \"error\": \"No audio player active\"}");
|
|
1385
|
+
}
|
|
1386
|
+
}
|
|
1387
|
+
|
|
1388
|
+
/**
|
|
1389
|
+
* Get current playback position in milliseconds.
|
|
1390
|
+
* Used for accurate waveform sync during playback.
|
|
1391
|
+
*/
|
|
1392
|
+
@ReactMethod
|
|
1393
|
+
public void getPlaybackPosition(Callback callback) {
|
|
1394
|
+
if (audioPlayer != null) {
|
|
1395
|
+
try {
|
|
1396
|
+
int position = audioPlayer.getCurrentPosition();
|
|
1397
|
+
int duration = audioPlayer.getDuration();
|
|
1398
|
+
callback.invoke("{\"success\": true, \"position\": " + position + ", \"duration\": " + duration + "}");
|
|
1399
|
+
} catch (Exception e) {
|
|
1400
|
+
callback.invoke("{\"success\": false, \"error\": \"" + e.getMessage() + "\"}");
|
|
1401
|
+
}
|
|
1402
|
+
} else {
|
|
1403
|
+
callback.invoke("{\"success\": false, \"error\": \"No audio player active\"}");
|
|
1404
|
+
}
|
|
1405
|
+
}
|
|
1406
|
+
|
|
1407
|
+
/**
|
|
1408
|
+
* Start playback from a specific position.
|
|
1409
|
+
* Uses OnSeekCompleteListener to ensure seek completes before starting playback.
|
|
1410
|
+
* @param positionMs Position in milliseconds to start from
|
|
1411
|
+
*/
|
|
1412
|
+
@ReactMethod
|
|
1413
|
+
public void playFromPosition(int positionMs, Callback callback) {
|
|
1414
|
+
if (fileName == null || fileName.isEmpty()) {
|
|
1415
|
+
callback.invoke("{\"success\": false, \"error\": \"No audio file to play\"}");
|
|
1416
|
+
return;
|
|
1417
|
+
}
|
|
1418
|
+
|
|
1419
|
+
// Stop any existing playback
|
|
1420
|
+
if (audioPlayer != null) {
|
|
1421
|
+
try {
|
|
1422
|
+
if (audioPlayer.isPlaying()) audioPlayer.stop();
|
|
1423
|
+
audioPlayer.release();
|
|
1424
|
+
} catch (Exception ignore) {}
|
|
1425
|
+
audioPlayer = null;
|
|
1426
|
+
}
|
|
1427
|
+
|
|
1428
|
+
try {
|
|
1429
|
+
audioPlayer = new MediaPlayer();
|
|
1430
|
+
|
|
1431
|
+
String playPath = fileName;
|
|
1432
|
+
if (playPath.startsWith("content://")) {
|
|
1433
|
+
Activity activity = getCurrentActivity();
|
|
1434
|
+
if (activity != null) {
|
|
1435
|
+
audioPlayer.setDataSource(activity, Uri.parse(playPath));
|
|
1436
|
+
} else {
|
|
1437
|
+
String[] parts = playPath.split("/external_files");
|
|
1438
|
+
if (parts.length > 1) {
|
|
1439
|
+
playPath = "/storage/emulated/0" + parts[1];
|
|
1440
|
+
audioPlayer.setDataSource(playPath);
|
|
1441
|
+
} else {
|
|
1442
|
+
throw new IOException("Cannot resolve content URI without activity");
|
|
1443
|
+
}
|
|
1444
|
+
}
|
|
1445
|
+
} else {
|
|
1446
|
+
if (playPath.startsWith("file://")) {
|
|
1447
|
+
playPath = playPath.substring(7);
|
|
1448
|
+
}
|
|
1449
|
+
audioPlayer.setDataSource(playPath);
|
|
1450
|
+
}
|
|
1451
|
+
|
|
1452
|
+
// Set up completion listener to emit event when playback finishes
|
|
1453
|
+
audioPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
|
|
1454
|
+
@Override
|
|
1455
|
+
public void onCompletion(MediaPlayer mp) {
|
|
1456
|
+
// Emit playback complete event to JS
|
|
1457
|
+
if (eventEmitter == null) {
|
|
1458
|
+
eventEmitter = getReactApplicationContext().getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class);
|
|
1459
|
+
}
|
|
1460
|
+
WritableMap params = Arguments.createMap();
|
|
1461
|
+
params.putString("state", "playbackComplete");
|
|
1462
|
+
eventEmitter.emit("status", params);
|
|
1463
|
+
}
|
|
1464
|
+
});
|
|
1465
|
+
|
|
1466
|
+
audioPlayer.prepare();
|
|
1467
|
+
|
|
1468
|
+
// Use OnSeekCompleteListener to ensure seek completes before starting playback
|
|
1469
|
+
audioPlayer.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
|
|
1470
|
+
@Override
|
|
1471
|
+
public void onSeekComplete(MediaPlayer mp) {
|
|
1472
|
+
// Clear the listener after use
|
|
1473
|
+
mp.setOnSeekCompleteListener(null);
|
|
1474
|
+
// Start playback after seek completes
|
|
1475
|
+
mp.start();
|
|
1476
|
+
int actualPosition = mp.getCurrentPosition();
|
|
1477
|
+
callback.invoke("{\"success\": true, \"position\": " + actualPosition + "}");
|
|
1478
|
+
}
|
|
1479
|
+
});
|
|
1480
|
+
audioPlayer.seekTo(positionMs);
|
|
1481
|
+
|
|
1482
|
+
} catch (Exception e) {
|
|
1483
|
+
Log.e("TAG", "playFromPosition error: " + e.getMessage());
|
|
1484
|
+
callback.invoke("{\"success\": false, \"error\": \"" + e.getMessage() + "\"}");
|
|
1485
|
+
}
|
|
1486
|
+
}
|
|
1487
|
+
|
|
1488
|
+
/**
|
|
1489
|
+
* Delete all segment files.
|
|
1490
|
+
* @validates Requirements 11.5
|
|
1491
|
+
*/
|
|
1492
|
+
@ReactMethod
|
|
1493
|
+
public void deleteSegments(ReadableArray segmentPathsArray, Callback callback) {
|
|
1494
|
+
if (segmentPathsArray == null || segmentPathsArray.size() == 0) {
|
|
1495
|
+
callback.invoke("{\"success\": true, \"deletedCount\": 0}");
|
|
1496
|
+
return;
|
|
1497
|
+
}
|
|
1498
|
+
|
|
1499
|
+
int deletedCount = 0;
|
|
1500
|
+
for (int i = 0; i < segmentPathsArray.size(); i++) {
|
|
1501
|
+
try {
|
|
1502
|
+
String path = segmentPathsArray.getString(i);
|
|
1503
|
+
// Handle content:// URIs
|
|
1504
|
+
if (path.startsWith("file://")) {
|
|
1505
|
+
path = path.substring(7);
|
|
1506
|
+
}
|
|
1507
|
+
if (path.startsWith("content://")) {
|
|
1508
|
+
// Skip content URIs
|
|
1509
|
+
continue;
|
|
1510
|
+
}
|
|
1511
|
+
|
|
1512
|
+
File file = new File(path);
|
|
1513
|
+
if (file.exists() && file.delete()) {
|
|
1514
|
+
deletedCount++;
|
|
1515
|
+
}
|
|
1516
|
+
} catch (Exception e) {
|
|
1517
|
+
Log.e(TAG, "Error deleting segment: " + e.getMessage());
|
|
1518
|
+
}
|
|
1519
|
+
}
|
|
1520
|
+
|
|
1521
|
+
// Clear internal segments list
|
|
1522
|
+
segmentPaths.clear();
|
|
1523
|
+
currentSegmentIndex = 0;
|
|
1524
|
+
|
|
1525
|
+
callback.invoke(String.format("{\"success\": true, \"deletedCount\": %d}", deletedCount));
|
|
1526
|
+
}
|
|
1527
|
+
|
|
1528
|
+
/**
|
|
1529
|
+
* Get all current segment paths.
|
|
1530
|
+
*/
|
|
1531
|
+
@ReactMethod
|
|
1532
|
+
public void getSegmentPaths(Callback callback) {
|
|
1533
|
+
try {
|
|
1534
|
+
JSONArray pathsArray = new JSONArray();
|
|
1535
|
+
Activity activity = getCurrentActivity();
|
|
1536
|
+
|
|
1537
|
+
for (String path : segmentPaths) {
|
|
1538
|
+
String fileUri = path;
|
|
1539
|
+
if (activity != null) {
|
|
1540
|
+
try {
|
|
1541
|
+
Uri contentUri = FileProvider.getUriForFile(
|
|
1542
|
+
activity, activity.getPackageName() + ".fileprovider", new File(path));
|
|
1543
|
+
fileUri = contentUri.toString();
|
|
1544
|
+
} catch (Exception e) {
|
|
1545
|
+
// Use original path if FileProvider fails
|
|
1546
|
+
}
|
|
1547
|
+
}
|
|
1548
|
+
pathsArray.put(fileUri);
|
|
1549
|
+
}
|
|
1550
|
+
|
|
1551
|
+
callback.invoke(String.format("{\"success\": true, \"segments\": %s}", pathsArray.toString()));
|
|
1552
|
+
} catch (Exception e) {
|
|
1553
|
+
callback.invoke("{\"success\": false, \"error\": \"" + e.getMessage() + "\"}");
|
|
1554
|
+
}
|
|
1555
|
+
}
|
|
1556
|
+
|
|
1557
|
+
/**
|
|
1558
|
+
* Clear all segments and reset state.
|
|
1559
|
+
*/
|
|
1560
|
+
@ReactMethod
|
|
1561
|
+
public void clearSegments(Callback callback) {
|
|
1562
|
+
segmentPaths.clear();
|
|
1563
|
+
currentSegmentIndex = 0;
|
|
1564
|
+
|
|
1565
|
+
if (audioPlayer != null) {
|
|
1566
|
+
try {
|
|
1567
|
+
if (audioPlayer.isPlaying()) audioPlayer.stop();
|
|
1568
|
+
audioPlayer.release();
|
|
1569
|
+
} catch (Exception ignore) {}
|
|
1570
|
+
audioPlayer = null;
|
|
1571
|
+
}
|
|
1572
|
+
|
|
1573
|
+
callback.invoke("{\"success\": true}");
|
|
1574
|
+
}
|
|
1575
|
+
|
|
677
1576
|
@ReactMethod
|
|
678
1577
|
public void openCamera(String fileType, Integer imageQuality, final Callback callback) {
|
|
679
1578
|
IMAGE_QUALITY = imageQuality;
|