Skip to content

Commit

Permalink
Support volume prop audio player (#952)
Browse files Browse the repository at this point in the history
* support volume prop

* add adjust volume example
  • Loading branch information
sieu-db authored Oct 6, 2024
1 parent 18fd15a commit a5f0084
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions example/src/AudioPlayerExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ export default function AudioPlayerExample() {
isLooping
/>
</Section>
<Section style={{}} title="Adjust volume">
<AudioPlayer
source={require("./assets/loop.wav")}
playsInBackground
interruptionMode="stop"
volume={0.3}
/>
</Section>
<Section style={{}} title="Custom styling">
<AudioPlayer
style={{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface HeadlessAudioPlayerProps extends MediaPlayerProps {
playsInSilentModeIOS?: boolean;
playThroughEarpieceAndroid?: boolean;
isLooping?: boolean;
volume?: number;
}

export interface AudioPlayerInterfaceProps {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const HeadlessAudioPlayer = React.forwardRef<
onPlaybackStatusUpdate: onPlaybackStatusUpdateProp,
onPlaybackFinish,
isLooping = false,
volume = 1.0,
},
ref
) => {
Expand All @@ -47,6 +48,12 @@ const HeadlessAudioPlayer = React.forwardRef<
}
}, [currentSound, isLooping]);

React.useEffect(() => {
if (currentSound && typeof currentSound?.setVolumeAsync === "function") {
currentSound.setVolumeAsync(volume);
}
}, [currentSound, volume]);

const updateAudioMode = React.useCallback(async () => {
try {
await Audio.setAudioModeAsync({
Expand Down

0 comments on commit a5f0084

Please sign in to comment.