-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: 레이싱 게임의 오디오 제어 함수들을 utils로 빼서 모듈화 (CC-162)
- Loading branch information
1 parent
481ac02
commit cf9d83a
Showing
2 changed files
with
41 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
export const playAudio = (audio: HTMLAudioElement | null) => { | ||
if(audio){ | ||
resetAudio(audio); | ||
const playPromise = audio.play(); | ||
|
||
if(playPromise !== undefined) { | ||
playPromise.catch(error => { | ||
console.error("Audio play error:", error); | ||
}); | ||
} | ||
} | ||
}; | ||
|
||
export const resetAudio = (audio: HTMLAudioElement | null) => { | ||
if(audio){ | ||
audio.pause(); | ||
audio.currentTime = 0; | ||
audio.volume = 1.0; | ||
audio.load(); | ||
} | ||
}; | ||
|
||
export const fadeOutAudio = (audio: HTMLAudioElement | null, duration: number, callback: () => void) => { | ||
if(!audio) return; | ||
|
||
const step = 0.1; | ||
const fadeInterval = duration / (audio.volume / step); | ||
|
||
const fade = setInterval(() => { | ||
if (audio.volume > step) { | ||
audio.volume -= step; | ||
}else { | ||
clearInterval(fade); | ||
audio.volume = 0; | ||
audio.pause(); | ||
callback(); | ||
} | ||
}, fadeInterval); | ||
}; |