开启和关闭音响按键不是JS工作 [英] On and Off audio button not working in JS

查看:207
本文介绍了开启和关闭音响按键不是JS工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我做了一个JavaScript脚本,将起到MP3文件,然后会有你会一下就切换上是否没了音讯应该发挥的图像。当我点击图片就停止播放的MP3文件,这就是我想要它做的事,但是当我点击它再没有恢复或重新开始的歌曲。
    
      
    

 <脚本类型=文/ JavaScript的>
VAR音频=的document.getElementById(myAudio);功能detectmob(){
         如果(navigator.userAgent.match(/ Android版/ I)//检查是否在移动
         || navigator.userAgent.match(/的webOS / I)
         || navigator.userAgent.match(/ iPhone / I)
         || navigator.userAgent.match(/ iPad的/ I)
         || navigator.userAgent.match(/ iPod的/ I)
         || navigator.userAgent.match(/黑莓/ I)
         || navigator.userAgent.match(/的Windows Phone / I)
         ){
              返回true;
          }
         其他{
                    audio.play();
              }
}
功能暂停(){
         如果(audio.play = FALSE){
            audio.play()
         }
         其他{
            audio.pause();
         }
}
在window.onload = detectmob;
< / SCRIPT><中心及GT; <输入类型=形象的onclick =暂停()SRC =/图像/ Nor.gif的风格=WIDTH:750px;高度:400像素;>< /中心及GT;


解决方案

您条件检查 audio.play = FALSE 是错误的,它是将数值播放属性覆盖默认函数引用。

您可以使用暂停属性检查音频是否暂停。

 函数暂停(){
  如果(audio.paused){
    audio.play();
  }其他{
    audio.pause();
  }
}

So I made a script in JavaScript that would play a mp3 file and then there would be an image that you would click on to toggle whether on not the audio should play. When I click on the picture it stops playing the mp3 file which is what I wanted it to do but when I click on it again it doesn't resume or start over the song.

<script type="text/javascript">
var audio = document.getElementById("myAudio"); 

function detectmob() { 
         if( navigator.userAgent.match(/Android/i) //Checks if on mobile
         || navigator.userAgent.match(/webOS/i)
         || navigator.userAgent.match(/iPhone/i)
         || navigator.userAgent.match(/iPad/i)
         || navigator.userAgent.match(/iPod/i)
         || navigator.userAgent.match(/BlackBerry/i)
         || navigator.userAgent.match(/Windows Phone/i)
         ){
              return true;
          }
         else {
                    audio.play();
              }
}
function Pause() {
         if (audio.play = false) {
            audio.play()
         }
         else {
            audio.pause();
         }
}
window.onload = detectmob;
</script>

<center> <input type="image" onclick="Pause()" src="/images/Nor.gif" style="width:750px;height:400px;"></center>

解决方案

Your condition check audio.play = false is wrong, it is assigning the value false to play property overriding the default function reference.

You can use the paused property to check whether the audio is paused.

function Pause() {
  if (audio.paused) {
    audio.play();
  } else {
    audio.pause();
  }
}

这篇关于开启和关闭音响按键不是JS工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