【发布时间】:2022-01-13 22:32:44
【问题描述】:
上下文:这是一个 Soundcloud API javascript 和 iframe,当您将鼠标悬停在链接上时播放 soundcloud 链接的声音。链接已输入到表格中,以便您知道。
问题:问题在于,当我将鼠标悬停在输入到表格中的链接上时,即说我在表格中放置了多个链接(每个链接都连接到不同的声音),但是当我将鼠标悬停在每个链接上时,它们只会播放声音表中的第一个链接;表中的其他链接不播放相应的声音。有人可以帮我修改下面的代码来解决这个问题吗?下面提供与原代码集成的修改代码。
HTML(Iframe 的 src 被定制为播放该特定链接):
<div class="clickme">
<iframe id="sc-widget" width="300" height="200" allow="autoplay"
src="https://w.soundcloud.com/player/?url=https://soundcloud.com{{-list2[1]-}}&show_artwork=true"
frameborder="0" style="display: none">
</iframe>
</div>
Javascript(Soundcloud API):
<script src="https://w.soundcloud.com/player/api.js" type="text/javascript"></script>
<script type="text/javascript">
(function () {
var widgetIframe = document.getElementById('sc-widget'),
widget = SC.Widget(widgetIframe);
widget.bind(SC.Widget.Events.READY, function () {
widget.bind(SC.Widget.Events.PLAY, function () {
// get information about currently playing sound
console.log('sound is beginning to play');
});
// get current level of volume
widget.getVolume(function (volume) {
console.log('current volume value is ' + volume);
});
// set new volume level
widget.setVolume(50);
// get the value of the current position
widget.bind(SC.Widget.Events.FINISH, function () {
// get information about currently playing sound
console.log('replaying sound');
widget.seekTo(0);
widget.play();
});
});
// Shorthand for $( document ).ready()
// A $( document ).ready() block.
$(document).ready(function () {
console.log("ready!");
var menu = document.getElementsByClassName("clickme");
for (i = 0; i < menu.length; i++) {
var list = menu[i];
var link = String(list.outerHTML)
if (link.includes('soundcloud')) {
list.addEventListener("mouseenter", function (event) {
console.log('start soundcloud');
widget.play();
});
list.addEventListener("mouseleave", function (event) {
console.log('pause soundcloud ');
widget.pause();
});
}
}
});
}());
</script>
【问题讨论】:
-
所以你有多个
clickme类的 div,其中每个都包含一个单独的 iframe?你说的表是什么意思?你能提供一些有效的soundcloud链接而不是https://soundcloud.com{{-list2[1]-}} -
不,我做不到它的工作方式是soundcloud.com{{-list2[1]-}} 将我放入表中的任何内容嵌入,以便可以为该特定链接播放适当的声音。我的问题是我将多个链接放在一个表格中(表格是我拥有的网页)当我将鼠标悬停在每个链接上时,如何获得适当的声音来播放它?
-
嗯,你肯定可以用
https://soundcloud.com和表格中的实际数据组成一些实际的链接,对吗? -
好的忘记表格我需要为每个视频初始化一个播放器我该怎么做?
标签: javascript html soundcloud