rolexx commited on
Commit
6845a0a
·
1 Parent(s): f89ae7e
Files changed (1) hide show
  1. src/components/intro/Intro.tsx +32 -23
src/components/intro/Intro.tsx CHANGED
@@ -41,40 +41,49 @@ const IntroScene: FC<IntroSceneProps> = ({
41
  }) => {
42
  const audioRef = useRef<HTMLAudioElement | null>(null);
43
 
44
- const handleContinue = () => {
45
- setNextScene();
46
- };
47
-
48
  useEffect(() => {
49
-
50
- let audio: HTMLAudioElement | undefined;
51
-
52
- if (language === 'en') {
53
- audio = new Audio('/sounds/intro_en.mp3');
54
- } else if (language === 'fr') {
55
- audio = new Audio('/sounds/intro_fr.mp3');
56
- } else if (language === 'es') {
57
- audio = new Audio('/sounds/intro_es.mp3');
58
- }
59
 
60
- if (!audio) return; // Add early return if no audio is set
 
 
 
 
 
 
 
 
61
 
62
- const playAudio = () => {
63
- audio.play().catch(error => {
 
64
  console.log('Audio playback error:', error);
65
- });
66
  };
67
 
68
- audio.addEventListener('canplaythrough', playAudio, { once: true });
69
 
70
  return () => {
71
- audio.removeEventListener('canplaythrough', playAudio);
72
  if (audioRef.current) {
73
  audioRef.current.pause();
74
- audioRef.current.currentTime = 0;
 
75
  }
76
  };
77
- }, []);
 
 
 
 
 
 
 
 
 
 
78
 
79
  return (
80
  <div className="relative w-screen h-screen">
@@ -96,7 +105,7 @@ const IntroScene: FC<IntroSceneProps> = ({
96
  </p>
97
 
98
  <button
99
- onClick={handleContinue}
100
  className={`px-8 py-4 text-xl font-bold text-white rounded-lg transition-colors roboto-slab
101
  ${story ? 'bg-blue-600 hover:bg-blue-700' : 'bg-gray-500 cursor-not-allowed'}`}
102
  disabled={!story}
 
41
  }) => {
42
  const audioRef = useRef<HTMLAudioElement | null>(null);
43
 
 
 
 
 
44
  useEffect(() => {
45
+ const audio = new Audio();
46
+ audio.id = 'introAudio';
47
+ document.body.appendChild(audio);
48
+ audioRef.current = audio;
 
 
 
 
 
 
49
 
50
+ const loadAndPlayAudio = async () => {
51
+ try {
52
+ if (language === 'en') {
53
+ audio.src = '/sounds/intro_en.mp3';
54
+ } else if (language === 'fr') {
55
+ audio.src = '/sounds/intro_fr.mp3';
56
+ } else if (language === 'es') {
57
+ audio.src = '/sounds/intro_es.mp3';
58
+ }
59
 
60
+ await audio.load();
61
+ await audio.play();
62
+ } catch (error) {
63
  console.log('Audio playback error:', error);
64
+ }
65
  };
66
 
67
+ loadAndPlayAudio();
68
 
69
  return () => {
 
70
  if (audioRef.current) {
71
  audioRef.current.pause();
72
+ document.body.removeChild(audioRef.current);
73
+ audioRef.current = null;
74
  }
75
  };
76
+ }, [language]);
77
+
78
+ const handleContinue = () => {
79
+ console.log('handleContinue:')
80
+ if (audioRef.current) {
81
+ audioRef.current.pause();
82
+ document.body.removeChild(audioRef.current);
83
+ audioRef.current = null;
84
+ }
85
+ setNextScene();
86
+ };
87
 
88
  return (
89
  <div className="relative w-screen h-screen">
 
105
  </p>
106
 
107
  <button
108
+ onClick={() => handleContinue()}
109
  className={`px-8 py-4 text-xl font-bold text-white rounded-lg transition-colors roboto-slab
110
  ${story ? 'bg-blue-600 hover:bg-blue-700' : 'bg-gray-500 cursor-not-allowed'}`}
111
  disabled={!story}