I am working on a simple Vue application that plays two audio files and asks the user to determine the audio interval between them. I am currently unable to play any audio files. Whenever my playNote() function is called, Chrome throws the error Uncaught (in promise) DOMException: Failed to load because no supported source was found.
I've tried changing the audio file type in case it was an issue with the file being read. I've tried changing where the audio file is located in case Vue was having trouble accessing the file (currently the file is located in the same folder as the Vue component for troubleshooting purposes). Neither of these have resolved the error I am getting.
I have tried opening the Vue app in Firefox and get a similar string of errors when calling playNote():
“Content-Type” of “text/html” is not supported. Load of media resource failed.NotSupportedError: The media resource indicated by the src attribute or assigned media provider object was not suitable.Cannot play media. No decoders for requested formats: text/html
I've stripped the code down to just the play button and relevant functionality in hopes that one of my other functions was accidentally interfering, but even with the below I get the same error:
<template>
<div class="hello">
<h1>Ear Training</h1>
<button v-on:click='playNote'>Play</button>
</div>
</template>
<script>
export default {
name: 'Intervals',
data: function() {
return {
middleC: 'middleC.aiff',
}
},
methods: {
// Selects a note
getNote: function() {
return this.middleC;
},
// Plays a note, selected via getNote()
playNote: function() {
let note = new Audio(this.getNote);
note.play();
}
},
}
</script>
Any help or guidance would be absolutely fantastic- thank you in advance!
Update:
I updated my code based on your suggestions, but I am still unable to play audio.
<template>
<div class="hello">
<h1>Ear Training</h1>
<button v-on:click='playNote'>Play</button>
</div>
</template>
<script>
const someSound = require("../assets/audio/middleC.mp3");
export default {
name: "Intervals",
data: () => ({
someSound
}),
methods: {
playNote() {
console.log('calling playNote()');
let note = new Audio(this.someSound);
note.addEventListener("canplaythrough", () => {
console.log('event listener called');
note.play();
});
}
}
};
</script>
When I click the play button on the page,calling playNote() is read by the console, meaning the function is being called. However, event listener called is never logged, meaning the event listener is never acting.