Skip to content

Commit d532cee

Browse files
committed
fix: async audio progress experience
1 parent 6db1d0e commit d532cee

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

package/src/components/Attachment/AudioAttachment.tsx

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
VideoProgressData,
1717
VideoSeekResponse,
1818
} from '../../native';
19-
import type { FileUpload } from '../../types/types';
19+
import { FileTypes, type FileUpload } from '../../types/types';
2020
import { getTrimmedAttachmentTitle } from '../../utils/getTrimmedAttachmentTitle';
2121
import { ProgressControl } from '../ProgressControl/ProgressControl';
2222
import { WaveProgressBar } from '../ProgressControl/WaveProgressBar';
@@ -52,19 +52,34 @@ export const AudioAttachment = (props: AudioAttachmentProps) => {
5252
} = props;
5353
const { changeAudioSpeed, pauseAudio, playAudio, seekAudio } = useAudioPlayer({ soundRef });
5454
const isExpoCLI = SDK === 'stream-chat-expo';
55+
const isVoiceRecording = item.type === FileTypes.VoiceRecording;
5556

5657
/** This is for Native CLI Apps */
5758
const handleLoad = (payload: VideoPayloadData) => {
58-
onLoad(item.id, item.duration || payload.duration);
59+
// The duration given by the rn-video is not same as the one of the voice recording, so we take the actual duration for voice recording.
60+
if (isVoiceRecording && item.duration) {
61+
onLoad(item.id, item.duration);
62+
} else {
63+
onLoad(item.id, item.duration || payload.duration);
64+
}
5965
};
6066

6167
/** This is for Native CLI Apps */
6268
const handleProgress = (data: VideoProgressData) => {
6369
const { currentTime, seekableDuration } = data;
64-
if (currentTime < seekableDuration && !audioFinished) {
65-
onProgress(item.id, currentTime / seekableDuration);
70+
// The duration given by the rn-video is not same as the one of the voice recording, so we take the actual duration for voice recording.
71+
if (isVoiceRecording && item.duration) {
72+
if (currentTime < item.duration && !audioFinished) {
73+
onProgress(item.id, currentTime / item.duration);
74+
} else {
75+
setAudioFinished(true);
76+
}
6677
} else {
67-
setAudioFinished(true);
78+
if (currentTime < seekableDuration && !audioFinished) {
79+
onProgress(item.id, currentTime / seekableDuration);
80+
} else {
81+
setAudioFinished(true);
82+
}
6883
}
6984
};
7085

@@ -129,10 +144,23 @@ export const AudioAttachment = (props: AudioAttachmentProps) => {
129144
// This is done for Expo CLI where we don't get file duration from file picker
130145
if (item.duration === 0) {
131146
onLoad(item.id, durationMillis / 1000);
147+
} else {
148+
// The duration given by the expo-av is not same as the one of the voice recording, so we take the actual duration for voice recording.
149+
if (isVoiceRecording && item.duration) {
150+
onLoad(item.id, item.duration);
151+
} else {
152+
onLoad(item.id, durationMillis / 1000);
153+
}
132154
}
133155
// Update your UI for the loaded state
134156
if (playbackStatus.isPlaying) {
135-
onProgress(item.id, positionMillis / durationMillis);
157+
if (isVoiceRecording && item.duration) {
158+
if (positionMillis <= item.duration * 1000) {
159+
onProgress(item.id, positionMillis / (item.duration * 1000));
160+
}
161+
} else {
162+
onProgress(item.id, positionMillis / durationMillis);
163+
}
136164
} else {
137165
// Update your UI for the paused state
138166
}
@@ -355,6 +383,7 @@ export const AudioAttachment = (props: AudioAttachmentProps) => {
355383
const styles = StyleSheet.create({
356384
audioInfo: {
357385
flexDirection: 'row',
386+
alignItems: 'center',
358387
},
359388
centerContainer: {
360389
flexGrow: 1,

package/src/components/Attachment/FileAttachmentGroup.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ const FileAttachmentGroupWithContext = <
128128
id: index.toString(),
129129
paused: file.paused,
130130
progress: file.progress,
131+
type: file.type,
131132
}}
132133
onLoad={onLoad}
133134
onPlayPause={onPlayPause}

package/src/native.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ export type SoundReturnType = {
160160
play?: () => void;
161161
playAsync?: () => void;
162162
rate?: number;
163-
replayAsync?: () => void;
163+
replayAsync?: (status: Partial<AVPlaybackStatusToSet>) => void;
164164
resizeMode?: string;
165165
resume?: () => void;
166166
seek?: (progress: number, tolerance?: number) => void;

0 commit comments

Comments
 (0)