Skip to content

Commit f8c9b9a

Browse files
authored
feat: manage audio centrally in the SDK (#3288)
This pull request refactors and modernizes the audio attachment components, improving state management, playback control, and code clarity. The most significant changes are the migration to a centralized audio player state store, deprecation of legacy callback props, and improved handling for both audio and voice recording attachments. These updates enhance maintainability and user experience across both Expo and native platforms. Starting this PR, integrators can choose to have only a single player instance at a time which can be controlled using the `allowConcurrentAudioPlayback` prop of the Channel component where `false` is the new default. This behaviour is done in parity with React SDK and as per decision. ### **Audio Attachment Component Refactor and State Management Improvements** * Migrated audio playback state (play/pause, progress, duration, playback rate) from local props and callbacks to a centralized state store via `useAudioPlayerControl` and `useStateStore`, removing the need for deprecated callback props like `onLoad`, `onPlayPause`, and `onProgress`. [[1]](diffhunk://#diff-d9e0483e9e85e351477d959f53b229e12d576be2e9bfa5bbff4dc04b70c5de0bL1-R176) [[2]](diffhunk://#diff-d9e0483e9e85e351477d959f53b229e12d576be2e9bfa5bbff4dc04b70c5de0bL273-R205) * Updated UI and logic to use new state values (`isPlaying`, `progress`, `duration`, `currentPlaybackRate`) for rendering controls, progress bars, and playback speed, replacing legacy props and local state. [[1]](diffhunk://#diff-d9e0483e9e85e351477d959f53b229e12d576be2e9bfa5bbff4dc04b70c5de0bL311-R231) [[2]](diffhunk://#diff-d9e0483e9e85e351477d959f53b229e12d576be2e9bfa5bbff4dc04b70c5de0bL347-R277) [[3]](diffhunk://#diff-d9e0483e9e85e351477d959f53b229e12d576be2e9bfa5bbff4dc04b70c5de0bL370-R291) [[4]](diffhunk://#diff-d9e0483e9e85e351477d959f53b229e12d576be2e9bfa5bbff4dc04b70c5de0bL380-R300) [[5]](diffhunk://#diff-d9e0483e9e85e351477d959f53b229e12d576be2e9bfa5bbff4dc04b70c5de0bL393-R313) ### **Voice Recording and Audio Attachment Handling** * Improved detection and handling of voice recordings using the new `isVoiceRecordingAttachment` helper, ensuring correct duration and title display, and simplifying logic for both audio and voice recording types. [[1]](diffhunk://#diff-d9e0483e9e85e351477d959f53b229e12d576be2e9bfa5bbff4dc04b70c5de0bL331-R251) [[2]](diffhunk://#diff-689763e88588fba421d39abbb74f3ca6853eac3cf45f74f1dc95855bbea86f99L4-R4) ### **File Attachment Group Updates** * Deprecated legacy callback props (`onLoad`, `onProgress`) in `FileAttachmentGroup`, updating documentation and usage to encourage migration to the new state management approach. Also added `message` prop for better context handling. [[1]](diffhunk://#diff-689763e88588fba421d39abbb74f3ca6853eac3cf45f74f1dc95855bbea86f99L20-R23) [[2]](diffhunk://#diff-689763e88588fba421d39abbb74f3ca6853eac3cf45f74f1dc95855bbea86f99L52-R59) [[3]](diffhunk://#diff-689763e88588fba421d39abbb74f3ca6853eac3cf45f74f1dc95855bbea86f99L62-R76) ### **Native and Expo Audio Player Integration** * Improved Expo audio player integration by subscribing to status events earlier and fixing seek logic to use seconds instead of milliseconds, ensuring more accurate playback control. [[1]](diffhunk://#diff-236979f75b937086a72522395304eefdd19108179da9b8a76fa32ed31032da12R102-R103) [[2]](diffhunk://#diff-236979f75b937086a72522395304eefdd19108179da9b8a76fa32ed31032da12L139-R141) [[3]](diffhunk://#diff-037ceecf6320e1a12857962f434b303715602833cd0f4209d1dd090fdc99d55eR134-R136) ### **Progress and Wavefrom progress control performance** Improved the progress and waveform progress bar performance by **10x** as previously the animations were slow and not adhereing to the pattern that could make it fast. These changes collectively modernize the audio attachment experience, making it more robust and easier to maintain.
1 parent c4089d0 commit f8c9b9a

File tree

25 files changed

+1056
-332
lines changed

25 files changed

+1056
-332
lines changed

package/expo-package/src/optionalDependencies/Sound.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ class ExpoAudioSoundAdapter {
9999

100100
// eslint-disable-next-line require-await
101101
loadAsync = async (initialStatus) => {
102+
// We have to subscribe as early as possible so that we know the initial status(durarion, etc.) of the audio.
103+
this.subscribeStatusEventListener();
102104
this.initialShouldCorrectPitch = initialStatus.shouldCorrectPitch;
103105
this.initialPitchCorrectionQuality = initialStatus.pitchCorrectionQuality;
104106
};
@@ -136,8 +138,7 @@ class ExpoAudioSoundAdapter {
136138
};
137139

138140
// eslint-disable-next-line require-await
139-
setPositionAsync: SoundReturnType['setPositionAsync'] = async (milliseconds) => {
140-
const seconds = milliseconds / 1000;
141+
setPositionAsync: SoundReturnType['setPositionAsync'] = async (seconds) => {
141142
this.player.seekTo(seconds);
142143
};
143144

package/native-package/src/optionalDependencies/Audio.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ class _Audio {
131131
resumePlayer = async () => {
132132
await audioRecorderPlayer.resumePlayer();
133133
};
134+
seekToPlayer = async (positionInMillis: number) => {
135+
await audioRecorderPlayer.seekToPlayer(positionInMillis);
136+
};
134137
startPlayer = async (uri, _, onPlaybackStatusUpdate) => {
135138
try {
136139
const playback = await audioRecorderPlayer.startPlayer(uri);

0 commit comments

Comments
 (0)