I am looking to trigger a function once the sound I am playing has completed in HTML and JavaScript

How can I implement a sound player that toggles between play and pause buttons, and changes back to play button after the sound finishes playing? Here is the code snippet:

.play-button {
    display: none
}

.play-button.show {
    display: block;
}
.pause-button {
    display: none;
}

.pause-button.show {
    display: block
}

HTML:

<audio id="audio" src="name.mp3">
    Your browser does not support the audio element.
</audio>
<button class="play-button show" id="play-button" onclick="PlayButtonHide()">Play</button>
<button class="pause-button" id="pause-button" onclick="PauseButtonShow()">Pause</button>

JavaScript:

var play-button = document.getElementById('play-button')
var pause-button = document.getElementById('pause-button')
var audio = document.getElementById("audio");
var duration = document.getElementById("myAudio").duration;

function PlayButtonHide() {
    pause-button.classList.add('show')
    pause-button.setAttribute('onclick', 'PlayButtonShow()');
    play-button.classList.remove('show')
    audio.play()
}

function PlayButtonShow() {
    play.classList.add('show')
    pause.classList.remove('show')
    x.pause()
    play.setAttribute('onclick', 'PlayButtonHide()');
}

function PauseButtonShow() {
    pause-button.classList.add('show')
    pause-button.setAttribute('onclick', 'PauseButtonHide()');
    play-button.classList.remove('show')
}

function PauseButtonHide() {
    pause-button.classList.remove('show')
    pause-button.setAttribute('onclick', 'PauseButtonShow()');
    play-button.classList.add('show')
}

Answer №1

To enhance the functionality of the HTML audio tag, include the following attribute and value:

<audio id="audio" src="name.mp3" onended="PlayButtonShow()">
    Your browser does not support the audio element.
</audio>

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

Switch between sliding elements to the right and to the left using JavaScript

I attempted to create a small header navigation bar for the search, language, and contact options (see demo below). All elements are clickable. However, I am having trouble getting the content to slide in, move the other icons, and slide back when another ...

What is the best way to vertically center align the content inside a div with a height of 100vh?

https://i.sstatic.net/RABhH.jpg .components{ background-color: #1DA1F2; text-align: left; height: 100vh; padding: 20px; } With the current CSS code, I've set the outermost div (.components) to have a height of 100vh but I'm h ...

What is the best way to modify JSON data within a file?

To start, I retrieve a JSON array by using the following JavaScript code: $.getJSON("myJSONfile.json") .done(function(data) { myData = data; }); After storing the data in myData, which is a global variable, I proceed to add more informati ...

What is the best way to adjust the CSS of an element within the #shadow-root (open) in web development?

When dealing with a "#shadow-root (open)" element, it appears that manipulating the style using only CSS is not feasible. The shadow-root was generated by a third-party JavaScript script and displays an element as an overlay on the page that I am trying to ...

Working with multi-line HTML content within Javascript

I am currently using JavaScript to define the behavior of a button on a website. I want the button to add new HTML content to a specific part of the page, and I would like to use the MVC extension method Html.EditorFor to create this new HTML. Here is wha ...

What is the process for importing something from index.js within the same directory?

My folder structure is similar to the one below /components/organisms -- ModuleA.vue -- ModuleB.vue -- index.js The content of index.js: export { default as ModuleA } from "./ModuleA.vue" export { default as ModuleB } from "./ModuleB.vue&qu ...

Using jQuery each with an asynchronous ajax call may lead to the code executing before the ajax call is completed

In my jQuery script, I utilize an each loop to iterate through values entered in a repeated form field on a Symfony 3 CRM platform. The script includes a $.post function that sends the inputted value to a function responsible for checking database duplicat ...

PHP and JavaScript Form: Include the page URL in the email form when submitting

After submitting a basic form, I am struggling to include the URL of the page in the email notification to track where the user completed the form. I have attempted various solutions from this forum, but unfortunately none of them seem to work. How can I ...

Tips for ensuring v-tabs-items and v-tab-item fill height

I found an example that I am trying to follow at the following link: https://vuetifyjs.com/en/components/tabs#content <v-tabs-items v-model="model"> <v-tab-item v-for="i in 3" :key="i" :value="`tab-${i}`" > < ...

Do you know the steps to achieve consistent spacing above and below my homepage gallery?

I am currently working on a website template with additional CSS modifications. I do not have much code to add, except for what is provided below. You can view my site that I am trying to fix. The code snippet below was added by me in an attempt to adjus ...

Experimenting with applying jquery .slideDown() to multiple classes in order to create dynamic animations

I am facing an issue while using .slideDown() with multiple classes. I want only one class to toggle/dropdown at a time, but currently when I press the button, all classes show up instead of just the one I want to display. How can I achieve this? Here is ...

How to efficiently monitor and calculate changes in an array of objects using Vue?

I have a collection named people that holds data in the form of objects: Previous Data [ {id: 0, name: 'Bob', age: 27}, {id: 1, name: 'Frank', age: 32}, {id: 2, name: 'Joe', age: 38} ] This data can be modified: New ...

"Enhance Your Website with Customized Background Sound Effects

Currently, I have been developing an application. Now, I am in the stage where I need to incorporate some background music into it. I am planning to utilize jQuery for playing the music and looping it once it finishes. Here is the specific background tr ...

What is the best way to transform the request query id = ' [12eetftt76237,jhgasduyas7657] ' into an array of elements or strings like [12eetftt76237,jhgasduyas7657]?

Hey there, I am working on a project using hapijs and typescript. I have a requirement to send an array of IDs as parameters through the request URL. Here is an example of the URL: localhost:3444/?id='[askjajk78686,ajshd67868]' I attempted to u ...

Learn how to efficiently pre-load data using the prefetchQuery method in React-Query

Attempting to pre-fetch data using react-query with prefetchQuery. The network tab in browser DevTools shows the requested data coming from the back-end, but strangely, the data is not present in the react-query DevTools cache. Any ideas on what might be c ...

What exactly is the data type of setInterval in TypeScript?

If I want to define the type of a variable that will be used with setInterval in the following code snippet: this.autoSaveInterval = setInterval(function(){ if(this.car.id){ this.save(); } else{ this.create(); } ...

Utilizing Firebase in place of .json files for the AngularJS Phonecat application

I am currently exploring firebase and attempting to retrieve data using this service from firebase instead of json files. However, I have encountered some challenges in getting it to function properly. This is inspired by the angularjs phonecat example .f ...

`The resurgence of CERT_FindUserCertByUsage function in JavaScript`

I am currently grappling with unraveling the connection between C++ dlls and JavaScript. There is a snippet of js code that reads: cert = CERT_FindUserCertByUsage(certDB, certName.nickname,certUsageEmailSigner, true, null); where the variable cert is ini ...

Is there a way to automatically set all object properties to false if one property is set to true in React?

The Issue: My task at work involves creating 3 buttons with separate filters to display different tickets in a table. All the functionality is completed, and the filtered tickets are displayed correctly. However, I am facing an issue that is preventing m ...

Search MongoDB using regular expressions that disregard any punctuation marks

I'm currently working on querying a MongoDB collection to find a single item based on a string property using react-router-dom's NavLink. The challenge arises when the prop via NavLink contains hyphens instead of spaces, and there are also names ...