好吧,我试图把我的权利旁边右上角时间声音播放流。我到处寻找,有很多方法可用于mp3等音频ogg,但没有什么简单的方法来显示他的soundcloud时间。 一个想法?在javascript中soundcloud的当前时间和持续时间
这里是我的代码:
<head>
<!-- SoundCloud-->
<script type="text/javascript" src="http://w.soundcloud.com/player/api.js"></script>
<script src="//connect.soundcloud.com/sdk-2.0.0.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<body>
<div id="slider-volume"></div>
<script src='//w.soundcloud.com/player/api.js'></script>
<div class="player">
<span class='info'></span>
<div class="waveform"></div>
<div class="position"></div>
<div id="playSound"></div>
<div id="stopSound"></div>
</div>
<iframe class="sc-widget" src="//w.soundcloud.com/player/?url=//soundcloud.com/cob/catchme&color=FF9900&show_bpm=true&hide_related=true&show_comments=true&show_user=true&show_reposts=true"></iframe>
</body>
<style type="text/css">
body {
overflow: hidden;
background: transparent;
}
#slider-volume {
position: absolute;
top: 29px;
left: 75px;
height: 5px;
background: rgba(255, 86, 0, 1);
border-color: transparent;
border-radius: 10px;
width: 190px;
border: none;
-webkit-transition: all 0.4s ease;
-moz-transition: all 0.4s ease;
-o-transition: all 0.4s ease;
-ms-transition: all 0.4s ease;
transition: all 0.4s ease;
cursor: pointer;
}
#slider-volume .ui-slider-range-min {
position: absolute;
height: 5px;
border-radius: 10px;
border-width: 0px;
background: rgba(255, 153, 0, 1);
border: none;
-webkit-transition: all 0.4s ease;
-moz-transition: all 0.4s ease;
-o-transition: all 0.4s ease;
-ms-transition: all 0.4s ease;
transition: all 0.4s ease;
cursor: pointer;
}
#slider-volume .ui-slider-handle {
opacity: 1;
filter: alpha(opacity=1);
border-radius: 10px;
position: absolute;
width: 10px;
height: 10px;
top: -2.5px;
outline: none;
border: 1px solid rgba(251, 27, 10, 1);
background: rgba(0, 0, 0, 1);
cursor: pointer;
}
#slider-volume .ui-slider-handle:hover,
#slider-volume:hover {
cursor: pointer;
-webkit-box-shadow: 0px 0px 3px 1px rgba(255, 255, 255, 1);
-moz-box-shadow: 0px 0px 3px 1px rgba(255, 255, 255, 1);
box-shadow: 0px 0px 3px 1px rgba(255, 255, 255, 1);
-webkit-transition: all 0.4s ease;
-moz-transition: all 0.4s ease;
-o-transition: all 0.4s ease;
-ms-transition: all 0.4s ease;
transition: all 0.4s ease;
}
#playSound {
z-index: 5;
position: fixed;
background-color: rgba(251, 27, 10, 1);
border: 1px solid rgba(251, 27, 10, 1);
-webkit-border-radius: 100%;
-moz-border-radius: 100%;
border-radius: 100%;
top: 7px;
left: 37px;
width: 25px;
height: 25px;
background-image: url(${resourcesFolderName}/play_hover_c.png);
background-repeat: no-repeat;
background-size: 20px 20px;
background-position: 50% 50%;
-webkit-box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.3);
-moz-box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.3);
box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.3);
-webkit-transition: all 0.4s ease;
-moz-transition: all 0.4s ease;
-o-transition: all 0.4s ease;
-ms-transition: all 0.4s ease;
transition: all 0.4s ease;
cursor: pointer;
}
#stopSound {
z-index: 5;
position: fixed;
background-color: rgba(251, 27, 10, 1);
border: 1px solid rgba(251, 27, 10, 1);
-webkit-border-radius: 100%;
-moz-border-radius: 100%;
border-radius: 100%;
top: 7px;
left: 7px;
width: 25px;
height: 25px;
background-image: url(${resourcesFolderName}/pause_hover_c.png);
background-repeat: no-repeat;
background-size: 20px 20px;
background-position: 50% 50%;
-webkit-box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.3);
-moz-box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.3);
box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.3);
-webkit-transition: all 0.4s ease;
-moz-transition: all 0.4s ease;
-o-transition: all 0.4s ease;
-ms-transition: all 0.4s ease;
transition: all 0.4s ease;
cursor: pointer;
}
#playSound:hover {
position: fixed;
background-color: white;
border: 4px solid white inset;
-webkit-border-radius: 100%;
-moz-border-radius: 100%;
border-radius: 100%;
top: 7px;
left: 37px;
width: 25px;
height: 25px;
background-image: url(${resourcesFolderName}/play_c.png);
background-repeat: no-repeat;
background-size: 20px 20px;
background-position: 50% 50%;
border-color: transparent;
-webkit-box-shadow: 0px 0px 5px 5px rgba(250, 250, 250, 0.2);
-moz-box-shadow: 0px 0px 5px 5px rgba(250, 250, 250, 0.2);
box-shadow: 0px 0px 5px 5px rgba(250, 250, 250, 0.2);
}
#stopSound:hover {
position: fixed;
background-color: white;
border: 4px solid white inset;
-webkit-border-radius: 100%;
-moz-border-radius: 100%;
border-radius: 100%;
top: 7px;
left: 7px;
width: 25px;
height: 25px;
background-image: url(${resourcesFolderName}/pause_c.png);
background-repeat: no-repeat;
background-size: 20px 20px;
background-position: 50% 50%;
border-color: transparent;
-webkit-box-shadow: 0px 0px 5px 5px rgba(250, 250, 250, 0.2);
-moz-box-shadow: 0px 0px 5px 5px rgba(250, 250, 250, 0.2);
box-shadow: 0px 0px 5px 5px rgba(250, 250, 250, 0.2);
}
iframe {
position: fixed;
top: 50%;
left: 10%;
width: 80%;
height: 166px;
border: 2px solid rgba(255, 86, 0, 1);
border-radius: 5px;
-webkit-box-shadow: 0px 0px 20px 0px rgba(0, 0, 0, 0.9);
-moz-box-shadow: 0px 0px 20px 0px rgba(0, 0, 0, 0.9);
box-shadow: 0px 10px 20px 0px rgba(0, 0, 0, 0.9);
-webkit-transition: all 0.4s ease;
-moz-transition: all 0.4s ease;
-o-transition: all 0.4s ease;
-ms-transition: all 0.4s ease;
transition: all 0.4s ease;
}
.player {
cursor: pointer;
position: fixed;
top: 4px;
left: 75px;
/*4 et 75*/
margin: auto;
height: 20px;
width: 190px;
font-family: 'Yantramanav', sans-serif;
background-color: rgba(251, 27, 10, 0.9);
transition: background-image 1s linear;
-webkit-border-radius: 5px;
-webkit-border-top-left-radius: 5px;
-webkit-border-bottom-left-radius: 5px;
border-radius: 5px;
-webkit-transition: all 0.4s ease;
-moz-transition: all 0.4s ease;
-o-transition: all 0.4s ease;
-ms-transition: all 0.4s ease;
transition: all 0.4s ease;
}
.waveform {
z-index: 3;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background-color: rgba(255, 86, 10, 0.1);
transition: background-image 1s linear;
-webkit-border-radius: 5px;
-webkit-border-top-left-radius: 5px;
-webkit-border-bottom-left-radius: 5px;
border-radius: 5px;
-webkit-transition: all 0.4s ease;
-moz-transition: all 0.4s ease;
-o-transition: all 0.4s ease;
-ms-transition: all 0.4s ease;
transition: all 0.4s ease;
}
.position {
position: absolute;
height: 100%;
width: 0px;
height: 20px;
background-color: rgba(251, 153, 0, 0.5);
-webkit-border-radius: 5px;
-webkit-border-top-left-radius: 5px;
-webkit-border-bottom-left-radius: 5px;
border-radius: 5px;
transition: width .1s linear;
-webkit-transition: all 0.4s ease;
-moz-transition: all 0.4s ease;
-o-transition: all 0.4s ease;
-ms-transition: all 0.4s ease;
transition: all 0.4s ease;
}
.info {
user-select: none;
z-index: 1;
position: absolute;
width: 80%;
left: 70px;
margin: 0.3em -5.5em;
font-size: 0.8em;
letter-spacing: .03em;
display: block;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
text-align: left;
color: white;
}
.player .waveform:hover {
cursor: pointer;
-webkit-box-shadow: 0px 0px 4px 1px rgba(255, 255, 255, 1);
-moz-box-shadow: 0px 0px 4px 1px rgba(255, 255, 255, 1);
box-shadow: 0px 0px 4px 1px rgba(255, 255, 255, 1);
-webkit-transition: all 0.4s ease;
-moz-transition: all 0.4s ease;
-o-transition: all 0.4s ease;
-ms-transition: all 0.4s ease;
transition: all 0.4s ease;
}
</style>
<script type="text/javascript">
SC.initialize({
client_id: "70919eb43a47839a9c5fe09ae9be450f",
redirect_uri: "http://127.0.0.1:64184/preview/callback",
});
var sounds = null;
SC.stream("/tracks/225335752", {
autoPlay: false,
show_comments: true,
show_user: true,
}, function(sound) {
var widget1 = SC.Widget($('iframe.sc-widget')[0]);
$("#slider-volume").slider("option", "step", 0.01);
$("#slider-volume").slider("option", "max", 1);
$("#slider-volume").slider("option", "value", sound.getVolume());
$('#playSound').click(function(e) {
e.preventDefault();
widget1.toggle();
e.stopPropagation();
});
$('#stopSound').click(function(e) {
e.preventDefault();
widget1.pause();
e.stopPropagation();
widget1.seekTo(0);
});
$("#slider-volume").on("slidechange", function(event, ui) {
widget1.setVolume(ui.value);
});
});
$(function() {
$("#slider-volume").slider();
});
$("#slider-volume").slider({
orientation: "horizontal",
range: "min",
min: 0,
max: 100,
value: 100,
slide: function(event, ui) {
$("#amount").val(ui.value);
}
});
$(document).ready(function() {
var player = SC.Widget($('iframe.sc-widget')[0]);
var pOffset = $('.player').offset();
var pWidth = $('.player').width();
var scrub;
player.bind(SC.Widget.Events.READY, function() {
setInfo();
player.pause();
}); //Set info on load
player.bind(SC.Widget.Events.PLAY_PROGRESS, function(e) {
if (e.relativePosition < 0.003) {
setInfo();
}
//Event listener when track is playing
$('.position').css('width', (e.relativePosition * 100) + "%");
});
$('.player').mousemove(function(e) { //Get position of mouse for scrubbing
scrub = (e.pageX - pOffset.left);
});
$(document).on('keydown', function(e) {
switch (e.keyCode) {
case 32:
player.toggle();
break;
case 39:
player.next();
break;
case 37:
player.prev();
break;
}
});
$('.player').click(function() { //Use the position to seek when clicked
$('.position').css('width', scrub + "px");
var seek = player.duration * (scrub/pWidth);
//Seeking to the start would be a previous?
if (seek < player.duration * 0.05) {
player.prev();
} else if (seek > player.duration * 0.99) {
player.next();
} else {
player.seekTo(seek);
}
});
function setInfo() {
player.getCurrentSound(function(song) {
console.log(song);
$('.position').css('background-color');
$('.player').css('background-color');
var info = song.title;
$('.info').html(info);
player.current = song;
});
player.getDuration(function(value) {
player.duration = value;
});
player.isPaused(function(bool) {
player.getPaused = bool;
});
}
});
</script>
我不是很好的代码,这不是我的专业,但我每天学习新的功能。
我会做的是:
触发的iframe的SoundCloud的播放按钮:OK完成
停止它:好做
工程量与当前的声音: ok done
但显示当前时间和持续时间,并与当前回放一起工作:???
我试图用一个例子:
但我想的时间被实时显示。
任何信息将帮助我
最好
嗨, 伟大的HARTGER答案... – cob
@cob如果我理解你的权利,你试图同步数据是存在于您的视图中的元素与您的JavaScript。要做到这一点,你必须观察javascript中值的变化并将其应用于视图。这被称为'数据绑定'。最简单的方法就是使用一个框架或库来为你做这件事。我建议你查看一下轻量级的数据绑定库(Rivet.JS也是这样,但我没有经验)。 – Hartger