Skip to content

Commit

Permalink
exit player page stops audio
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinekllee committed Jul 17, 2023
1 parent 65aeceb commit db35164
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
4 changes: 4 additions & 0 deletions backend/controllers/track.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ const updateTrackRating = async (req, res) => {
try {
const { patientId, trackId, rating } = req.body;

console.log ("patientId: " + patientId)
console.log ("trackId: " + trackId)
console.log ("rating: " + rating)

if (!patientId || !trackId || rating === undefined)
return res
.status(400)
Expand Down
37 changes: 29 additions & 8 deletions frontend/app/components/AudioPlayerComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,35 @@ function AudioPlayerComponent(props) {
const [isLooping, setIsLooping] = useState(false);
const [trackFinished, setTrackFinished] = useState(false); // New state to track if the track has finished

// useFocusEffect(
// useCallback(() => {
// return () => {
// audio?.stopAsync();
// setIsPlaying(false);
// };
// }, [])
// );
useEffect(() => {
if (audio) {
audio.setOnPlaybackStatusUpdate((status) => {
setPosition(status.positionMillis);

const totalSeconds = Math.floor(status.positionMillis / 1000);
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;
setElapsedTime(
`${minutes}:${seconds < 10 ? "0" : ""}${seconds}`
);

if (status.didJustFinish) {
if (isLooping) {
// TODO: increase rating for this track by 1
audio.replayAsync(); // Replay track if isLooping is true
} else {
setTrackFinished(true); // Set trackFinished to true if the track has finished
}
}
});

return async () => {
// This is the cleanup function that React will run when the component unmounts
await audio.stopAsync();
await audio.unloadAsync();
};
}
}, [audio, isLooping]);

useEffect(() => {
if (audio) {
Expand Down
3 changes: 3 additions & 0 deletions frontend/app/screens/PlayerScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ const PlayerScreen = ({ route, navigation }) => {
const nextTrack = async () => {
if (isPreloading) {
console.log("WAITING FOR PRELOADING TO FINISH");
if (audio) {
audio.stopAsync();
}
setUsePreloadedImmediately(true);
setIsLoading(true);
return
Expand Down

0 comments on commit db35164

Please sign in to comment.