【问题标题】:How to create an audio player using html and javascript如何使用 html 和 javascript 创建音频播放器
【发布时间】:2016-03-11 11:59:53
【问题描述】:

我正在尝试使用 html 和 javascript 创建音频播放器,没有 jquery。我有一些专辑需要返回专辑中的歌曲。然后我会选择一首歌曲,它会播放。到目前为止,这是我的代码。我知道这非常混乱,我只是一个初学者,所以请原谅。

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="music.js"></script>
    <style type="text/css"></style>
</head>    
<body>

    <table width="400" height="400" border="1" style="display: inline-block;">
    <caption>Albums</caption>
<tr>
    <td><p>Please select an album from the list
<select name='Album'>
<option>Birdsong Small Birds</option>
<option>Birdsong Large Birds</option>
<option>Birdsong Medium Birds</option>
<option>Birdsong Finches</option>
<option>Birdsong Tits</option>
</select>
</select>
</td>
</tr>
</table>


<table width="400" height="400" border="1" style="display: inline-block;">
<caption>Songs</caption>
<tr>
    <td>a</td>
</tr>
</table>


<table width="400" height="400" border="1" style="display: inline-block;">
<caption>
Selected Songs
</caption>
<tr>
<td>a</td>
</tr>
</table>
</body>
</html>            

所以当我在第一个表中选择一张专辑时,我选择的歌曲将出现在第二个表中。然后当我在第二个表中选择一首歌曲时,它将在最后一个表中播放

这是我的最终作品应该是什么样子的示例
Example
我有一个 music.js 文件,格式为:

