Skip to content

Commit d887f5a

Browse files
authored
cherry pick about support isLoop audio player (#929)
1 parent 53c3822 commit d887f5a

File tree

4 files changed

+19
-0
lines changed

4 files changed

+19
-0
lines changed

example/src/AudioPlayerExample.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ export default function AudioPlayerExample() {
1717
interruptionMode="stop"
1818
/>
1919
</Section>
20+
<Section style={{}} title="Loop">
21+
<AudioPlayer
22+
source={require("./assets/loop.wav")}
23+
playsInBackground
24+
interruptionMode="stop"
25+
isLooping
26+
/>
27+
</Section>
2028
<Section style={{}} title="Custom styling">
2129
<AudioPlayer
2230
style={{

packages/core/src/__tests__/components/AudioPlayer.test.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const mockAudioSource = {
1212
const mockPlayAsync = jest.fn();
1313
const mockPauseAsync = jest.fn();
1414
const mockUnloadAsync = jest.fn();
15+
const mockSetIsLoopAsync = jest.fn();
1516

1617
// To ignore the warning: 'When testing, code that causes React state updates should be wrapped into act'
1718
// This is caused because state is updated in the useEffect, this is intentional and not an issue, so we can ignore it
@@ -44,6 +45,7 @@ jest.mock("expo-av", () => {
4445
positionMillis: position,
4546
});
4647
},
48+
setIsLoopingAsync: mockSetIsLoopAsync,
4749
},
4850
};
4951
},

packages/core/src/components/MediaPlayer/AudioPlayer/AudioPlayerCommon.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface HeadlessAudioPlayerProps extends MediaPlayerProps {
88
playsInBackground?: boolean;
99
playsInSilentModeIOS?: boolean;
1010
playThroughEarpieceAndroid?: boolean;
11+
isLooping?: boolean;
1112
}
1213

1314
export interface AudioPlayerInterfaceProps {

packages/core/src/components/MediaPlayer/AudioPlayer/HeadlessAudioPlayer.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,17 @@ const HeadlessAudioPlayer = React.forwardRef<
3131
playThroughEarpieceAndroid = false,
3232
onPlaybackStatusUpdate: onPlaybackStatusUpdateProp,
3333
onPlaybackFinish,
34+
isLooping = false,
3435
},
3536
ref
3637
) => {
3738
const [currentSound, setCurrentSound] = React.useState<Audio.Sound>();
3839
const [isPlaying, setIsPlaying] = React.useState(false);
3940

41+
React.useEffect(() => {
42+
currentSound?.setIsLoopingAsync(isLooping);
43+
}, [currentSound, isLooping]);
44+
4045
const updateAudioMode = React.useCallback(async () => {
4146
try {
4247
await Audio.setAudioModeAsync({
@@ -71,6 +76,9 @@ const HeadlessAudioPlayer = React.forwardRef<
7176

7277
if (status.isLoaded) {
7378
if (status.didJustFinish) {
79+
if (isLooping) {
80+
return;
81+
}
7482
onPlaybackFinish?.();
7583
}
7684
setIsPlaying(status.isPlaying);

0 commit comments

Comments
 (0)