Hi all,
Struggling to pass test #6 (only one missing) for the Drum machine project.
The code is working (does what I expect it to do), but I can’t pass this one test.
I get The play() request was interrupted by a call to pause()
on the console. Pointers on how to fix whatever is stopping the test are more than welcome!
My code:
const soundLibrary = [
{
idDivKey: 'Heater-1',
idAudioKey: "Q" ,
url: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-1.mp3',
key: 'q'
},
{
idDivKey: 'Heater-2',
idAudioKey: "W" ,
url: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-2.mp3',
key: 'w'
},
{
idDivKey: 'Heater-3',
idAudioKey: "E" ,
url: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-3.mp3',
key: 'e'
},
{
idDivKey: 'Dsc_Oh',
idAudioKey: "A" ,
url: 'https://s3.amazonaws.com/freecodecamp/drums/Dsc_Oh.mp3',
key: 'a'
},
{
idDivKey: 'RP4_KICK_1',
idAudioKey: "S" ,
url: 'https://s3.amazonaws.com/freecodecamp/drums/RP4_KICK_1.mp3',
key: 's'
},
{
idDivKey: 'Cev_H2',
idAudioKey: "D" ,
url: 'https://s3.amazonaws.com/freecodecamp/drums/Cev_H2.mp3',
key: 'd'
},
{
idDivKey: 'Chord_3',
idAudioKey: "Z" ,
url: 'https://s3.amazonaws.com/freecodecamp/drums/Chord_3.mp3',
key: 'z'
},
{
idDivKey: 'Dry_ohh',
idAudioKey: "X" ,
url: 'https://s3.amazonaws.com/freecodecamp/drums/Dry_Ohh.mp3',
key: 'x'
},
{
idDivKey: 'Brk_Snr',
idAudioKey: "C" ,
url: 'https://s3.amazonaws.com/freecodecamp/drums/Brk_Snr.mp3',
key: 'c'
},
]
const App = () => {
const [display, setDisplay] = React.useState(
'Drum Name'
);
React.useEffect(() => {
document.addEventListener('keydown', PressedKey);
})
const Play = (element) => {
const audioElement = document.getElementById(element.idAudioKey);
audioElement.currentTime = 0; //allows user to click continuously on element without having to wait for sound to die off;
audioElement.play(); //play the current audio
setDisplay(element.idDivKey) //set state with the name of the drum
}
const PressedKey = (e) => {
soundLibrary.forEach(sound => {
// console.log(e.key);
// console.log(sound.key);
if(e.key === sound.key) {
// console.log(sound.key);
// console.log(e.key);
document.getElementById(sound.idAudioKey).currentTime = 0;
document.getElementById(sound.idAudioKey).play();
setDisplay(sound.idDivKey);
}
})
}
return (
<React.Fragment>
<div id="drum-machine">
<div className="container">
<div id="display">{display}
{soundLibrary.map(sound => {
return (
<div className="drum-pad" id={sound.idDivKey} onClick={() => Play(sound)}><audio src={sound.url} className="clip" id={sound.idAudioKey}></audio>{sound.idAudioKey}
</div>
)
})}
</div>
<footer>
<ul className="footer-options">
<li className="footer-link"><a href="#" className="footer-linktext">Legal</a></li>
<li className="footer-link"><a href="#" className="footer-linktext">Contact Us</a></li>
</ul>
<span>© 2019 Developed by Pat. All Rights Reserved.</span>
</footer>
</div>
</div>
</React.Fragment>
)
}
ReactDOM.render(<App />, document.getElementById('app'));