fixed issues with bad state reset and play pause gestion
This commit is contained in:
@@ -38,6 +38,18 @@ const getCursorToPlay = (
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const startResumePauseWatch = (stopwatch: ReturnType<typeof useStopwatch>, shouldPlay: boolean) => {
|
||||||
|
if (shouldPlay) {
|
||||||
|
if (stopwatch.isPaused()) {
|
||||||
|
stopwatch.resume();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
stopwatch.start();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
stopwatch.pause();
|
||||||
|
};
|
||||||
|
|
||||||
const transitionDuration = 200;
|
const transitionDuration = 200;
|
||||||
|
|
||||||
const PartitionMagic = ({ songID }: ParitionMagicProps) => {
|
const PartitionMagic = ({ songID }: ParitionMagicProps) => {
|
||||||
@@ -83,7 +95,13 @@ const PartitionMagic = ({ songID }: ParitionMagicProps) => {
|
|||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
if (isPartitionSvgLoaded && !isLoading && (melodySound.current?._loaded || isPianoLoaded)) {
|
if (isPartitionSvgLoaded && !isLoading && (melodySound.current?._loaded || isPianoLoaded)) {
|
||||||
setPartitionState('ready');
|
setPartitionState('ready');
|
||||||
|
} else if (partitionState !== 'loading') {
|
||||||
|
setPartitionState('loading');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
setPartitionState('loading');
|
||||||
|
};
|
||||||
}, [isPartitionSvgLoaded, isLoading, melodySound.current?._loaded, isPianoLoaded]);
|
}, [isPartitionSvgLoaded, isLoading, melodySound.current?._loaded, isPianoLoaded]);
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
@@ -134,26 +152,17 @@ const PartitionMagic = ({ songID }: ParitionMagicProps) => {
|
|||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
if (Platform.OS === 'web') {
|
if (Platform.OS === 'web') {
|
||||||
if (!piano.current || !isPianoLoaded) return;
|
if (!piano.current || !isPianoLoaded) return;
|
||||||
shouldPlay ? stopwatch.start() : stopwatch.pause();
|
startResumePauseWatch(stopwatch, shouldPlay);
|
||||||
setPartitionState(shouldPlay ? 'playing' : 'paused');
|
setPartitionState(shouldPlay ? 'playing' : 'paused');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!melodySound.current || !melodySound.current._loaded) return;
|
if (!melodySound.current || !melodySound.current._loaded) return;
|
||||||
shouldPlay ? stopwatch.start() : stopwatch.pause();
|
startResumePauseWatch(stopwatch, shouldPlay);
|
||||||
|
setPartitionState(shouldPlay ? 'playing' : 'paused');
|
||||||
if (shouldPlay) {
|
if (shouldPlay) {
|
||||||
melodySound.current
|
melodySound.current.playAsync().catch(console.error);
|
||||||
.playAsync()
|
|
||||||
.then(() => {
|
|
||||||
setPartitionState('playing');
|
|
||||||
})
|
|
||||||
.catch(console.error);
|
|
||||||
} else {
|
} else {
|
||||||
melodySound.current
|
melodySound.current.pauseAsync().catch(console.error);
|
||||||
.pauseAsync()
|
|
||||||
.then(() => {
|
|
||||||
setPartitionState('paused');
|
|
||||||
})
|
|
||||||
.catch(console.error);
|
|
||||||
}
|
}
|
||||||
}, [shouldPlay]);
|
}, [shouldPlay]);
|
||||||
|
|
||||||
|
|||||||
@@ -27,6 +27,14 @@ const PlayViewControlBar = ({ song }: PlayViewControlBarProps) => {
|
|||||||
const textColor = colors.text;
|
const textColor = colors.text;
|
||||||
const isPlaying = partitionState === 'playing';
|
const isPlaying = partitionState === 'playing';
|
||||||
const disabled = partitionState === 'loading' || partitionState === 'error';
|
const disabled = partitionState === 'loading' || partitionState === 'error';
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
return () => {
|
||||||
|
setShouldPlay(false);
|
||||||
|
setShouldEnd(false);
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Row
|
<Row
|
||||||
style={{
|
style={{
|
||||||
|
|||||||
Reference in New Issue
Block a user