D
D
Denis2020-12-05 16:04:59
JavaScript
Denis, 2020-12-05 16:04:59

How to automatically play music on the site (several different mp3 files)?

The task is trivial - the host has an "Audio" folder with several mp3 files. How to make random playback of these files on the site?
With one file - no questions - everything is elementary. Of course, you can merge all mp3 files into one, but it is important in my task to make the browser randomly play all mp3 files in the Audio folder every time you open the site.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2020-12-05
@max_shane

Step 1. Create an array with file addresses.
Step 2. Write a function that returns a random number from 0 to an array of Tracks.length-1
Step 3. Write a function that calls the browser API to play audio files with a file from an array and substitute a dynamically generated number from the randomizer into it.
Step 4. Write a function that determines the end of the file playback (using the same browser API) and generates an event that means the end of the track.
Step 5. Hang an eventListener on the page and put a function inside it that will be called whenever an event occurs from the function in the previous paragraph. This function will call the play file function every time the event fires.
Step 6. Call the file playback function on the window.onload event (when the site page has loaded).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question