Skip to content

Commit

Permalink
Merge pull request #16 from HeesuKim0203/feat/audio
Browse files Browse the repository at this point in the history
feat : Add background audio
  • Loading branch information
HeesuKim0203 authored Mar 6, 2024
2 parents 84f8d39 + 9c58994 commit f160e48
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 5 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added public/audio_off.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/audio_on.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/bgm.mp3
Binary file not shown.
4 changes: 2 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React from 'react' ;
import { BrowserRouter, Route, Routes } from 'react-router-dom' ;
import { BrowserRouter, Route, Routes } from 'react-router-dom'

import './App.css'

import Home from './page/home'
import History from './page/history'
import { HOME, HISTORY } from './util/url' ;
import { HOME, HISTORY } from './util/url'

function App() {
return (
Expand Down
39 changes: 39 additions & 0 deletions src/AudioPlayer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React, { useState, useRef } from 'react';

interface AudioPlayerProps {
src: string
}

const AudioPlayerComponent: React.FC<AudioPlayerProps> = ({ src }) => {
const audioRef = useRef<HTMLAudioElement>(null)
const [isPlaying, setIsPlaying] = useState<boolean>(false)

const handlePlay = () => {

const audio = audioRef.current

if (audio) {
setIsPlaying(!isPlaying)

if (!isPlaying) {
audio.play().catch((error) => {
console.error("Audio Error : ", error)
setIsPlaying(false)
})
} else {
audio.pause()
}
}
};

return (
<div className = 'absolute top-8 right-8 text-2xl text-white' >
<audio ref = { audioRef } src = { src } loop />
<button className = 'flex items-center' onClick = { handlePlay } >
<img className = 'w-5 h-7' src = {isPlaying ? `${ process.env.PUBLIC_URL }/audio_on.png` : `${ process.env.PUBLIC_URL }/audio_off.png`} alt = "Audio button" />
</button>
</div>
);
}

export default AudioPlayerComponent ;
6 changes: 5 additions & 1 deletion src/page/home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
BACK_BUTTON_TEXT
} from '../../util/text'
import { USER_LIFE, USER_SCORE } from '../../util/className'
import AudioPlayer from '../../AudioPlayer'

type Prop = {
displayStatus : string
Expand Down Expand Up @@ -104,7 +105,7 @@ function GameScreen({ displayStatus, setDisplayStatus } : Prop) {

function Home() {

const [ displayStatus, setDisplayStatus ] = useState<string>(START_SCREEN) ;
const [ displayStatus, setDisplayStatus ] = useState<string>(START_SCREEN)

return (
<div className = "flex flex-col relative items-center w-screen h-screen bg-[url('/public/background.png')]">
Expand All @@ -119,6 +120,9 @@ function Home() {
}
})()
}
<AudioPlayer
src = {`${ process.env.PUBLIC_URL }/bgm.mp3`}
/>
</div>
) ;
}
Expand Down

0 comments on commit f160e48

Please sign in to comment.