Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audio Hook #428

Merged
merged 4 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions components/global/beat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ import { View, Text, Pressable } from "react-native";
import Entypo from "@expo/vector-icons/Entypo";
import { beat } from "@/types";
import AntDesign from "@expo/vector-icons/AntDesign";
import useAudio from "@/hooks/useAudio";

const Drop = ({ image, artist, song, location, length, onAdd }: beat) => {
const { playing, play, pause } = useAudio(
"https://www.bensound.com/bensound-music/bensound-oblivion.mp3",
);

return (
<View className="flex flex-row items-center gap-4 p-2">
<View className="w-[50] h-[50] rounded-lg overflow-hidden">
Expand All @@ -31,9 +36,16 @@ const Drop = ({ image, artist, song, location, length, onAdd }: beat) => {
</Text>
</Pressable>
)}
<View className="bg-beatdrop-primary rounded-full p-2">
<Entypo name="controller-play" size={24} color="white" />
</View>
<Pressable
className="bg-beatdrop-primary rounded-full p-2"
onPress={() => (playing ? pause : play)}
>
{playing ? (
<Entypo name="controller-paus" size={24} color="white" />
) : (
<Entypo name="controller-play" size={24} color="white" />
)}
</Pressable>
</View>
</View>
</View>
Expand Down
41 changes: 41 additions & 0 deletions hooks/useAudio.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { useState, useEffect } from "react";
import { Audio } from "expo-av";

const useAudio = (
url: string,
): { playing: boolean; play: () => void; pause: () => void } => {
const [audio, setAudio] = useState<Audio.Sound>();
const [playing, setPlaying] = useState<boolean>(false);

const play = () => setPlaying(true);
const pause = () => setPlaying(false);

useEffect(() => {
const load = async () => {
const { sound: playbackObject } = await Audio.Sound.createAsync(
{ uri: url },
{ shouldPlay: false },
);

setAudio(playbackObject);
};

if (!audio) load();

return () => {
if (audio) audio.unloadAsync();
};
}, [url, audio]);

useEffect(() => {
if (playing) {
audio?.playAsync();
} else {
audio?.pauseAsync();
}
}, [audio, playing]);

return { playing: playing, play: play, pause: pause };
};

export default useAudio;
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"react-native-screens": "3.31.1",
"react-native-simple-default-props": "^1.0.0",
"react-native-toast-message": "^2.2.0",
"react-native-web": "~0.19.10"
"react-native-web": "~0.19.10",
"expo-av": "~14.0.7"
},
"devDependencies": {
"@babel/core": "^7.20.0",
Expand Down