最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - How to use JavaScript to control audio element - Stack Overflow

programmeradmin3浏览0评论

Firstly I'm trying not to use the default html5 standard controls and I'd be happy to use jQuery if possible but I've taken it out for now as I was unsure of what the problem is.

At the moment I'm simply trying to have a play button which plays some music once clicked, which will change to a pause button. This pause button will then obviously pause the music once clicked.

I will leave some annotation in so you can see some of the things I have tried. Currently the play button appears but nothing happens when it is clicked.

Any help would be greatly appreciated.

HTML:

<head>
<script src=".1.1/jquery.min.js"></script>
<script src="javascript/audio-controls.js"></script>
<link rel="stylesheet" href="css/menu.css">
</head>

<div id="content-container">
     <audio id="music">
     <source src="media/mexico-music.mp3" type="audio/mpeg"/>
     </audio>
     <button id="play-music-button" class="play"></button>
</div>

CSS:

#play-music-button {
    height:60px;
    width: 60px;
    border: none;
    background-size: 50% 50%;
    background-position: center;
}

.play {
    background: url("../assets/play.png") no-repeat;
}

.pause {
    background: url("../assets/pause.png") no-repeat;
}

JS:

$(document).ready(function() {
var music = document.getElementById("music");
var play_music_button = document.getElementById("play-music-button");

function playAudio() {
    if (music.paused) {
        music.play();
        play_music_button.innerHTML = "Pause";
        /*play-music-button.className = "";
        play-music-button.className = "pause";*/
    } else {
        music.pause();
        play_music_button.innerHTML = "Resume";
        /*play-music-button.className = "";
        play-music-button.className = "play";*/
    }
    music.addEventListener('ended',function() {
        play_music_button.innerHTML = "Play";
    });
}
play-music-button.addEventListener("click", playAudio);
});

Firstly I'm trying not to use the default html5 standard controls and I'd be happy to use jQuery if possible but I've taken it out for now as I was unsure of what the problem is.

At the moment I'm simply trying to have a play button which plays some music once clicked, which will change to a pause button. This pause button will then obviously pause the music once clicked.

I will leave some annotation in so you can see some of the things I have tried. Currently the play button appears but nothing happens when it is clicked.

Any help would be greatly appreciated.

HTML:

<head>
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="javascript/audio-controls.js"></script>
<link rel="stylesheet" href="css/menu.css">
</head>

<div id="content-container">
     <audio id="music">
     <source src="media/mexico-music.mp3" type="audio/mpeg"/>
     </audio>
     <button id="play-music-button" class="play"></button>
</div>

CSS:

#play-music-button {
    height:60px;
    width: 60px;
    border: none;
    background-size: 50% 50%;
    background-position: center;
}

.play {
    background: url("../assets/play.png") no-repeat;
}

.pause {
    background: url("../assets/pause.png") no-repeat;
}

JS:

$(document).ready(function() {
var music = document.getElementById("music");
var play_music_button = document.getElementById("play-music-button");

function playAudio() {
    if (music.paused) {
        music.play();
        play_music_button.innerHTML = "Pause";
        /*play-music-button.className = "";
        play-music-button.className = "pause";*/
    } else {
        music.pause();
        play_music_button.innerHTML = "Resume";
        /*play-music-button.className = "";
        play-music-button.className = "play";*/
    }
    music.addEventListener('ended',function() {
        play_music_button.innerHTML = "Play";
    });
}
play-music-button.addEventListener("click", playAudio);
});
Share Improve this question edited Feb 27, 2017 at 17:58 Xander asked Feb 27, 2017 at 17:41 XanderXander 1,0111 gold badge16 silver badges33 bronze badges 2
  • Your JavaScript code has syntax errors in it right now (play-music-button is not a valid identifier). It's not clear what you expect to happen by including the addEventListener twice. – Heretic Monkey Commented Feb 27, 2017 at 17:47
  • Wasn't sure whether to add it inside the doc ready function or outside so that's the only reason it's currently in both. @MikeMcCaughan – Xander Commented Feb 27, 2017 at 17:51
Add a ment  | 

1 Answer 1

Reset to default 3

You're calling play_music_button as play-music-button, and you've defined that twice when you only need it inside of your defined function. And you need to add an ended event listener to set the button back to "Play"

$(document).ready(function() {
  var music = document.getElementById("music");
  var play_music_button = document.getElementById("play-music-button");

  function playAudio() {
    if (music.paused) {
      music.play();
      play_music_button.className = 'pause';
    } else {
      music.pause();
      play_music_button.className = 'play';
    }
    music.addEventListener('ended',function() {
      play_music_button.className = 'play';
    });
  }
  play_music_button.addEventListener("click", playAudio);
});
#play-music-button {
  height: 60px;
  width: 60px;
  border: none;
  background-size: 50% 50%;
  background-position: center;
}

.play {
  background: url("https://image.flaticon./icons/png/512/0/375.png") no-repeat;
}

.pause {
  background: url("http://downloadicons/sites/default/files/pause-icon-14021.png") no-repeat;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="content-container">
  <audio id="music">
    <source src="http://skwaat./clips/iloveyou.mp3" type="audio/mpeg">
  </audio>
  <button id="play-music-button" class="play"></button>
</div>

发布评论

评论列表(0)

  1. 暂无评论