简体   繁体   English

如何链接来自 <ul> 播放清单相同 <audio> 标签?

[英]How can I link different MP3 files from an <ul> playlist to the same <audio> tag?

I found a good tutorial on how to build a playlist with HTML 5 <audio> tag and JavaScript ( http://blog.lastrose.com/html5-audio-video-playlist/ ). 我找到了一个很好的教程,介绍如何使用HTML 5 <audio>标签和JavaScript( http://blog.lastrose.com/html5-audio-video-playlist/ )构建播放列表。

But when I copy that code into my site, it simply doesn't work well, as every element of the playlist (if clicked) launch a new audio player in a new page instead of being played by the same player above the playlist. 但是,当我将该代码复制到我的网站中时,它根本无法正常工作,因为播放列表的每个元素(如果单击)都会在新页面中启动新的音频播放器,而不是由播放列表上方的同一播放器播放。

How can I link different MP3 files from an <ul> playlist to the same <audio> tag? 如何将<ul>播放列表中的不同MP3文件链接到相同的<audio>标签?

This is my code 这是我的代码

 var video_player = document.getElementById("video_player"); var links = video_player.getElementsByTagName('a'); for (var i = 0; i < links.length; i++) { links[i].onclick = handler; } function handler(e) { e.preventDefault(); videotarget = this.getAttribute("href"); filename = videotarget.substr(0, videotarget.lastIndexOf('.')) || videotarget; video = document.querySelector("#video_player video"); video.removeAttribute("controls"); video.removeAttribute("poster"); source = document.querySelectorAll("#video_player video source"); source[0].src = filename + ".mp4"; source[1].src = filename + ".webm"; video.load(); video.play(); } 
 <audio controls id="player"> <source src="01 Scusate.mp3" type="audio/mp3"> <source src="22 Gli affitti.mp3" type="audio/mp3"> </audio> <ul id="playlist"> <li class="playlist"><a href="01 Scusate.mp3" class="p">Scusate - Gino Negri performer</a></li> <li class="playlist"><a href="22 Gli affitti.mp3" class="p">Gli affitti - Sandro Massimini</a></li> </ul> 

I'd be very grateful to anybody who is able to find the mistake in JavaScript and suggest me a solution. 非常感谢能够在JavaScript中发现错误并提出解决方案的任何人。

I have this functionality on our band's page. 我在乐队的页面上有此功能。

Here's how I did it: 这是我的操作方式:

/* HTML5 player */
$('#playlist li').on('click', pickSong);

var pickSong = function(ev){
var item = ev.target;
var audio = $("audio")[0];
var pad = 'muziek\\';
audio.pause();
$('#mp3src').attr('src', '').attr('src', pad.concat(item.getAttribute('data-val'),'.mp3'));
$('#oggsrc').attr('src', '').attr('src', pad.concat(item.getAttribute('data-val'),'.ogg'));
audio.load();
audio.play();

$('#playlist li').removeClass('plays');
$(item).addClass('plays');

} }

This is in the HTML: 在HTML中:

<audio controls preload="none">
    <source id="mp3src" src="muziek/friend.mp3" type="audio/mp3">
    <source id="oggsrc" src="muziek/friend.ogg" type="audio/ogg">
</audio>
<ul id="playlist">
    <li class="plays" data-val="friend"></li>
    <li data-val="mercy"></li>
    <li data-val="anouk"></li>
    <li data-val="tmyfml"></li>
    <li data-val="krezip"></li>
</ul>

There are some difference with your code. 您的代码有些不同。 Put one file in audio tag as jsfiddler example and the other file on tag as the example. 将一个文件放在audio标签中作为jsfiddler示例,将另一个文件放在tag中作为示例。 Try to past the example in you code and try it. 尝试将示例粘贴到代码中,然后尝试。 Then, if works, adpt for you needs ( http://blog.lastrose.com/html5-audio-video-playlist/ ) 然后,如果可行,请根据需要进行调整( http://blog.lastrose.com/html5-audio-video-playlist/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM