-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from EvandroLG/refactor/audio
Refactor the Audio module
- Loading branch information
Showing
12 changed files
with
391 additions
and
233 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,62 @@ | ||
import { Audio } from 'ts-audio'; | ||
import song from './song.mp3'; | ||
import { Audio } from 'ts-audio' | ||
|
||
const getVolume = (element) => Number(element.value) / 100; | ||
import song from './song.mp3' | ||
|
||
const range = document.getElementById('range'); | ||
const buttonPlay = document.getElementById('bt-play'); | ||
const buttonPause = document.getElementById('bt-pause'); | ||
const buttonToggle = document.getElementById('bt-toggle'); | ||
const buttonStop = document.getElementById('bt-stop'); | ||
const getVolume = (element) => Number(element.value) / 100 | ||
|
||
const range = document.getElementById('range') | ||
const buttonPlay = document.getElementById('bt-play') | ||
const buttonPause = document.getElementById('bt-pause') | ||
const buttonToggle = document.getElementById('bt-toggle') | ||
const buttonStop = document.getElementById('bt-stop') | ||
const audio = Audio({ | ||
file: song, | ||
loop: true, | ||
volume: getVolume(range), | ||
preload: true, | ||
}); | ||
}) | ||
|
||
audio.on('end', () => { | ||
buttonPlay.removeAttribute('disabled'); | ||
buttonPause.setAttribute('disabled', 'disabled'); | ||
buttonStop.setAttribute('disabled', 'disabled'); | ||
}); | ||
buttonPlay.removeAttribute('disabled') | ||
buttonPause.setAttribute('disabled', 'disabled') | ||
buttonStop.setAttribute('disabled', 'disabled') | ||
}) | ||
|
||
console.log('audio ctx', audio.audioCtx); | ||
console.log('audio ctx', audio.audioCtx) | ||
|
||
setTimeout(() => { | ||
audio.loop = false; | ||
}, 2000); | ||
audio.loop = false | ||
}, 2000) | ||
|
||
audio.on('ready', ({ data }) => console.log(data)); | ||
audio.on('start', () => console.log('start')); | ||
audio.on('state', ({ data }) => console.log(data)); | ||
audio.on('ready', ({ data }) => console.log(data)) | ||
audio.on('start', () => console.log('start')) | ||
audio.on('state', ({ data }) => console.log(data)) | ||
|
||
buttonPlay.addEventListener('click', () => { | ||
audio.play(); | ||
buttonPlay.setAttribute('disabled', 'disabled'); | ||
buttonPause.removeAttribute('disabled'); | ||
buttonStop.removeAttribute('disabled'); | ||
}); | ||
audio.play() | ||
buttonPlay.setAttribute('disabled', 'disabled') | ||
buttonPause.removeAttribute('disabled') | ||
buttonStop.removeAttribute('disabled') | ||
}) | ||
|
||
buttonPause.addEventListener('click', () => { | ||
audio.pause(); | ||
buttonPause.setAttribute('disabled', 'disabled'); | ||
buttonStop.setAttribute('disabled', 'disabled'); | ||
buttonPlay.removeAttribute('disabled'); | ||
}); | ||
audio.pause() | ||
buttonPause.setAttribute('disabled', 'disabled') | ||
buttonStop.setAttribute('disabled', 'disabled') | ||
buttonPlay.removeAttribute('disabled') | ||
}) | ||
|
||
buttonToggle.addEventListener('click', () => { | ||
audio.toggle(); | ||
}); | ||
audio.toggle() | ||
}) | ||
|
||
buttonStop.addEventListener('click', () => { | ||
audio.stop(); | ||
buttonPause.setAttribute('disabled', 'disabled'); | ||
buttonStop.setAttribute('disabled', 'disabled'); | ||
}); | ||
audio.stop() | ||
buttonPause.setAttribute('disabled', 'disabled') | ||
buttonStop.setAttribute('disabled', 'disabled') | ||
}) | ||
|
||
range.addEventListener('change', (e) => { | ||
const volume = getVolume(e.target); | ||
audio.volume = volume; | ||
}); | ||
const volume = getVolume(e.target) | ||
audio.volume = volume | ||
}) |
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 |
---|---|---|
@@ -1,72 +1,73 @@ | ||
import { AudioPlaylist } from 'ts-audio'; | ||
import songOne from './1.mp3'; | ||
import songTwo from './2.mp3'; | ||
import songThree from './3.mp3'; | ||
import { AudioPlaylist } from 'ts-audio' | ||
|
||
const getVolume = (element) => Number(element.value) / 100; | ||
const range = document.getElementById('range'); | ||
import songOne from './1.mp3' | ||
import songTwo from './2.mp3' | ||
import songThree from './3.mp3' | ||
|
||
const getVolume = (element) => Number(element.value) / 100 | ||
const range = document.getElementById('range') | ||
|
||
const playlist = AudioPlaylist({ | ||
files: { [songOne]: 1, [songTwo]: 5, [songThree]: 1 }, | ||
}); | ||
}) | ||
|
||
const buttonPlay = document.getElementById('bt-play'); | ||
const buttonPause = document.getElementById('bt-pause'); | ||
const buttonStop = document.getElementById('bt-stop'); | ||
const buttonNext = document.getElementById('bt-next'); | ||
const buttonPrev = document.getElementById('bt-prev'); | ||
const buttonToggle = document.getElementById('bt-toggle'); | ||
const buttonPlay = document.getElementById('bt-play') | ||
const buttonPause = document.getElementById('bt-pause') | ||
const buttonStop = document.getElementById('bt-stop') | ||
const buttonNext = document.getElementById('bt-next') | ||
const buttonPrev = document.getElementById('bt-prev') | ||
const buttonToggle = document.getElementById('bt-toggle') | ||
|
||
playlist.on('start', () => { | ||
console.log(playlist.audioCtx); | ||
}); | ||
console.log(playlist.audioCtx) | ||
}) | ||
|
||
playlist.on('end', () => { | ||
buttonPlay.removeAttribute('disabled'); | ||
buttonPause.setAttribute('disabled', 'disabled'); | ||
buttonStop.setAttribute('disabled', 'disabled'); | ||
}); | ||
buttonPlay.removeAttribute('disabled') | ||
buttonPause.setAttribute('disabled', 'disabled') | ||
buttonStop.setAttribute('disabled', 'disabled') | ||
}) | ||
|
||
buttonPlay.addEventListener('click', () => { | ||
playlist.play(); | ||
buttonPlay.setAttribute('disabled', 'disabled'); | ||
buttonPause.removeAttribute('disabled'); | ||
buttonStop.removeAttribute('disabled'); | ||
}); | ||
playlist.play() | ||
buttonPlay.setAttribute('disabled', 'disabled') | ||
buttonPause.removeAttribute('disabled') | ||
buttonStop.removeAttribute('disabled') | ||
}) | ||
|
||
buttonPause.addEventListener('click', () => { | ||
playlist.pause(); | ||
buttonPause.setAttribute('disabled', 'disabled'); | ||
buttonStop.setAttribute('disabled', 'disabled'); | ||
buttonPlay.removeAttribute('disabled'); | ||
}); | ||
playlist.pause() | ||
buttonPause.setAttribute('disabled', 'disabled') | ||
buttonStop.setAttribute('disabled', 'disabled') | ||
buttonPlay.removeAttribute('disabled') | ||
}) | ||
|
||
buttonStop.addEventListener('click', () => { | ||
playlist.stop(); | ||
buttonPause.setAttribute('disabled', 'disabled'); | ||
buttonStop.setAttribute('disabled', 'disabled'); | ||
buttonPlay.removeAttribute('disabled'); | ||
}); | ||
playlist.stop() | ||
buttonPause.setAttribute('disabled', 'disabled') | ||
buttonStop.setAttribute('disabled', 'disabled') | ||
buttonPlay.removeAttribute('disabled') | ||
}) | ||
|
||
buttonNext.addEventListener('click', () => { | ||
playlist.next(); | ||
buttonPlay.setAttribute('disabled', 'disabled'); | ||
buttonPause.removeAttribute('disabled'); | ||
buttonStop.removeAttribute('disabled'); | ||
}); | ||
playlist.next() | ||
buttonPlay.setAttribute('disabled', 'disabled') | ||
buttonPause.removeAttribute('disabled') | ||
buttonStop.removeAttribute('disabled') | ||
}) | ||
|
||
buttonPrev.addEventListener('click', () => { | ||
playlist.prev(); | ||
buttonPlay.setAttribute('disabled', 'disabled'); | ||
buttonPause.removeAttribute('disabled'); | ||
buttonStop.removeAttribute('disabled'); | ||
}); | ||
playlist.prev() | ||
buttonPlay.setAttribute('disabled', 'disabled') | ||
buttonPause.removeAttribute('disabled') | ||
buttonStop.removeAttribute('disabled') | ||
}) | ||
|
||
buttonToggle.addEventListener('click', () => { | ||
playlist.toggle(); | ||
}); | ||
playlist.toggle() | ||
}) | ||
|
||
range.addEventListener('change', (e) => { | ||
const volume = getVolume(e.target); | ||
playlist.volume = volume; | ||
}); | ||
const volume = getVolume(e.target) | ||
playlist.volume = volume | ||
}) |
Oops, something went wrong.