Playing sound with JavaScript

前端 未结 3 2019
-上瘾入骨i
-上瘾入骨i 2020-12-03 20:38

Doesn\'t matter what I do, I simply can\'t get this to play a sound in Firefox or IE, or Chrome for that matter.





        
3条回答
  •  醉梦人生
    2020-12-03 20:54

    Try using this revised version of the function play()

    function play() 
    {
      var embed=document.createElement('object');
      embed.setAttribute('type','audio/wav');
      embed.setAttribute('data', 'c:\test.wav');
      embed.setAttribute('autostart', true);
      document.getElementsByTagName('body')[0].appendChild(embed);
    }
    

    The problem with your code was you were using the src attribute, which is for the tag. Instead, use the data attribute for the tag.



    If you are trying to get the most compatibility out of this, you should also consider adding the embed tag as an alternate for the object tag. The way it works is like this:

    
    
    
    

    This works similar to the noscript tag, where older browsers that don't support the object tag resort to the embed tag.

    提交回复
    热议问题