创建可以在音频播放器中播放的音乐的HTML下拉列表

时间:2016-10-27 08:26:22

标签: javascript html audio-player

我正在尝试为我的网站创建一个简单的音乐播放器,用户可以从下拉列表中选择多个不同的音乐曲目。音乐存储在我们的服务器上。

这是我到目前为止所做的:http://jsfiddle.net/rUf6j/7/我只需要将选项移到下拉列表中。

HTML:

<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>

<table>
    <tr>
        <td>Flying in clouds</td>
        <td><a href="#" source="http://www.culturedub.com/assets/04-Forward.mp3">Listen</a></td>
    </tr>
    <tr>
        <td>Chilling on beach</td>
        <td><a href="#" source="http://www.culturedub.com/assets/04-Moringa-JahYu-Remix-feat-BaNdula-1.mp3">Listen</a></td>
    </tr>
</table>

jQuery的:

$(document).ready(function(){
    $('[source]').on('click', function(){
        change( $(this).attr('source')  );
    });
});  

JS:

 function change(sourceUrl) {
    var audio = document.getElementById("player"),
    source = document.getElementById("mp3_src");
    source.src = sourceUrl;
    audio.pause();
    audio.load();
    audio.play();
 }

3 个答案:

答案 0 :(得分:0)

收听var observable_1 = require("data/observable"); var ValueModel = (function (_super) { __extends(ValueModel, _super); function ValueModel() { _super.call(this); this.model = new Value("This is a text", "INITIAL VALUE 0"); this.model.value = "VALUE 1"; } Object.defineProperty(PersonViewModel.prototype, "model", { get: function () { return this.get("_model"); }, set: function (value) { this.set("_model", value); }, enumerable: true, configurable: true }); return PersonViewModel; }(observable_1.Observable)); 的更改事件,并根据<select>值更改音频。

HTML:

<option>

JavaScript的:

<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>

答案 1 :(得分:0)

只需将链接移至select,其中option值链接到源,文字是歌曲名称。将活动更改为.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>

答案 2 :(得分:0)

$('#songpicker').on('change', function() {
    change( $(this).val()  );
})
<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>