|
| 1 | +import type { PlaybackStatus, SoundReturnType } from 'stream-chat-react-native-core'; |
| 2 | + |
1 | 3 | import { AudioComponent } from './AudioVideo'; |
2 | 4 |
|
| 5 | +let ExpoAudioComponent; |
| 6 | +let expoCreateSoundPlayer; |
| 7 | + |
| 8 | +try { |
| 9 | + const { createAudioPlayer, AudioModule } = require('expo-audio'); |
| 10 | + ExpoAudioComponent = AudioModule; |
| 11 | + expoCreateSoundPlayer = createAudioPlayer; |
| 12 | +} catch (e) { |
| 13 | + // do nothing |
| 14 | +} |
| 15 | + |
3 | 16 | export const Sound = { |
4 | | - initializeSound: AudioComponent |
| 17 | + initializeSound: ExpoAudioComponent |
5 | 18 | ? async (source, initialStatus, onPlaybackStatusUpdate: (playbackStatus) => void) => { |
6 | | - await AudioComponent.setAudioModeAsync({ |
7 | | - playsInSilentModeIOS: true, |
| 19 | + await ExpoAudioComponent.setAudioModeAsync({ |
| 20 | + playsInSilentMode: true, |
8 | 21 | }); |
9 | | - const { sound } = await AudioComponent.Sound.createAsync( |
10 | | - source, |
11 | | - initialStatus, |
12 | | - onPlaybackStatusUpdate, |
13 | | - ); |
| 22 | + const sound = new ExpoAudioSoundAdapter(); |
| 23 | + await sound.loadAsync(source, initialStatus); |
| 24 | + sound.setOnPlaybackStatusUpdate(onPlaybackStatusUpdate); |
14 | 25 | return sound; |
15 | 26 | } |
16 | | - : null, |
| 27 | + : AudioComponent |
| 28 | + ? async (source, initialStatus, onPlaybackStatusUpdate: (playbackStatus) => void) => { |
| 29 | + await AudioComponent.setAudioModeAsync({ |
| 30 | + playsInSilentModeIOS: true, |
| 31 | + }); |
| 32 | + const { sound } = await AudioComponent.Sound.createAsync( |
| 33 | + source, |
| 34 | + initialStatus, |
| 35 | + onPlaybackStatusUpdate, |
| 36 | + ); |
| 37 | + return sound; |
| 38 | + } |
| 39 | + : null, |
17 | 40 | Player: null, |
18 | 41 | }; |
| 42 | + |
| 43 | +type ExpoAudioPlaybackStatus = { |
| 44 | + currentTime: number; |
| 45 | + didJustFinish: boolean; |
| 46 | + duration: number; |
| 47 | + id: number; |
| 48 | + isBuffering: boolean; |
| 49 | + isLoaded: boolean; |
| 50 | + loop: boolean; |
| 51 | + mute: boolean; |
| 52 | + playbackRate: number; |
| 53 | + playbackState: string; |
| 54 | + playing: boolean; |
| 55 | + reasonForWaitingToPlay: string; |
| 56 | + shouldCorrectPitch: boolean; |
| 57 | + timeControlStatus: string; |
| 58 | +}; |
| 59 | + |
| 60 | +class ExpoAudioSoundAdapter { |
| 61 | + private player; |
| 62 | + private statusEventListener; |
| 63 | + private initialPitchCorrectionQuality; |
| 64 | + private initialShouldCorrectPitch; |
| 65 | + |
| 66 | + constructor() {} |
| 67 | + |
| 68 | + setOnPlaybackStatusUpdate = ( |
| 69 | + onPlaybackStatusUpdate: (playbackStatus: PlaybackStatus) => void, |
| 70 | + ) => { |
| 71 | + this.statusEventListener = this.player.addListener( |
| 72 | + 'playbackStatusUpdate', |
| 73 | + (playbackStatus: ExpoAudioPlaybackStatus) => { |
| 74 | + onPlaybackStatusUpdate(expoAudioToExpoAvStatusAdapter(playbackStatus)); |
| 75 | + }, |
| 76 | + ); |
| 77 | + }; |
| 78 | + |
| 79 | + // eslint-disable-next-line require-await |
| 80 | + loadAsync = async (source, initialStatus) => { |
| 81 | + this.player = expoCreateSoundPlayer?.(source, initialStatus.progressUpdateIntervalMillis); |
| 82 | + this.initialShouldCorrectPitch = initialStatus.shouldCorrectPitch; |
| 83 | + this.initialPitchCorrectionQuality = initialStatus.pitchCorrectionQuality; |
| 84 | + }; |
| 85 | + |
| 86 | + // eslint-disable-next-line require-await |
| 87 | + stopAsync: SoundReturnType['stopAsync'] = async () => { |
| 88 | + this.player.seekTo(0); |
| 89 | + this.player.pause(); |
| 90 | + }; |
| 91 | + |
| 92 | + // eslint-disable-next-line require-await |
| 93 | + unloadAsync: SoundReturnType['unloadAsync'] = async () => { |
| 94 | + this.statusEventListener.remove(); |
| 95 | + this.player.remove(); |
| 96 | + }; |
| 97 | + |
| 98 | + // eslint-disable-next-line require-await |
| 99 | + playAsync: SoundReturnType['playAsync'] = async () => { |
| 100 | + this.player.play(); |
| 101 | + }; |
| 102 | + |
| 103 | + // eslint-disable-next-line require-await |
| 104 | + pauseAsync: SoundReturnType['pauseAsync'] = async () => { |
| 105 | + this.player.pause(); |
| 106 | + }; |
| 107 | + |
| 108 | + // eslint-disable-next-line require-await |
| 109 | + replayAsync: SoundReturnType['replayAsync'] = async () => { |
| 110 | + this.player.seekTo(0); |
| 111 | + }; |
| 112 | + |
| 113 | + // eslint-disable-next-line require-await |
| 114 | + setPositionAsync: SoundReturnType['setPositionAsync'] = async (milliseconds) => { |
| 115 | + const seconds = milliseconds / 1000; |
| 116 | + this.player.seekTo(seconds); |
| 117 | + }; |
| 118 | + |
| 119 | + // eslint-disable-next-line require-await |
| 120 | + setRateAsync: SoundReturnType['setRateAsync'] = async ( |
| 121 | + rate, |
| 122 | + shouldCorrectPitch = this.initialShouldCorrectPitch, |
| 123 | + pitchCorrectionQuality = this.initialPitchCorrectionQuality, |
| 124 | + ) => { |
| 125 | + if (shouldCorrectPitch && pitchCorrectionQuality) { |
| 126 | + this.player.setPlaybackRate(rate, pitchCorrectionQuality); |
| 127 | + return; |
| 128 | + } |
| 129 | + this.player.setPlaybackRate(rate); |
| 130 | + }; |
| 131 | +} |
| 132 | + |
| 133 | +const expoAudioToExpoAvStatusAdapter = ( |
| 134 | + playbackStatus: ExpoAudioPlaybackStatus, |
| 135 | +): PlaybackStatus => { |
| 136 | + const { currentTime, didJustFinish, duration, isBuffering, isLoaded, loop, mute, playing } = |
| 137 | + playbackStatus; |
| 138 | + |
| 139 | + return { |
| 140 | + currentPosition: undefined, // not present in the expo-av api, breaks things if set |
| 141 | + didJustFinish, |
| 142 | + duration: undefined, // not present in the expo-av api, breaks things if set |
| 143 | + durationMillis: duration * 1000, |
| 144 | + error: null, // TODO: check how we can see if there is an error |
| 145 | + isBuffering, |
| 146 | + isLoaded, |
| 147 | + isLooping: loop, |
| 148 | + isMuted: mute, |
| 149 | + isPlaying: playing, |
| 150 | + isSeeking: false, // we don't use this anywhere, so just defaulting to a safe value since nothing similar exists in expo-audio |
| 151 | + positionMillis: currentTime * 1000, |
| 152 | + shouldPlay: undefined, // we cannot determine whether the audio should be playing or not |
| 153 | + }; |
| 154 | +}; |
0 commit comments