Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript audio.play()html标记不';我不能玩镀铬的_Javascript_Angularjs_Google Chrome_Html5 Audio - Fatal编程技术网

Javascript audio.play()html标记不';我不能玩镀铬的

Javascript audio.play()html标记不';我不能玩镀铬的,javascript,angularjs,google-chrome,html5-audio,Javascript,Angularjs,Google Chrome,Html5 Audio,我在chrome中播放音频时遇到了一个问题,在播放调用之前未调用audio.src。不过,firefox玩得还不错。有人能推荐一下吗?下面是小提琴链接- 你也可以在这里找到代码- <html> <head> <script language="javascript"> var audioo = document.createElement("audio"); function newCa

我在chrome中播放音频时遇到了一个问题,在播放调用之前未调用audio.src。不过,firefox玩得还不错。有人能推荐一下吗?下面是小提琴链接-

你也可以在这里找到代码-

<html>
   <head>
      <script language="javascript">      
          var audioo = document.createElement("audio");

          function newCall() {        
              audioo.src = "";
              audioo.src = "http://xx.xx.xx.xx:8181/api/captcha/sound";
              audioo.play();               
          } 

          function playAgain() {               
               audioo.play(); // doesn't work in chrome :(
          } 

      </script>
   </head>
   <body>
   <button type="button" onClick="newCall()">New Captcha Call</button>
   <br>
   <button type="button" onClick="playAgain()">Replay Captcha</button>   
</body>
</html>

var audioo=document.createElement(“音频”);
函数newCall(){
audioo.src=“”;
audioo.src=”http://xx.xx.xx.xx:8181/api/captcha/sound";
audio.play();
} 
函数play(){
audioo.play();//在chrome中不起作用:(
} 
新验证码呼叫

重播验证码
更新 哦,哇,显然是因为audio.currentTime在重写src之前是不可写的。下面是您可以做的,它确实可以工作:

function newCall() {  
    audioo.src = "http://xx.xx.xx.xx:8181/api/captcha/sound";
    // make all audio attributes writeable again by resetting the src
    audioo.src = audio.src;             
} 
答案需要一点谷歌搜索,但我从这里得到:

起初的 我在Chrome上进行了测试,重置了音频位置,它成功了。首先暂停是最安全的,但不管怎样都有效

var audioo = document.createElement("audio");

// Lets add an event listener to play when we are ready to start playing
audioo.addEventListener("canplaythrough", function(){
    audioo.play(); 
}, false); 

function newCall() {        
     audioo.src = "";
     audioo.src = "http://xx.xx.xx.xx:8181/api/captcha/sound";             
} 

function playAgain() { 
     // Let's check if we are ready enough to play
     if(audioo.readyState === 4){                       
         audioo.pause(); // first pause        
         audioo.currentTime = 0; // then reset           
         audioo.play(); // then play
     } else {
         // else inform us that we are not ready to play yet.
         alert("Audio not ready yet.");
     }
} 
这里有两个有趣的资源可以帮助您:


您是否尝试过重置音频位置?
audioo.currentTime=0;
然后
audioo.play()
?尝试在音频元素上使用load event(加载事件)。在OsX Chrome v38上对我有效。0@somethinghere-.curentTime不起作用。我试过了吗?@user3137592请检查我下面的答案。我已经测试过了,它可以工作,尽管必须先加载音频。我会修改我的答案以帮助你发现。好的,我知道你想说什么了t currentTime=0对我不起作用。可能是因为我正在尝试调用Web服务(GET call)通过src属性。当我在我的机器上调用本地mp3时,即使在chrome上也能正常工作。你能尝试通过网络发送的文件吗?我已经能够通过网络实际获得音频文件播放。你的控制台中有错误吗?比如这个(如果我输入了错误的URL):
net::ERR\u FILE\u NOT\u FOUND
?不,如果是这样的话。当我单击New Call按钮时,我不会听到音频。问题是audio.play()只执行一次。如果我从newcall()函数中删除audio.play(),它将在PlayReach()中执行函数只运行一次。请检查更新,但伙计,这是一个奇怪的问题!不过解决方案是有效的。是的,它确实很奇怪,我已经尝试了所有方法。上面的更新会引发JS错误,因为audio.src未定义。以防您有兴趣知道,如果我获取音频数据(wav),audio.play()工作正常进入audio.src,而不是获取数据的url。但是,正如我们前面看到的,使用url在chrome上只播放一次:(