首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我需要在点击点击按钮时播放下一首歌曲

我需要在点击点击按钮时播放下一首歌曲
EN

Stack Overflow用户
提问于 2018-06-16 22:08:30
回答 1查看 451关注 0票数 1
代码语言:javascript
复制
var song1 = $('#sound-1');
var song2 = $('#sound-2');

var audioArray = [song1, song2];
var i=0;
var lastPlayedFile = null;
$(".click").click(function(){
    if(lastPlayedFile !== null) {
        lastPlayedFile[0].currentTime = 0;
        lastPlayedFile.trigger('pause');
    }
    if (i< audioArray.length){
        lastPlayedFile = audioArray[i];
        audioArray[i].trigger('play');
        i++;
    } else if (i>=audioArray.length){
        i = 0;
        lastPlayedFile = audioArray[0];
        audioArray[i].trigger('play');
    };
});

这段代码不适用于我,我使用的是firefox web浏览器。这段代码有什么问题吗?

EN

回答 1

Stack Overflow用户

发布于 2018-06-18 19:17:26

  1. 请确保正确使用<audio>标记,如here所示。浏览器将拾取第一个可识别的源:

<audio controls> // browser tries to fetch horse.ogg first <source src="horse.ogg" type="audio/ogg"> // browser tries to fetch horse.mp3 if the above couldn't be recognized <source src="horse.mp3" type="audio/mpeg"> // if none files are valid the browser falls back with a message Your browser does not support the audio element. </audio>

  • Make还可以确保您尝试访问的文件确实存在。我试着找到它们,但得到了404的响应。

  • 同样这行代码什么也做不了:

lastPlayedFile[0].currentTime = 0;

确保在每个audioArray元素(song1song2) )上都设置了currentTime属性

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50888632

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档