Skip to content

Commit

Permalink
Merge pull request #184 from ajinkyapandetekdi/fillintheblanks-all-de…
Browse files Browse the repository at this point in the history
…v-tn

Issueid #229207 fix: Even after clicking on stop button,audio keeps p…
  • Loading branch information
gouravmore authored Oct 24, 2024
2 parents 3041c0c + bbddb57 commit 19df211
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 31 deletions.
1 change: 1 addition & 0 deletions src/utils/AudioCompare.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ const AudioRecorder = (props) => {
<Box
sx={{
marginLeft: props.isShowCase ? "" : "35px",
cursor: "pointer",
}}
>
{props.recordedAudio ? (
Expand Down
99 changes: 68 additions & 31 deletions src/utils/VoiceAnalyser.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ function VoiceAnalyser(props) {
const [currentIndex, setCurrentIndex] = useState();
const [temp_audio, set_temp_audio] = useState(null);
const [isStudentAudioPlaying, setIsStudentAudioPlaying] = useState(false);
const [temp_Student_audio, set_temp_Student_audio] = useState(null);
const { callUpdateLearner } = props;
const lang = getLocalData("lang");
const { livesData, setLivesData } = props;
Expand Down Expand Up @@ -107,7 +108,11 @@ function VoiceAnalyser(props) {
audio.addEventListener("canplaythrough", () => {
set_temp_audio(audio);
setPauseAudio(val);
audio.play();
if (val) {
audio.play();
} else {
audio.pause();
}
});

audio.addEventListener("error", (e) => {
Expand All @@ -127,20 +132,50 @@ function VoiceAnalyser(props) {
}
try {
const audio = new Audio(recordedAudio);

if (val) {
audio.play();
setIsStudentAudioPlaying(true);
audio.onended = () => setIsStudentAudioPlaying(false);
} else {
audio.pause();
audio.addEventListener("canplaythrough", () => {
setIsStudentAudioPlaying(val);
set_temp_Student_audio(audio);
if (val) {
audio.play();
audio.onended = () => setIsStudentAudioPlaying(false);
} else {
audio.pause();
}
});
audio.addEventListener("error", (e) => {
console.error("Audio failed to load", e);
setIsStudentAudioPlaying(false);
}
alert("Failed to load the audio. Please try again.");
});
} catch (err) {
console.log(err);
}
};

useEffect(() => {
if (temp_Student_audio !== null) {
if (!isStudentAudioPlaying) {
temp_Student_audio.pause();
props.setVoiceAnimate(false);
} else {
temp_Student_audio.play();
props.setVoiceAnimate(true);
}
temp_Student_audio.onended = function () {
setPauseAudio(false);
props.setVoiceAnimate(false);
};
temp_Student_audio.addEventListener("ended", () =>
setIsStudentAudioPlaying(false)
);
}
return () => {
if (temp_Student_audio !== null) {
temp_Student_audio.pause();
}
};
}, [temp_Student_audio]);

useEffect(() => {
if (temp_audio !== null) {
if (!pauseAudio) {
Expand Down Expand Up @@ -671,28 +706,30 @@ function VoiceAnalyser(props) {
}
})()
)}
<Box sx={{ display: "flex", justifyContent: "center", mt: 4 }}>
{props.enableNext && (
<Box
sx={{ cursor: "pointer" }}
onClick={() => {
if (
props.pageName === "wordsorimage" ||
props.pageName === "m5"
) {
props.updateStoredData(recordedAudio, isMatching);
}
if (props.setIsNextButtonCalled) {
props.setIsNextButtonCalled(true);
} else {
props.handleNext();
}
}}
>
<NextButtonRound />
</Box>
)}
</Box>
{!loader && (
<Box sx={{ display: "flex", justifyContent: "center", mt: 4 }}>
{props.enableNext && (
<Box
sx={{ cursor: "pointer" }}
onClick={() => {
if (
props.pageName === "wordsorimage" ||
props.pageName === "m5"
) {
props.updateStoredData(recordedAudio, isMatching);
}
if (props.setIsNextButtonCalled) {
props.setIsNextButtonCalled(true);
} else {
props.handleNext();
}
}}
>
<NextButtonRound />
</Box>
)}
</Box>
)}
</div>
);
}
Expand Down

0 comments on commit 19df211

Please sign in to comment.