@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
package/ios/FileManager.m
CHANGED
|
@@ -32,6 +32,12 @@ static NSString *const FIELD_SIZE = @"size";
|
|
|
32
32
|
@property (nonatomic, strong) AVAudioPlayer *player;
|
|
33
33
|
@property (nonatomic, strong) UIDocumentInteractionController *documentController;
|
|
34
34
|
|
|
35
|
+
// Segment-based recording properties
|
|
36
|
+
@property (nonatomic, strong) NSMutableArray<NSURL *> *segmentPaths;
|
|
37
|
+
@property (nonatomic, assign) NSInteger currentSegmentIndex;
|
|
38
|
+
@property (nonatomic, strong) AVQueuePlayer *queuePlayer;
|
|
39
|
+
@property (nonatomic, strong) id playbackObserver;
|
|
40
|
+
|
|
35
41
|
@end
|
|
36
42
|
|
|
37
43
|
@implementation CometChatFileManager {
|
|
@@ -49,7 +55,7 @@ static NSString *const FIELD_SIZE = @"size";
|
|
|
49
55
|
}
|
|
50
56
|
|
|
51
57
|
- (NSArray<NSString *> *)supportedEvents {
|
|
52
|
-
return @[@"opening", @"downloading", @"status", @"downloadComplete"];
|
|
58
|
+
return @[@"opening", @"downloading", @"status", @"downloadComplete", @"audioAmplitude"];
|
|
53
59
|
}
|
|
54
60
|
|
|
55
61
|
RCT_EXPORT_MODULE(FileManager)
|
|
@@ -423,6 +429,7 @@ RCT_EXPORT_METHOD(pauseRecording:(RCTPromiseResolveBlock)resolve
|
|
|
423
429
|
rejecter:(RCTPromiseRejectBlock)reject) {
|
|
424
430
|
if (self.audioRecorder.isRecording) {
|
|
425
431
|
[self.audioRecorder pause];
|
|
432
|
+
[self stopAmplitudeTimer];
|
|
426
433
|
resolve(@"success");
|
|
427
434
|
} else {
|
|
428
435
|
resolve(@"error");
|
|
@@ -433,6 +440,7 @@ RCT_EXPORT_METHOD(resumeRecording:(RCTPromiseResolveBlock)resolve
|
|
|
433
440
|
rejecter:(RCTPromiseRejectBlock)reject) {
|
|
434
441
|
if (![self.audioRecorder isRecording]) {
|
|
435
442
|
[self.audioRecorder record];
|
|
443
|
+
[self startAmplitudeTimer];
|
|
436
444
|
resolve(@"success");
|
|
437
445
|
} else {
|
|
438
446
|
resolve(@"error");
|
|
@@ -456,12 +464,38 @@ RCT_EXPORT_METHOD(resumeRecording:(RCTPromiseResolveBlock)resolve
|
|
|
456
464
|
self.audioRecorder.meteringEnabled = YES;
|
|
457
465
|
[self.audioRecorder record];
|
|
458
466
|
|
|
467
|
+
// Start amplitude timer for real-time waveform
|
|
468
|
+
[self startAmplitudeTimer];
|
|
469
|
+
|
|
459
470
|
NSString *jsonString = [NSString stringWithFormat:@"{\"success\": true, \"file\": \"%@\"}", [self.audioFilename path]];
|
|
460
471
|
NSLog(@"FILENAME: %@", jsonString);
|
|
461
472
|
callback(@[jsonString]);
|
|
462
473
|
}
|
|
463
474
|
}
|
|
464
475
|
|
|
476
|
+
- (void)startAmplitudeTimer {
|
|
477
|
+
[self stopAmplitudeTimer];
|
|
478
|
+
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 repeats:YES block:^(NSTimer * _Nonnull timer) {
|
|
479
|
+
if (self.audioRecorder && self.audioRecorder.isRecording && self->hasListeners) {
|
|
480
|
+
[self.audioRecorder updateMeters];
|
|
481
|
+
float decibels = [self.audioRecorder averagePowerForChannel:0];
|
|
482
|
+
// Convert decibels to linear scale (0.0 to 1.0)
|
|
483
|
+
// Decibels range from -160 (silence) to 0 (max)
|
|
484
|
+
// We normalize to 0-1 range
|
|
485
|
+
float linear = pow(10, decibels / 20.0);
|
|
486
|
+
linear = MIN(1.0, MAX(0.0, linear));
|
|
487
|
+
[self sendEventWithName:@"audioAmplitude" body:@{@"amplitude": @(linear)}];
|
|
488
|
+
}
|
|
489
|
+
}];
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
- (void)stopAmplitudeTimer {
|
|
493
|
+
if (self.timer) {
|
|
494
|
+
[self.timer invalidate];
|
|
495
|
+
self.timer = nil;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
|
|
465
499
|
RCT_EXPORT_METHOD(stopRecordingAudio:(RCTResponseSenderBlock)callback) {
|
|
466
500
|
if (self.audioRecorder != nil) {
|
|
467
501
|
NSString *path = [self stopRecordingWithSuccess:YES];
|
|
@@ -473,6 +507,7 @@ RCT_EXPORT_METHOD(stopRecordingAudio:(RCTResponseSenderBlock)callback) {
|
|
|
473
507
|
}
|
|
474
508
|
|
|
475
509
|
- (NSString *)stopRecordingWithSuccess:(BOOL)success {
|
|
510
|
+
[self stopAmplitudeTimer];
|
|
476
511
|
if (success) {
|
|
477
512
|
[self.audioRecorder stop];
|
|
478
513
|
self.audioRecorder = nil;
|
|
@@ -534,27 +569,101 @@ RCT_EXPORT_METHOD(resumePlaying:(RCTResponseSenderBlock)callback) {
|
|
|
534
569
|
callback(@[jsonString]);
|
|
535
570
|
}
|
|
536
571
|
|
|
572
|
+
/**
|
|
573
|
+
* Get current playback position in milliseconds.
|
|
574
|
+
* Used for accurate waveform sync during playback.
|
|
575
|
+
*/
|
|
576
|
+
RCT_EXPORT_METHOD(getPlaybackPosition:(RCTResponseSenderBlock)callback) {
|
|
577
|
+
if (self.player != nil) {
|
|
578
|
+
NSTimeInterval currentTime = self.player.currentTime;
|
|
579
|
+
NSTimeInterval duration = self.player.duration;
|
|
580
|
+
int positionMs = (int)(currentTime * 1000);
|
|
581
|
+
int durationMs = (int)(duration * 1000);
|
|
582
|
+
NSString *jsonString = [NSString stringWithFormat:@"{\"success\": true, \"position\": %d, \"duration\": %d}", positionMs, durationMs];
|
|
583
|
+
callback(@[jsonString]);
|
|
584
|
+
} else if (self.queuePlayer != nil) {
|
|
585
|
+
// For queue player (segment playback)
|
|
586
|
+
CMTime currentTime = self.queuePlayer.currentTime;
|
|
587
|
+
int positionMs = (int)(CMTimeGetSeconds(currentTime) * 1000);
|
|
588
|
+
|
|
589
|
+
// Get total duration from all items in queue
|
|
590
|
+
int totalDurationMs = 0;
|
|
591
|
+
for (AVPlayerItem *item in self.queuePlayer.items) {
|
|
592
|
+
CMTime itemDuration = item.asset.duration;
|
|
593
|
+
if (CMTIME_IS_VALID(itemDuration) && !CMTIME_IS_INDEFINITE(itemDuration)) {
|
|
594
|
+
totalDurationMs += (int)(CMTimeGetSeconds(itemDuration) * 1000);
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
NSString *jsonString = [NSString stringWithFormat:@"{\"success\": true, \"position\": %d, \"duration\": %d}", positionMs, totalDurationMs];
|
|
599
|
+
callback(@[jsonString]);
|
|
600
|
+
} else {
|
|
601
|
+
callback(@[@"{\"success\": false, \"error\": \"No audio player active\"}"]);
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
/**
|
|
606
|
+
* Seek to a specific position in the audio playback.
|
|
607
|
+
* @param positionMs Position in milliseconds
|
|
608
|
+
*/
|
|
609
|
+
RCT_EXPORT_METHOD(seekTo:(int)positionMs callback:(RCTResponseSenderBlock)callback) {
|
|
610
|
+
if (self.player != nil) {
|
|
611
|
+
NSTimeInterval positionSeconds = positionMs / 1000.0;
|
|
612
|
+
self.player.currentTime = positionSeconds;
|
|
613
|
+
NSString *jsonString = [NSString stringWithFormat:@"{\"success\": true, \"position\": %d}", positionMs];
|
|
614
|
+
callback(@[jsonString]);
|
|
615
|
+
} else {
|
|
616
|
+
callback(@[@"{\"success\": false, \"error\": \"No audio player active\"}"]);
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
/**
|
|
621
|
+
* Start playback from a specific position.
|
|
622
|
+
* @param positionMs Position in milliseconds to start from
|
|
623
|
+
*/
|
|
624
|
+
RCT_EXPORT_METHOD(playFromPosition:(int)positionMs callback:(RCTResponseSenderBlock)callback) {
|
|
625
|
+
if (self.audioFilename == nil) {
|
|
626
|
+
callback(@[@"{\"success\": false, \"error\": \"No audio file to play\"}"]);
|
|
627
|
+
return;
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
[self preparePlayer];
|
|
631
|
+
|
|
632
|
+
if (self.player != nil) {
|
|
633
|
+
NSTimeInterval positionSeconds = positionMs / 1000.0;
|
|
634
|
+
self.player.currentTime = positionSeconds;
|
|
635
|
+
[self.player play];
|
|
636
|
+
NSString *jsonString = [NSString stringWithFormat:@"{\"success\": true, \"position\": %d}", positionMs];
|
|
637
|
+
callback(@[jsonString]);
|
|
638
|
+
} else {
|
|
639
|
+
callback(@[@"{\"success\": false, \"error\": \"Failed to create audio player\"}"]);
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
|
|
537
643
|
- (void)stopPlaying {
|
|
538
644
|
[self.player stop];
|
|
539
645
|
self.player = nil;
|
|
540
646
|
}
|
|
541
647
|
|
|
542
648
|
RCT_EXPORT_METHOD(releaseMediaResources:(RCTResponseSenderBlock)callback) {
|
|
543
|
-
if (self.audioRecorder != nil
|
|
649
|
+
if (self.audioRecorder != nil) {
|
|
544
650
|
NSTimeInterval duration = self.audioRecorder.currentTime;
|
|
545
651
|
[self stopRecordingWithSuccess:YES];
|
|
546
652
|
NSString *jsonString = [NSString stringWithFormat:@"{\"success\": true, \"file\": \"%@\", \"duration\": %f}", [self.audioFilename path], duration];
|
|
547
653
|
NSLog(@"FILENAME: %@", jsonString);
|
|
548
654
|
callback(@[jsonString]);
|
|
655
|
+
return;
|
|
549
656
|
}
|
|
550
657
|
|
|
551
|
-
if (self.player != nil
|
|
658
|
+
if (self.player != nil) {
|
|
552
659
|
[self stopPlaying];
|
|
553
660
|
NSString *jsonString = [NSString stringWithFormat:@"{\"success\": true, \"file\": \"%@\"}", [self.audioFilename path]];
|
|
554
661
|
NSLog(@"FILENAME: %@", jsonString);
|
|
555
662
|
callback(@[jsonString]);
|
|
663
|
+
return;
|
|
556
664
|
}
|
|
557
665
|
|
|
666
|
+
callback(@[@"{\"success\": true}"]);
|
|
558
667
|
}
|
|
559
668
|
|
|
560
669
|
RCT_EXPORT_METHOD(deleteFile:(RCTResponseSenderBlock)callback) {
|
|
@@ -574,6 +683,324 @@ RCT_EXPORT_METHOD(deleteFile:(RCTResponseSenderBlock)callback) {
|
|
|
574
683
|
}
|
|
575
684
|
}
|
|
576
685
|
|
|
686
|
+
#pragma mark - Segment-Based Recording Methods
|
|
687
|
+
|
|
688
|
+
/**
|
|
689
|
+
* Finalize the current recording segment without stopping the recorder.
|
|
690
|
+
* This allows the user to preview the recorded audio while paused.
|
|
691
|
+
* @validates Requirements 11.1
|
|
692
|
+
*/
|
|
693
|
+
RCT_EXPORT_METHOD(finalizeSegment:(RCTResponseSenderBlock)callback) {
|
|
694
|
+
if (self.audioRecorder == nil || !self.audioRecorder.isRecording) {
|
|
695
|
+
// If not recording, check if we have a paused recording
|
|
696
|
+
if (self.audioRecorder != nil) {
|
|
697
|
+
NSTimeInterval duration = self.audioRecorder.currentTime;
|
|
698
|
+
[self.audioRecorder stop];
|
|
699
|
+
|
|
700
|
+
// Initialize segments array if needed
|
|
701
|
+
if (self.segmentPaths == nil) {
|
|
702
|
+
self.segmentPaths = [NSMutableArray array];
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
// Add current file to segments
|
|
706
|
+
if (self.audioFilename != nil) {
|
|
707
|
+
[self.segmentPaths addObject:self.audioFilename];
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
NSString *jsonString = [NSString stringWithFormat:@"{\"success\": true, \"segmentPath\": \"%@\", \"duration\": %f, \"segmentIndex\": %lu}",
|
|
711
|
+
[self.audioFilename path],
|
|
712
|
+
duration,
|
|
713
|
+
(unsigned long)(self.segmentPaths.count - 1)];
|
|
714
|
+
callback(@[jsonString]);
|
|
715
|
+
return;
|
|
716
|
+
}
|
|
717
|
+
callback(@[@"{\"success\": false, \"error\": \"No active recording\"}"]);
|
|
718
|
+
return;
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
[self stopAmplitudeTimer];
|
|
722
|
+
NSTimeInterval duration = self.audioRecorder.currentTime;
|
|
723
|
+
[self.audioRecorder stop];
|
|
724
|
+
|
|
725
|
+
// Initialize segments array if needed
|
|
726
|
+
if (self.segmentPaths == nil) {
|
|
727
|
+
self.segmentPaths = [NSMutableArray array];
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
// Add current file to segments
|
|
731
|
+
[self.segmentPaths addObject:self.audioFilename];
|
|
732
|
+
|
|
733
|
+
NSString *jsonString = [NSString stringWithFormat:@"{\"success\": true, \"segmentPath\": \"%@\", \"duration\": %f, \"segmentIndex\": %lu}",
|
|
734
|
+
[self.audioFilename path],
|
|
735
|
+
duration,
|
|
736
|
+
(unsigned long)(self.segmentPaths.count - 1)];
|
|
737
|
+
callback(@[jsonString]);
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
/**
|
|
741
|
+
* Start recording a new segment after pausing.
|
|
742
|
+
* This creates a new audio file for the next segment.
|
|
743
|
+
* @validates Requirements 11.2
|
|
744
|
+
*/
|
|
745
|
+
RCT_EXPORT_METHOD(startNewSegment:(RCTResponseSenderBlock)callback) {
|
|
746
|
+
self.recordingSession = [AVAudioSession sharedInstance];
|
|
747
|
+
NSError *error = nil;
|
|
748
|
+
if ([self.recordingSession setCategory:AVAudioSessionCategoryPlayAndRecord
|
|
749
|
+
withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker
|
|
750
|
+
error:&error] &&
|
|
751
|
+
[self.recordingSession setActive:YES error:&error]) {
|
|
752
|
+
|
|
753
|
+
// Create a new file for this segment
|
|
754
|
+
self.audioFilename = [self getFileURL];
|
|
755
|
+
|
|
756
|
+
NSDictionary *settings = @{
|
|
757
|
+
AVFormatIDKey : [NSNumber numberWithInt:kAudioFormatMPEG4AAC],
|
|
758
|
+
AVSampleRateKey : [NSNumber numberWithFloat:12000.0],
|
|
759
|
+
AVNumberOfChannelsKey : [NSNumber numberWithInt:1],
|
|
760
|
+
AVEncoderAudioQualityKey : [NSNumber numberWithInt:AVAudioQualityHigh]
|
|
761
|
+
};
|
|
762
|
+
|
|
763
|
+
NSError *recorderError = nil;
|
|
764
|
+
self.audioRecorder = [[AVAudioRecorder alloc] initWithURL:self.audioFilename settings:settings error:&recorderError];
|
|
765
|
+
|
|
766
|
+
if (recorderError) {
|
|
767
|
+
callback(@[@"{\"success\": false, \"error\": \"Failed to create recorder\"}"]);
|
|
768
|
+
return;
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
self.audioRecorder.delegate = self;
|
|
772
|
+
self.audioRecorder.meteringEnabled = YES;
|
|
773
|
+
[self.audioRecorder record];
|
|
774
|
+
|
|
775
|
+
// Start amplitude timer for real-time waveform
|
|
776
|
+
[self startAmplitudeTimer];
|
|
777
|
+
|
|
778
|
+
// Initialize segments array if needed
|
|
779
|
+
if (self.segmentPaths == nil) {
|
|
780
|
+
self.segmentPaths = [NSMutableArray array];
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
NSString *jsonString = [NSString stringWithFormat:@"{\"success\": true, \"file\": \"%@\", \"segmentIndex\": %lu}",
|
|
784
|
+
[self.audioFilename path],
|
|
785
|
+
(unsigned long)self.segmentPaths.count];
|
|
786
|
+
callback(@[jsonString]);
|
|
787
|
+
} else {
|
|
788
|
+
callback(@[@"{\"success\": false, \"error\": \"Failed to setup audio session\"}"]);
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
/**
|
|
793
|
+
* Merge all recorded segments into a single audio file.
|
|
794
|
+
* Uses AVMutableComposition for seamless audio concatenation.
|
|
795
|
+
* @validates Requirements 11.4
|
|
796
|
+
*/
|
|
797
|
+
RCT_EXPORT_METHOD(mergeSegments:(NSArray<NSString *> *)segmentPaths callback:(RCTResponseSenderBlock)callback) {
|
|
798
|
+
if (segmentPaths == nil || segmentPaths.count == 0) {
|
|
799
|
+
callback(@[@"{\"success\": false, \"error\": \"No segments to merge\"}"]);
|
|
800
|
+
return;
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
// If only one segment, just return it
|
|
804
|
+
if (segmentPaths.count == 1) {
|
|
805
|
+
NSString *jsonString = [NSString stringWithFormat:@"{\"success\": true, \"mergedPath\": \"%@\", \"totalDuration\": 0}", segmentPaths[0]];
|
|
806
|
+
callback(@[jsonString]);
|
|
807
|
+
return;
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
|
811
|
+
AVMutableComposition *composition = [AVMutableComposition composition];
|
|
812
|
+
AVMutableCompositionTrack *audioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
|
|
813
|
+
|
|
814
|
+
CMTime currentTime = kCMTimeZero;
|
|
815
|
+
Float64 totalDuration = 0;
|
|
816
|
+
|
|
817
|
+
for (NSString *segmentPath in segmentPaths) {
|
|
818
|
+
NSURL *segmentURL = [NSURL fileURLWithPath:segmentPath];
|
|
819
|
+
AVURLAsset *asset = [AVURLAsset assetWithURL:segmentURL];
|
|
820
|
+
|
|
821
|
+
NSArray *tracks = [asset tracksWithMediaType:AVMediaTypeAudio];
|
|
822
|
+
if (tracks.count == 0) {
|
|
823
|
+
NSLog(@"No audio track found in segment: %@", segmentPath);
|
|
824
|
+
continue;
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
AVAssetTrack *assetTrack = tracks[0];
|
|
828
|
+
CMTimeRange timeRange = CMTimeRangeMake(kCMTimeZero, asset.duration);
|
|
829
|
+
|
|
830
|
+
NSError *error = nil;
|
|
831
|
+
[audioTrack insertTimeRange:timeRange ofTrack:assetTrack atTime:currentTime error:&error];
|
|
832
|
+
|
|
833
|
+
if (error) {
|
|
834
|
+
NSLog(@"Error inserting track: %@", error);
|
|
835
|
+
continue;
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
currentTime = CMTimeAdd(currentTime, asset.duration);
|
|
839
|
+
totalDuration += CMTimeGetSeconds(asset.duration);
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
// Create output file
|
|
843
|
+
NSURL *outputURL = [self getMergedFileURL];
|
|
844
|
+
|
|
845
|
+
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetAppleM4A];
|
|
846
|
+
exportSession.outputURL = outputURL;
|
|
847
|
+
exportSession.outputFileType = AVFileTypeAppleM4A;
|
|
848
|
+
|
|
849
|
+
[exportSession exportAsynchronouslyWithCompletionHandler:^{
|
|
850
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
851
|
+
if (exportSession.status == AVAssetExportSessionStatusCompleted) {
|
|
852
|
+
// Update audioFilename to point to merged file
|
|
853
|
+
self.audioFilename = outputURL;
|
|
854
|
+
|
|
855
|
+
NSString *jsonString = [NSString stringWithFormat:@"{\"success\": true, \"mergedPath\": \"%@\", \"totalDuration\": %f}",
|
|
856
|
+
[outputURL path],
|
|
857
|
+
totalDuration];
|
|
858
|
+
callback(@[jsonString]);
|
|
859
|
+
} else {
|
|
860
|
+
NSString *errorMsg = exportSession.error ? [exportSession.error localizedDescription] : @"Unknown error";
|
|
861
|
+
NSString *jsonString = [NSString stringWithFormat:@"{\"success\": false, \"error\": \"%@\"}", errorMsg];
|
|
862
|
+
callback(@[jsonString]);
|
|
863
|
+
}
|
|
864
|
+
});
|
|
865
|
+
}];
|
|
866
|
+
});
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
/**
|
|
870
|
+
* Play multiple segments sequentially.
|
|
871
|
+
* Uses AVQueuePlayer for seamless playback.
|
|
872
|
+
* @validates Requirements 11.4
|
|
873
|
+
*/
|
|
874
|
+
RCT_EXPORT_METHOD(playSegments:(NSArray<NSString *> *)segmentPaths callback:(RCTResponseSenderBlock)callback) {
|
|
875
|
+
if (segmentPaths == nil || segmentPaths.count == 0) {
|
|
876
|
+
callback(@[@"{\"success\": false, \"error\": \"No segments to play\"}"]);
|
|
877
|
+
return;
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
// Stop any existing playback
|
|
881
|
+
if (self.queuePlayer) {
|
|
882
|
+
[self.queuePlayer pause];
|
|
883
|
+
if (self.playbackObserver) {
|
|
884
|
+
[self.queuePlayer removeTimeObserver:self.playbackObserver];
|
|
885
|
+
self.playbackObserver = nil;
|
|
886
|
+
}
|
|
887
|
+
self.queuePlayer = nil;
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
// Create player items for each segment
|
|
891
|
+
NSMutableArray<AVPlayerItem *> *playerItems = [NSMutableArray array];
|
|
892
|
+
for (NSString *segmentPath in segmentPaths) {
|
|
893
|
+
NSURL *segmentURL = [NSURL fileURLWithPath:segmentPath];
|
|
894
|
+
AVPlayerItem *item = [AVPlayerItem playerItemWithURL:segmentURL];
|
|
895
|
+
[playerItems addObject:item];
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
// Create queue player
|
|
899
|
+
self.queuePlayer = [AVQueuePlayer queuePlayerWithItems:playerItems];
|
|
900
|
+
self.currentSegmentIndex = 0;
|
|
901
|
+
|
|
902
|
+
// Add observer for playback completion
|
|
903
|
+
__weak typeof(self) weakSelf = self;
|
|
904
|
+
[[NSNotificationCenter defaultCenter] addObserverForName:AVPlayerItemDidPlayToEndTimeNotification
|
|
905
|
+
object:nil
|
|
906
|
+
queue:[NSOperationQueue mainQueue]
|
|
907
|
+
usingBlock:^(NSNotification *note) {
|
|
908
|
+
__strong typeof(weakSelf) strongSelf = weakSelf;
|
|
909
|
+
if (!strongSelf) return;
|
|
910
|
+
if (strongSelf.queuePlayer.currentItem == note.object) {
|
|
911
|
+
strongSelf.currentSegmentIndex++;
|
|
912
|
+
if (strongSelf.currentSegmentIndex >= segmentPaths.count) {
|
|
913
|
+
// All segments played
|
|
914
|
+
if (strongSelf->hasListeners) {
|
|
915
|
+
[strongSelf sendEventWithName:@"status" body:@{@"state": @"playbackComplete"}];
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
}];
|
|
920
|
+
|
|
921
|
+
[self.queuePlayer play];
|
|
922
|
+
|
|
923
|
+
NSString *jsonString = [NSString stringWithFormat:@"{\"success\": true, \"segmentCount\": %lu}", (unsigned long)segmentPaths.count];
|
|
924
|
+
callback(@[jsonString]);
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
/**
|
|
928
|
+
* Delete all segment files.
|
|
929
|
+
* @validates Requirements 11.5
|
|
930
|
+
*/
|
|
931
|
+
RCT_EXPORT_METHOD(deleteSegments:(NSArray<NSString *> *)segmentPaths callback:(RCTResponseSenderBlock)callback) {
|
|
932
|
+
if (segmentPaths == nil || segmentPaths.count == 0) {
|
|
933
|
+
callback(@[@"{\"success\": true, \"deletedCount\": 0}"]);
|
|
934
|
+
return;
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
NSFileManager *fileManager = [NSFileManager defaultManager];
|
|
938
|
+
NSInteger deletedCount = 0;
|
|
939
|
+
|
|
940
|
+
for (NSString *segmentPath in segmentPaths) {
|
|
941
|
+
NSError *error = nil;
|
|
942
|
+
if ([fileManager removeItemAtPath:segmentPath error:&error]) {
|
|
943
|
+
deletedCount++;
|
|
944
|
+
} else {
|
|
945
|
+
NSLog(@"Error deleting segment: %@, error: %@", segmentPath, error);
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
// Clear internal segments array
|
|
950
|
+
[self.segmentPaths removeAllObjects];
|
|
951
|
+
|
|
952
|
+
NSString *jsonString = [NSString stringWithFormat:@"{\"success\": true, \"deletedCount\": %ld}", (long)deletedCount];
|
|
953
|
+
callback(@[jsonString]);
|
|
954
|
+
}
|
|
955
|
+
|
|
956
|
+
/**
|
|
957
|
+
* Get all current segment paths.
|
|
958
|
+
*/
|
|
959
|
+
RCT_EXPORT_METHOD(getSegmentPaths:(RCTResponseSenderBlock)callback) {
|
|
960
|
+
if (self.segmentPaths == nil || self.segmentPaths.count == 0) {
|
|
961
|
+
callback(@[@"{\"success\": true, \"segments\": []}"]);
|
|
962
|
+
return;
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
NSMutableArray<NSString *> *paths = [NSMutableArray array];
|
|
966
|
+
for (NSURL *url in self.segmentPaths) {
|
|
967
|
+
[paths addObject:[url path]];
|
|
968
|
+
}
|
|
969
|
+
|
|
970
|
+
NSError *error = nil;
|
|
971
|
+
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:paths options:0 error:&error];
|
|
972
|
+
NSString *pathsJson = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
|
|
973
|
+
|
|
974
|
+
NSString *jsonString = [NSString stringWithFormat:@"{\"success\": true, \"segments\": %@}", pathsJson];
|
|
975
|
+
callback(@[jsonString]);
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
/**
|
|
979
|
+
* Clear all segments and reset state.
|
|
980
|
+
*/
|
|
981
|
+
RCT_EXPORT_METHOD(clearSegments:(RCTResponseSenderBlock)callback) {
|
|
982
|
+
[self.segmentPaths removeAllObjects];
|
|
983
|
+
self.currentSegmentIndex = 0;
|
|
984
|
+
|
|
985
|
+
if (self.queuePlayer) {
|
|
986
|
+
[self.queuePlayer pause];
|
|
987
|
+
if (self.playbackObserver) {
|
|
988
|
+
[self.queuePlayer removeTimeObserver:self.playbackObserver];
|
|
989
|
+
self.playbackObserver = nil;
|
|
990
|
+
}
|
|
991
|
+
self.queuePlayer = nil;
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
callback(@[@"{\"success\": true}"]);
|
|
995
|
+
}
|
|
996
|
+
|
|
997
|
+
- (NSURL *)getMergedFileURL {
|
|
998
|
+
self.dateFormatter = [[NSDateFormatter alloc] init];
|
|
999
|
+
self.dateFormatter.dateFormat = @"yyyyMMddHHmmss";
|
|
1000
|
+
NSURL *path = [[self getDocumentsDirectory] URLByAppendingPathComponent:[NSString stringWithFormat:@"audio-merged-%@.m4a", [self.dateFormatter stringFromDate:[NSDate date]]]];
|
|
1001
|
+
return path;
|
|
1002
|
+
}
|
|
1003
|
+
|
|
577
1004
|
- (NSURL *) saveImage:(NSString *) imageName image:(UIImage *) image {
|
|
578
1005
|
NSFileManager *defaultManager = [NSFileManager defaultManager];
|
|
579
1006
|
NSURL *documentUrl = [defaultManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask].firstObject;
|