Create HTML dropdown list of music that can be played inside an audio player

♀尐吖头ヾ 提交于 2019-12-08 05:10:31

Simply move links to select where option value is link to source and text is song name. Change event to .on('change')

$(document).ready(function() {

  $('#selection').on('change', function() {
    change($(this).val());
  });

});


function change(sourceUrl) {
  var audio = document.getElementById("player");
  var source = document.getElementById("mp3_src");

  audio.pause();

  if (sourceUrl) {
    source.src = sourceUrl;
    audio.load();
    audio.play();
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<label for="selection">Listen:</label>
<select id="selection">
  <option value="">- Select track -</option>
  <option value="http://www.culturedub.com/assets/04-Forward.mp3">Flying in clouds</option>
  <option value="http://www.culturedub.com/assets/04-Moringa-JahYu-Remix-feat-BaNdula-1.mp3">Chilling on beach</option>
</select>
<br/>

<audio id="player" controls="controls">
  <source id="mp3_src" src="/teachings/2011_01_09_Cut.mp3" type="audio/mp3" />Your browser does not support the audio element.
</audio>

Listens to the change event of the <select> and changes the audio based on the <option> value.

HTML:

<label>Select a track:
  <select id="songpicker">
    <option selected="" disabled>Select a track</option>
    <option value="http://www.culturedub.com/assets/04-Forward.mp3">Flying in clouds</option>
    <option value="http://www.culturedub.com/assets/04-Moringa-JahYu-Remix-feat-BaNdula-1.mp3">Chilling on beach</option>
  </select>
</label>

JavaScript:

$('#songpicker').on('change', function() {
    change( $(this).val()  );
})
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!