• JavaScript声音播放


    方式一:

    /**
     * 播放音频(Chrome、opera)支持
     * @param file:支持 rm,mid,wav
     */
    function  playAudio(file) {
       var embed=document.getElementById("bgsoundid");
       if(embed){
          document.removeChild(embed);
       } 
       embed = document.createElement("embed");
       embed.setAttribute('id', "bgsoundid");
       embed.setAttribute('src', file);
       embed.setAttribute('hidden', true);
       embed.setAttribute('autostart', true);
       embed.setAttribute('loop', "1");
       document.body.appendChild(embed);
    }

    方式二:

    /**
     * Iframe实现声音播放(兼容:IE、firefox、chrome、Opera)
     * @param file 支持 rm,mid,wav,*基本上都支持
     */
    function iframeAudio(file){
      var iframe=document.getElementById("audioIframe");
      if(iframe) {
        document.removeChild(iframe);
      }
      iframe = document.createElement("iframe");
      iframe.setAttribute('id', "audioIframe");
      iframe.setAttribute('src', file);
      iframe.setAttribute('height', "0");
      iframe.setAttribute('width', "0");
      iframe.setAttribute('border', "0");
      document.body.appendChild(iframe);
    }

    方式三:

    <bgsound loop='10' src='../res/audio/alarm.wav'>


    方式四:

    使用soundManageer2插件,下载地址:http://www.schillmania.com/projects/soundmanager2/#getting-started

    var  soundManager=soundManager.createSound({
      id: 'mySound',
      url: '/path/to/some.mp3',
      autoLoad: true,
      autoPlay: false,
      onload: function() {
        alert('The sound '+this.id+' loaded!');
      },
      volume: 50
    });
    //播放
    soundManager.play();
    
    //停止
    soundManager.stop();
  • 相关阅读:
    MLE
    AHOI/HNOI2018道路
    AHOI/HNOI2018排列
    推式子
    AHOI/HNOI2018游戏
    ! BJOI2018治疗之雨
    BJOI2018链上二次求和
    BJOI2018双人猜数游戏
    ! BJOI2018染色
    BJOI2018二进制
  • 原文地址:https://www.cnblogs.com/boonya/p/3299156.html
Copyright © 2020-2023  润新知