1

我将 java 脚本用于一个简单的 if else 语句,并试图向它添加一些音频。在正确的时候播放一首歌曲,如果不好则播放另一首。这是我减去实际 if 语句的内容。我注释掉了音频部分,因为这破坏了我的代码并且无法让它工作。

if (x is true)
      {
    document.write("Good job!<br />");

        /*<audio controls = "controls" autoplay = "autoplay" loop ="loop" >
        <source src = "congrats.ogg" type = "audio/ogg" />
        <source src = "congrats.mp3" type = "audio/mpeg" />
        <source src = "congrats.wav" type = "audio/wav" />
        Your browser does not support the audio element
        </audio>*/
      }
else
      {
    document.write("Boo!<br />");
      }

任何帮助表示赞赏!

更新澄清:我想说如果我的“if 语句”执行,播放 song1,如果我的“else 语句”执行,播放 song2。

更新,工作!: 做了更多的研究,并能够找到以下对我有用的代码片段。

http://www.position-absolute.com/articles/introduction-to-the-html5-audio-tag-javascript-manipulation/

4

2 回答 2

0

使用 JavaScript,您可以执行以下操作:

var supports = function(mime) { return (new Audio()).canPlayType('audio/' + mime); };
var sound;

if (supports('ogg; codecs=vorbis')) {
    sound = new Audio('congrats.ogg');
} else if (supports('mpeg')) {
    sound = new Audio('congrats.mp3');
} else if (supports('wav')) {
    sound = new Audio('congrats.wav');
} else {
  alert('Your browser is horrible');
}

if (typeof sound !== 'undefined') {
    sound.play();
}
于 2012-09-18T01:53:30.023 回答
-1

你真的有if (x is true)吗?

那应该只是if (x)

于 2012-09-18T01:42:11.900 回答