- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的JavaScript代码最近遇到了问题。它是一种音乐播放器,可以根据时间更改播放列表;
2 AM-6AM-播放音频音乐/ early_morning
6 AM-11AM-从音频/早晨播放音乐
11 AM-7PM-播放音频/白天的音乐
7 PM-10PM-从音频/黄金时间播放音乐
10 PM-2AM-从音频/夜总会播放音乐
这是有问题的代码:
// TIME OF DAY
var early_morning, morning, daytime, primetime, nightclub;
early_morning = new Date();
morning = new Date();
daytime = new Date();
primetime = new Date();
nightclub = new Date();
early_morning.setUTCHours(1);
morning.setUTCHours(5);
daytime.setUTCHours(10);
primetime.setUTCHours(18);
nightclub.setUTCHours(21);
// AUDIO PLAYER
var lastSong = null;
var selection = null;
if (early_morning > morning) {
var playlist = ["audio/early_morning/1998_trk04.mp3", "audio/early_morning/adagio.mp3", "audio/early_morning/all_obsessed.mp3", "audio/early_morning/no_saints.mp3", "audio/early_morning/swingin_partay.mp3"];
}
if (morning > daytime) {
var playlist = ["audio/morning/1998_trk04.mp3", "audio/morning/cool_groovings.mp3", "audio/morning/game_on.mp3", "audio/morning/half_pipe.mp3", "audio/morning/hope.mp3", "audio/morning/internationale!.mp3", "audio/morning/move_it.mp3", "audio/morning/muted.mp3", "audio/morning/no_saints.mp3", "audio/morning/swingin_partay.mp3"];
}
if (daytime > primetime) {
var playlist = ["audio/primetime/1998_trk04.mp3", "audio/primetime/apocalypse(remix).mp3", "audio/primetime/bondicular(remix).mp3", "audio/primetime/choir_of_impending_breakbeat.mp3", "audio/primetime/cine2000.mp3", "audio/primetime/kompressor(remix).mp3", "audio/primetime/no_saints.mp3", "audio/primetime/orchestral_hardcore(remix).mp3", "audio/primetime/orchestral_hardcore.mp3", "audio/primetime/quest.mp3", "audio/primetime/swingin_partay.mp3", "audio/primetime/the_trap.mp3"];
}
if (primetime > nightclub) {
var playlist = ["audio/daytime/1998_trk04.mp3", "audio/daytime/2009_trk7.mp3", "audio/daytime/4mp3", "audio/daytime/connected.mp3", "audio/daytime/funkerama.mp3", "audio/daytime/gritty_groove.mp3", "audio/daytime/internationale!(remix)", "audio/daytime/no_saints.mp3", "audio/daytime/smooth_strings_trip.mp3", "audio/daytime/sunset.mp3", "audio/daytime/swingin_partay.mp3"];
}
if (nightclub > early_morning) {
var playlist = ["audio/nightclub/adagio.mp3", "audio/nightclub/apocalypse.mp3", "audio/nightclub/dance_9.mp3", "audio/nightclub/internationale!.mp3", "audio/nightclub/nu-soul_village(remix).mp3", "audio/nightclub/obsessive.mp3", "audio/nightclub/universe.mp3"];
}
var player = document.getElementById("player"); // 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
最佳答案
如果查看以下代码段,将看到符合条件的代码。设置时间的方式,唯一适用的条件就是nightclub > early_morning
,这是因为当您使用setUTCHours
时,它基本上是在设置日期并冻结该时间。想象一个简单的情况,您不使用日期对象,而只在这里使用数字。在此处查找如何使用调试器,并逐步执行代码以查看变量实际评估的结果。
// TIME OF DAY
var early_morning, morning, daytime, primetime, nightclub;
early_morning = new Date();
morning = new Date();
daytime = new Date();
primetime = new Date();
nightclub = new Date();
early_morning.setUTCHours(1);
morning.setUTCHours(5);
daytime.setUTCHours(10);
primetime.setUTCHours(18);
nightclub.setUTCHours(21);
// AUDIO PLAYER
var lastSong = null;
var selection = null;
if (early_morning > morning) {
console.log('early_morning > morning')
}
if (morning > daytime) {
console.log('morning > daytime')
}
if (daytime > primetime) {
console.log('daytime > primetime')
}
if (primetime > nightclub) {
console.log('primetime > nightclub')
}
if (nightclub > early_morning) {
console.log('nightclub > early_morning')
}
const intervals = {
2: "early_morning",
3: "early_morning",
4: "early_morning",
5: "early_morning",
6: "early_morning",
7: "morning",
8: "morning",
9: "morning",
10: "morning",
11: "morning",
12: "daytime",
13: "daytime",
14: "daytime",
15: "daytime",
16: "daytime",
17: "daytime",
18: "daytime",
19: "daytime",
20: "primetime",
21: "primetime",
22: "primetime",
23: "nightclub",
0: "nightclub",
1: "nightclub",
2: "nightclub",
}
const hour = new Date().getHours()
console.log(intervals[hour]);
const playlist = [{
name: 'earlyMorning',
times: [2, 3, 4, 5, 6],
songs: ["audio/morning/1998_trk04.mp3", "audio/morning/cool_groovings.mp3", "audio/morning/game_on.mp3", "audio/morning/half_pipe.mp3", "audio/morning/hope.mp3", "audio/morning/internationale!.mp3", "audio/morning/move_it.mp3", "audio/morning/muted.mp3", "audio/morning/no_saints.mp3", "audio/morning/swingin_partay.mp3"]
},
{
name: 'morning',
times: [7, 8, 9],
songs: ["audio/daytime/1998_trk04.mp3", "audio/daytime/2009_trk7.mp3", "audio/daytime/4mp3", "audio/daytime/connected.mp3", "audio/daytime/funkerama.mp3", "audio/daytime/gritty_groove.mp3", "audio/daytime/internationale!(remix)", "audio/daytime/no_saints.mp3", "audio/daytime/smooth_strings_trip.mp3", "audio/daytime/sunset.mp3", "audio/daytime/swingin_partay.mp3"]
},
{
name: 'daytime',
times: [12, 13, 14, 15, 16, 17, 18, 19],
songs: ["audio/primetime/1998_trk04.mp3", "audio/primetime/apocalypse(remix).mp3", "audio/primetime/bondicular(remix).mp3", "audio/primetime/choir_of_impending_breakbeat.mp3", "audio/primetime/cine2000.mp3", "audio/primetime/kompressor(remix).mp3", "audio/primetime/no_saints.mp3", "audio/primetime/orchestral_hardcore(remix).mp3", "audio/primetime/orchestral_hardcore.mp3", "audio/primetime/quest.mp3", "audio/primetime/swingin_partay.mp3", "audio/primetime/the_trap.mp3"]
},
{
name: 'primetime',
times: [20, 21, 22],
songs: ["audio/daytime/1998_trk04.mp3", "audio/daytime/2009_trk7.mp3", "audio/daytime/4mp3", "audio/daytime/connected.mp3", "audio/daytime/funkerama.mp3", "audio/daytime/gritty_groove.mp3", "audio/daytime/internationale!(remix)", "audio/daytime/no_saints.mp3", "audio/daytime/smooth_strings_trip.mp3", "audio/daytime/sunset.mp3", "audio/daytime/swingin_partay.mp3"]
},
{
name: 'nightclub',
times: [23, 0, 1, 2],
songs: ["audio/nightclub/adagio.mp3", "audio/nightclub/apocalypse.mp3", "audio/nightclub/dance_9.mp3", "audio/nightclub/internationale!.mp3", "audio/nightclub/nu-soul_village(remix).mp3", "audio/nightclub/obsessive.mp3", "audio/nightclub/universe.mp3"]
}
]
let nowPlaying = null;
// const player = document.getElementById("player"); // Get Audio Element
// player.autoplay = true;
// player.addEventListener("ended", selectRandom); // Run function when song ends
function selectRandom() {
// destructure object because all we want is the song array
const { songs } = playlist.find(list => list.times.includes(new Date().getHours()));
// remove the currently playing song, or nothing if null
const filteredSongs = songs.filter(song => song != nowPlaying);
// pick a random song out of the remaining songs
const selection = filteredSongs[Math.floor(Math.random() * filteredSongs.length)]
lastSong = selection; // Remember last song
// player.src = selection; // Tell HTML the location of the new Song
console.log(`Now playing ${selection}`);
}
selectRandom(); // Select initial song
//player.play(); // Start Song
关于javascript - 音乐播放器仅从一个文件夹播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61737721/
启动Android游戏时,我试图让一些音乐淡出。音乐在主菜单中播放,然后在播放器单击播放时淡出。我可以停止播放音乐,只是不会消失。 我试图用这个淡出: using UnityEngine; using
我正在 XNA 中创建一个游戏,它可以使用大量的音乐循环,但我似乎无法同步这些声音。 我总是错过几毫秒你能帮我吗? 这是我第一次尝试同步声音。请注意,我需要处理数十种声音...... 这个同步问题可能
我使用以下代码播放了一首歌,但它有 11 分钟长,我该如何停止它? 此代码位于 jFrame 1 中。如何使其停止在 jFrame 2 中? Input
在阅读我的书并浏览一些 YouTube 教程后,我对标准化的理解是,重要的事情之一就是不要有重复的值。更具体地说,主键 (ID) 不应重复。 因此,如果我正在使用音乐/音乐会数据库中的某些表,那么以下
我正在用 java 创建一个应用程序/游戏,其中包含背景音乐/声音。一切都按预期工作。如果播放某些系统声音/媒体声音/其他声音,我想静音/停止。 有什么建议...?? 最佳答案 我建议不要关心这个问题
只是尝试从图像 (1080p .png) + 音乐 (320Kb mp3) 为 youtube 制作视频。 ffmpeg -loop 1 -i image.png -i music.mp3 -
我正在GW-Basic中开发游戏,我想向其中添加音乐,但问题是我无法在后台播放,但是当我添加声音时先播放声音,然后在开始执行游戏和vICE之后vERSA。而我希望这两件事同时播放..所以知道怎么做吗?
我正在使用Xcode的Sprite套件开发iPhone游戏,想知道是否有一种简单或最佳实践的方法可以通过编程方式消除所有声音效果/音乐?对我来说,最明显的方法是创建一些 bool(boolean) 变
关闭。这个问题需要更多 focused .它目前不接受答案。 想改进这个问题?更新问题,使其仅关注一个问题 editing this post . 3年前关闭。 Improve this questi
我正在设计一个 Android 应用程序,需要依次播放各种短音乐文件。 我设置了一个“onClick”监听器来运行一个循环,一遍又一遍地播放这些声音文件,它们可能会播放 100 多次。 但是 我需要用
我有一个网站,其中包含集成到布局中的 Flash 音乐播放器。我希望用户能够在不中断音乐的情况下浏览网站。我做了很多研究和思考,以下是我提出的选项(请记住,我希望尽可能对 SEO 友好)。有人有其他想
我可以即时生成一些音调并播放声音吗?例如,如果我想生成 DO RI MI FA SO LA XI 音调并播放它们? 似乎声音 API 都是为了播放现有的音频文件。谢谢! 最佳答案 参见 Matt Ga
我一直在尝试将音乐添加到我的应用程序中,但没有特别成功。我一直在尝试使用 AVFoundation,我的代码如下: //MUSIC var audioPlayer = AVAudioP
MPMediaPickerController 返回一个 MPMediaItem 如果返回本地 url,则可以播放声音。 如果音乐没有下载到本地,则assetURL为空。 如何在本地下载 MPMedi
有什么方法可以检测是否正在播放 iPod 应用以外的其他来源的音乐或媒体? 我知道我可以检测到从 ipod 播放的音乐: [[MPMusicPlayerController iPodMusicPlay
我刚刚安装了 music21,我正在尝试开始使用它。我安装了 Musescore3,但我无法使 show() 函数工作。 我试过按照我在网上找到的一些说明更改路径,但无济于事 from music21
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
我目前正在寻找某种方式来扩展SFML.Net以使用MP3。因此,ioctllr推荐了NLayer,我想尝试一下。这是我的方法: namespace AudioCuesheetEditor.AudioB
我包含了正确的库和所有内容,这部分代码由于某种原因给出了段错误: int numerator = atoi(&fraction[0]); int denominator = atoi(&fracti
是否可以播放 AVPLayer 实例(带有视频)并同时使用 MPMusicPlayerController 实例来播放 iTunes 播放列表? 我尝试了几种解决方案,但每次播放视频时音频都会停止,每
我是一名优秀的程序员,十分优秀!