Is there a way to sustain audio playback even after the current track has finished?

I am facing an issue with my code where I am trying to play the next song in a playlist or album after the current one finishes playing. Although I was able to get a song to play, it does not progress to the next one automatically.

Below is the snippet of my code:

<button class="controlButton play" title="Play" @click="play" v-if="!isPlaying">
   <img src="storage/icons/play.png" alt="Play">
</button>
<button class="controlButton pause" title="Pause" @click="pause" v-else>
   <img src="storage/icons/pause.png" alt="Pause">
</button>
<script>
import { Link } from "@inertiajs/inertia-vue3";
export default {
    components: {
        Link
    },
    data() {
        return {
            audio: new Audio(),
            currentSong: {},
            myWidth: 0,
            loop:{},
            index: 0,
            songs: [
                {
                    title: 'Song',
                    artist: 'Artis A',
                    src: '/storage/audio/song.mp3',
                    cover: '/storage/images/cover.jpeg',
                },
                {
                    title: 'Song 2',
                    artist: 'Artis B',
                    src: '/storage/audio/song2.mp3',
                    cover: '/storage/images/cover2.jpeg',
                },
 mounted() {
        this.currentSong = this.songs[this.index];
},
methods: {
play(song) {
            if(typeof song.src != 'undefined') {
                this.currentSong = song;
                this.audio.src = this.currentSong.src

            }
            this.audio.play();
            this.isPlaying = true
            this.activeItem = this.currentSong
        },
 pause() {
            this.audio.pause();
            this.isPlaying = false;
        },
}

I need help on how I can ensure that the second song plays automatically after the current one ends and also change the button back to "play" when there are no more songs left in the playlist.

Answer №1

The Audio element, a descendant of HTMLMediaElement in the DOM, provides access to an ended event that can be monitored.

mounted() {
    this.audio.addEventListener('ended', (event) => {
        // move on to the next track
    })
}

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

Troubleshooting Vue Router Matching Problem

I have implemented routes using https://github.com/posva/unplugin-vue-router#readme in my project with the vitesse template. My folder structure is set up like this: The paths /hi/asd and /hi/asd/other correspond to the view for /hi/:name, which is deter ...

Activate Bootstrap tooltip when input element is focused, and then when the next input element is selected

I'm trying to activate a tooltip on an input element within a table. My goal is to trigger the tooltip when either that specific input element or the adjacent input element in the table are focused. Below is the HTML structure: <td class="fea ...

Download files from Firebase storage to a user's device

I have a variety of files such as images, videos, and audio stored in my firebase storage. My goal is to provide users with the ability to download these files to their computers by clicking on a download button. After reviewing the firebase documentation ...

Is there a way to open a particular Bootstrap tab within a modal using a URL?

I am seeking a way to automatically open a specific tab within a modal using its URL. To achieve this, I have included the following JavaScript code at the end of my website. By entering website.com/#modal-6 in the browser, it will open with modal-6 activa ...

jQuery.get() function is limited to specific types of webpages

I have successfully combined multiple weather APIs on my website, which can be found here. Recently, I started using the weather.gov API and it has been quite effective. However, there are certain data points that I need to extract which the weather.gov A ...

Steps for aligning an image to the right using Bootstrap Vue

I'm currently learning Bootstrap Vue and I'm trying to align an image on the right using a b-card header. The challenge I'm facing is that when I add the image, it disrupts the perfect size of the template header, causing the image to show a ...

Can you explain the inner workings of the provided code in a step-by-step manner?

I stumbled upon this code snippet that checks if the number of occurrences of an element in an array is greater than a specified value, and if so, it will remove the number: function deleteNth(arr,x) { var cache = {}; return arr.filter(function(n) { ...

Tips for incorporating dynamic URLs in Next.js

In my current project using nextjs, I am dealing with fetching images via an API. Right now, I am receiving the "Full image path" (for example, "https://myurl.com/image/imagename.jpg") without any issue. However, I need to figure out how to fetch the image ...

Guide on how to refresh the 'id' after a set period using Ajax technology

I am having an issue with reloading/refreshing a specific table in order to fetch updated database information. The problem is that when I try to refresh only the table, the entire page reloads instead. However, I have managed to identify the 'id&apos ...

Clearing up memory in ThreeJS application

I'm facing challenges with memory management in my ThreeJS application. I know there are already a few queries regarding this issue: freeing memory in three.js Freeing memory with threejs Three.js Collada - What's the proper way to dispose() a ...

Sending data from frontend to backend in a Node.js Express application

My Node.js Express project in the WebStorm IDE is structured like this: I'm trying to find a way to pass a variable from index.js to app.js so that I can write it to data.json at the end. As a beginner in Node.js, I'm still grappling with the c ...

What is the best way to sort and organize JSON data?

Upon successful ajax call, I receive JSON data structured like this: var videolist = [ { "video_id": 0, "video_name": "Guerrero Beard", "timelength": 15 }, { "video_id": 1, "video_name": "Hallie Key", "timelength": 8 }, { ...

Utilize an A-frame conditional statement to check a variable and dynamically display various glTF models depending on its value

Is it possible to use JavaScript and A-frame () to create a scenario like this? I want to have an onload function named load() that will evaluate the value of variable x. If x is equal to one, I want the gltf with the ID of 1 to be visible while hiding th ...

Is there a better method to accomplish this task in a more effective manner?

Is there a more efficient way to achieve this code with fewer lines of code? I'm looking for a solution that avoids repetition and makes it easier to manage, especially since I plan on creating multiple instances of this. Performance is a key consider ...

.npmignore failing to exclude certain files from npm package

I'm facing an issue with a private module on Github that I am adding to my project using npm. Despite having a .npmignore file in the module, the files specified are not being ignored when I install or update it. Here is what my project's packag ...

Incorporating a new parameter into the JSON response from SoundCloud

Currently, I am in the process of developing an app utilizing the SoundCloud JavaScript API. My approach involves fetching the JSON data from: http://api.soundcloud.com/resolve.json?url=http://soundcloud.com/matas/hobnotropic&client_id=YOUR_CLIENT_ID ...

Vue transition not functioning properly when hovering over text for changes

When you hover over the circular region on the website, the text and description in the red box will change. To add a fade animation effect when the text changes, I attempted to use <transition> as per the documentation provided in this link. Unfor ...

Efficiently run multiple Node-written apps on a single server

I currently have a single VPS and would like to host multiple node.js apps on it, similar to how Apache or Nginx works. I am using Nginx as a proxy, but I have concerns. As you know, one of the key features of Node.js is its non-blocking I/O and sing ...

Pause the jquery script when a key is pressed

Currently, I have a script that loads a php file within a div and automatically refreshes every 5 seconds. Check out the code below: $("#load_timeout").load("time_out.php"); var refreshId = setInterval(function() { $("#load_timeout").load('time_o ...

Issue with scroll down button functionality not functioning as expected

Is there a way to create a simple scroll down button that smoothly takes you to a specific section on the page? I've tried numerous buttons, jQuery, and JavaScript methods, but for some reason, it's not working as expected. The link is set up co ...