简体   繁体   English

如何使用 JavaScript 处理一个文件夹中的多个值?

[英]How can I deal with many values in one folder using JavaScript?

I want to add random music player on my page.我想在我的页面上添加随机音乐播放器。
And found random music player source.并找到随机的音乐播放器源。 But it has just 2 musics.但它只有 2 首音乐。

If I have many music file over 100 in 'audio' folder, are there some tips dealing with many music files?如果我在“音频”文件夹中有超过 100 个音乐文件,是否有一些处理许多音乐文件的技巧?

图片

And this is my JavaScirpt code.这是我的 JavaScirpt 代码。

var lastSong = null;
var selection = null;
var playlist = ["audio/Boy with luv.mp3", "audio/Mikrokosmos.mp3"]; // List of Songs

var player = document.getElementById("audioplayer"); // Get Audio Element
player.autoplay = true;
player.addEventListener("ended", selectRandom); // Run function when song ends

function selectRandom() {
  while (selection == lastSong) {
    // Repeat until different song is selected
    selection = Math.floor(Math.random() * playlist.length);
  }
  lastSong = selection; // Remember last song
  player.src = playlist[selection]; // Tell HTML the location of the new Song
}

selectRandom(); // Select initial song
player.play(); // Start Song

Above code以上代码

var playlist = ["audio/Boy with luv.mp3", "audio/Mikrokosmos.mp3"];

If playlist has many music files, I want to add music file using like for function and so on.如果播放列表有很多音乐文件,我想使用 function 等添加音乐文件。

Have a php or some other script generate a JSON with the list of all the songs for you, fetch it with some AJAX, and populate your playlist array from there.让 php 或其他脚本生成 JSON 和所有歌曲的列表,用一些 AJAX 获取它,然后从那里填充您的播放列表数组。

like:喜欢:

{
[
 {"title" : "something.mp3"},
 {"title" : "something_else.mp3"}
]
}

as your json, that you leave in a specific folder.作为您的 json,您将其保留在特定文件夹中。 Then:然后:

function loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
     populatePlaylist(this.responseText);
    }
  };
  xhttp.open("GET", "songs.json", true);
  xhttp.send();
}

function populatePlaylist(songsJSON){
 var songs = [];

 var obj = JSON.parse(songsJSON);

 obj.forEach(songs.push(obj["title"]));

 return songs;

}

And you set songs as your playlist, that's it.您将歌曲设置为播放列表,仅此而已。

(PS: this code shouldn't work out of the box) (PS:此代码不应该开箱即用)

you can do this你可以这样做

const fs = require('fs'); // file handling module
const files = fs.readdirSync('.'); // use to read all avaliable file in the current directory
const extname = '.mp3'; // audio file extension
const playlist = files.filter(file => file.endsWith(extname)); // filter out all the files which have `.mp3` extension 

add these lines above the below line在下面的行上方添加这些行

var lastSong = null;

and remove your declared playlist cause we have now one that comes from this const playlist = files.filter(file => file.endsWith(extname));并删除您声明的playlist ,因为我们现在有一个来自此const playlist = files.filter(file => file.endsWith(extname));

note: make sure you have you code and mp3 file in same directory.注意:确保您的代码和 mp3 文件位于同一目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM