Struggling to Play Sound on Vue Template

I attempted to implement the audio tag using a variable, but it was not functioning properly. Any assistance would be greatly appreciated.

The following code is within the script tag:

<script setup>
import { ref } from 'vue'
import data from '../../../data/game3/data2.json'

const music = ref(data.categories[0].units[0].items[0].pronunciation)

console.log(music.value) //when  log it show ../../assets/pronunciation/apple.mp3 which is the right path

</script>

And this code is within the template tag:

<template>
  <div>
    {{ music }}

    <audio controls>
      <source :src="music" type="audio/mp3" />
    </audio>

    <audio controls>
      <source src="../../assets/pronunciation/apple.mp3" type="audio/mp3" />
    </audio>
  </div>

The first version does not seem to be working correctly. Could someone please offer some guidance? (I have already tested in multiple browsers with the same result.)

This is the error displayed by the browser: https://i.sstatic.net/6XA1G.png

Answer №1

It's not confirmed, but it seems like the source in the first audio tag may not be identified during the HTML parsing process.
One potential solution could be to invoke the play method on the audio element after it has finished loading.

document.querySelector('audio').load();
document.querySelector('audio').play();

You may find more insights by visiting this question.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Converting a text file to JSON format using Adobe Acrobat: A tutorial on proper referencing

I am facing an issue with converting a string from a file attached to my PDF (JSONTEST.txt) into JSON format so that I can reference it using obj[key]. Despite trying to use eval(), I encounter the following error every time: SyntaxError: missing ; before ...

Maintaining the consistent structure of build directories within a Docker container is crucial, especially when compiling TypeScript code that excludes the test

Our application is built using TypeScript and the source code resides in the /src directory. We have tests located in the /tests directory. When we compile the code locally using TSC, the compiled files are deposited into /dist/src and /dist/test respectiv ...

Having trouble with JS functionality in ASP.NET?

While experimenting, I discovered a way to update the text of both label and textbox when a checkbox is clicked. It appears that asp.net provides extensive support for JavaScript. <script type="text/javascript> $(document).ready(function () { ...

Chai can't verify the HTML component

During my testing of an Angular project, I initially used Jasmine to check if a specific attribute of an HTML element exists. The following code snippet worked successfully for me: expect(element(by.css("*[id='ref_button']")).getAttribute(&ap ...

RxJS emits an array of strings with a one second interval between each emission

Currently, my code is set up to transform an Observable<string[]> into an Observable<string>, emitting the values one second apart from each other. It's like a message ticker on a website. Here's how it works at the moment: const ...

Tips on elevating a dragged div above all other elements when utilizing two scrollbars

Currently, I have two horizontal scrollers where I need to drag a div from the upper scroller to the lower scroller. While this functionality is working properly, there is an issue with the dragged div getting lost under the borders of the scroller during ...

Adding colors to your Vue3 and Vuetify3 theme: A Step-by-Step Guide

Currently, I am in the process of upgrading an old Vue2 app to Vue3 and I have a specific requirement to set up 3 global colors: red, green, and gray. In order to achieve this, I am implementing the following code snippet within my vuetify.js file: // Styl ...

What is the best way to implement sorting for multiple table headers in Vue.js?

Is there a way to enable sorting for multiple table headers in Vue.js? Currently, sorting is only working for the firstname column using orderBy 'firstname' order. I wonder if it's possible to sort by other columns as well like orderBy &apos ...

Utilizing Pagination in Elasticsearch with MongoDB and ExpressJS

Despite my efforts to find a solution online, I have not yet come across anything that helps me achieve what I need. This is my first time working with ElasticSearch, and I am relatively new to Node.js and MongoDB as well. Following a tutorial, I impleme ...

Expanding all rows with the same name in a Vuetify expandable table

I have noticed an issue with the expandable tables in Vuetify, where if two entries in the table have the same name, clicking on one row to expand it also expands another row with the same name. Is there a way to prevent this from happening? To see the pr ...

Adding new divisions to an already existing division with angled borders

I am currently attempting to create a header similar to the one displayed in the attached image. Despite trying various solutions found on this platform, I have not been able to achieve the desired result. I have managed to work out the text and logo eleme ...

Storing the output of a GET request in a text file can lead to a never-ending loop

After attempting to save a String obtained from a GET request into a text file, I encountered an unexpected infinite loop issue. It seems that without utilizing created(), the process fails entirely, resulting in story remaining empty. <div id="app"> ...

The challenge of optimizing Angular websites for SEO while also addressing page reload issues

I am struggling to create SEO-friendly URLs in my Node server backend. I want URLs like the following: http://localhost:3003/my-new-mobile //product http://localhost:3003/my-new-category //category http://localhost:3003/my-first-blog-post // blog post I ...

Retrieve the data of the current component from the slot template

This specific vue component showcases a CardGroup template with a headerRight slot. The content of the slot includes a message displaying the total number of items, accessed through the this.total data property. <template> <CardGroup> ...

Comparing different items within a single entity

My objective is to analyze multiple sets of location data (latitude, longitude) in order to determine the farthest two locations from the group. While I am familiar with calculating the distance between two locations, I am seeking guidance on how to approa ...

I am looking to retrieve a particular entry from a JSON data array and make some modifications to it

Initially, I fetched JSON data from the web server using the following code: $.getJSON(url,function(){ //my callback function; }); Now, I have received the data in the format shown below: {entries:[{title:'foo',id:'UUID',finished:nul ...

React event handling

Currently, I am in the process of developing an accordion app using React. The data for this app is fetched from an API, and while I have the basic structure of the app ready, I am facing some difficulties with handling the click event on the accordion. H ...

Tips for storing the values of multiple checkboxes in a React application

Though it may seem like a silly question, I am new to ReactJS and struggling with creating a logic for checkboxes. I have multiple checkboxes in my program and I want to save the values of the selected checkboxes in a single state or array. Despite my effo ...

Loading Objects with Material Textures Ahead in Three.js

I am faced with the challenge of preloading multiple obj+mtl files using Three.js, each with distinct file paths, and I need to trigger another function once all the objects have finished loading. Initially, I attempted to use a boolean variable that woul ...

Guide to integrating the Google identity services library in a React application

I've encountered an issue with my current code, which has suddenly stopped working: import React, { Component } from "react"; import Auth from "../../helper/auth"; import PropTypes from "prop-types"; import logo from &quo ...