var albums= [ { "title":"Birdsong Small Birds", "artist":"BBC", “艺术品”:“Wren”,“曲目”:[ {"title":"Dunnock","mp3":"Birdsong-Dunnock.mp3", "lyrics":"The 邓诺克或树篱麻雀有一首快速的鸣叫,通常来自 树篱或灌木的顶部"},

这是一小部分,因为它包含每首歌曲的所有专辑文件夹。

【问题讨论】:

  • 试试这个:kolber.github.io/audiojs
  • 我创建了示例(在 codepin.io 中起草)。 codepen.io/anon/pen/rxNXdd 这可能会有所帮助。只需询问您是否需要更多解释。
  • 感谢您的帮助。我对帖子底部进行了编辑,以便您更好地理解。我仍然不明白如何从我会选择的专辑中获取歌曲的下拉列表。很抱歉打扰了。另外,我会将 javascript 代码放在我的 HTML 代码中的什么位置?
  • @RileyAnderson 如果有帮助,您需要接受答案。

标签: javascript html audio drop-down-menu


【解决方案1】:

我更改了codepin 示例。您可以在 pin link to codepin.io 中查看工作示例

我更改了专辑和歌曲自动呈现的 HTML 代码。

添加了&lt;audio&gt; 标签来播放我们的mp3。删除 &lt;select&gt; options 以按代码填充。

这里是代码

更新

var albums = [{
    "title": "Birdsong Small Birds",
    "artist": "BBC",
    "artwork": "Wren",
    "tracks": [{
      "title": "Dunnock",
      "mp3": "Birdsong-Dunnock.mp3",
      "lyrics": "The Dunnock or hedge sparrow has a fast warbling song often delivered from the top of a hedge or bush"
    }, {
      "title": "Another Dunnock",
      "mp3": "http://www.noiseaddicts.com/samples_1w72b820/272.mp3",
      "lyrics": "The Dunnock or hedge sparrow has a fast warbling song often delivered from the top of a hedge or bush"
    }, {
      "title": "Third Dunnock",
      "mp3": "Third-Dunnock.mp3",
      "lyrics": "The Dunnock or hedge sparrow has a fast warbling song often delivered from the top of a hedge or bush"
    }]
  },
  {
    "title": "Second Birdsong Birds",
    "artist": "BBC",
    "artwork": "Wren",
    "tracks": [{
      "title": "Dunnock in Second",
      "mp3": "Birdsong-Dunnock.mp3",
      "lyrics": "The Dunnock or hedge sparrow has a fast warbling song often delivered from the top of a hedge or bush"
    }, {
      "title": "Another Dunnock  in Second",
      "mp3": "http://www.noiseaddicts.com/samples_1w72b820/272.mp3",
      "lyrics": "The Dunnock or hedge sparrow has a fast warbling song often delivered from the top of a hedge or bush"
    }, {
      "title": "Third Dunnock  in Second",
      "mp3": "Third-Dunnock.mp3",
      "lyrics": "The Dunnock or hedge sparrow has a fast warbling song often delivered from the top of a hedge or bush"
    }]
  }
];

//add onchange event
var albumElement = document.getElementById('album-select');
albumElement.addEventListener('change', function() {
  // populate songs for selected album
  populateSongs(albumElement.value)
});

// fill Albums from database (albums JSON obj)
for (var i = 0; albums.length; i++) {
  var option = document.createElement("option");
  option.text = albums[i].title;
  albumElement.add(option)
}

function populateSongs(album) {
  var songsTable = document.getElementById('songs-table');

  //delete old songs
  while (songsTable.rows.length > 0) {
    songsTable.deleteRow(0);
  }


  //populate songs in table
  // loop through albums
  for (var i = 0; albums.length; i++) {

    //check selected album
    if (albums[i].title == album) {

      //found album: loop through tracks 
      for (var track = 0; albums[i].tracks.length; track++) {
        //add new <td> and <td>
        var row = songsTable.insertRow(track);
        var cell = row.insertCell(0);

        cell.innerHTML = albums[i].tracks[track].title;

        // add attribute to <td> for mp3 file.
        // we need mp3, title and album onClick
        // creates something like: 
        //         <td title="Song title" album="Album title" file="file.mp3"
        //            Song title
        //         </td>

        cell.setAttribute("title", albums[i].tracks[track].title);
        cell.setAttribute("album", albums[i].title);
        cell.setAttribute("file", albums[i].tracks[track].mp3);


        // add click event
        cell.addEventListener('click', function() {
          // pass clicked <td>
          // this <td> has all data 
          play(this);
        });

      }
    }
  }

  // Add some text to the new cells:


}

function play(element) {
  // retrieve passed data from element attributes
  var songTitle = element.getAttribute('title');
  var albumTitle = element.getAttribute('album');
  var songFile = element.getAttribute('file');

  document.getElementById('audio-player').src = songFile;
  document.getElementById('song-album').innerHTML = albumTitle;
  document.getElementById('song-title').innerHTML = songTitle;

  console.log(song);

}
<html>

<head>

  <style type="text/css"></style>
</head>

<body>

  <table width="400" border="1" style="display: inline-block;">
    <caption>Albums</caption>
    <tr>
      <td>
        <p>Please select an album from the list</p>
        <select id="album-select" name='Album'>
          <option></option>
        </select>
    </tr>
  </table>


  <table id="songs-table" width="400" border="1" style="display: inline-block;">
    <caption>Songs</caption>
    <tr>
      <td></td>
    </tr>
  </table>


  <table width="400" border="1" style="display: inline-block;">
    <caption>
      Selected Songs
    </caption>
    <tr>
      <td>
        <h3 id="song-album">Choosen Album </h3>
        <h4 id="song-title">Song 1</h4>
        <!-- play() adds src attribute -->
        <audio id="audio-player" autoplay controls title="TJs PL">
    Your browser does not support the audio tag.
  </audio>
      </td>
    </tr>
  </table>
</body>

</html>

【讨论】:

  • 感谢您的帮助。我仍然不确定如何在每张专辑被选中后获取歌曲列表。我尝试了多种方法,但到目前为止都没有奏效。我已经收到了 mp3 和 wav 格式的每首歌曲,并且应该使用代码将两者都包含在内。 &lt;audio controls='controls'&gt; &lt;source src='xxxxx.mp3' type='audio/mpeg'&gt; &lt;source src='xxxxx.wav' type='audio/wav'&gt; Your browser does not support the audio element. &lt;/audio&gt;
  • 您必须将 wavmp3 文件添加到 JSON 对象中的轨道数组中。然后你可以在play()函数中循环遍历不同的格式。
  • 感谢 Maxali 的帮助。我仍然不确定该怎么做,我让下拉列表工作,当我选择一张专辑时,它会返回该专辑中的歌曲。但是我该如何做到这一点,以便当我选择一首歌曲时它会播放。
  • 我如何在最后一个表格中添加艺术作品、专辑和曲目标题,如示例图片所示?
  • @RileyAnderson 现在检查代码(运行 sn-p 或 codepin.io)。它就像您的示例图片一样工作。我不改变 UI,所以尝试使用一些 CSS。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-07
  • 1970-01-01
相关资源
最近更新 更多