2016-11-16 218 views
0

我试图显示实时播放的曲目的剩余时间,但无法弄清楚,因为我的JavaScript技能不是很先进。如何使用waves实时显示剩余时间

Wavesurfer.js提供了一个名为getCurrentTime()的函数,但我不知道如何实现它,所以时间显示在播放按钮旁边。

感谢您的帮助!

<!doctype html> 
<html> 
<head> 
<meta charset="UTF-8"> 
<title>Unbenanntes Dokument</title> 

<style type="text/css"> 
body { 
    padding: 2em; 
    padding-top:4em; 
    font: "Times New Roman"; 
    color: white; 
    background-color: black; 
    letter-spacing: -.007em; 
} 
titel { line-height:1.5em; padding-bottom:13em;} 
.colme { 
    color: #B7B498 
} 
.subtitel { 
    font-style:italic;} 

#maincontent { 
    float:left; 
    width:700px; 
    padding-right:3em;} 

#sidecontent {float:left; width:300px;} 



.link {font-size:1em; float:left;} 
</style> 
</head> 

<body> 


<section id="maincontent"> 
<titel> 
<p>Lorem Ipsom<br> 
</p> 
</titel> 
<div id="waveform"></div> 
<div id="waveform-timeline"></div> 
<script src="https://cdnjs.cloudflare.com/ajax/libs/wavesurfer.js/1.2.3/wavesurfer.min.js"> 
</script> 
<script src="https://cdnjs.cloudflare.com/ajax/libs/wavesurfer.js/1.2.3/plugin/wavesurfer.timeline.min.js"></script> 
<script type="text/javascript"> 
    var wavesurfer = WaveSurfer.create({ 
    container: '#waveform', 
    waveColor: 'white', 
    progressColor: 'red', 
    audioRate: 1, 
    barWidth: 3, 
    height: 250, 
    pixelRatio:1 
}); 
wavesurfer.load('http://ia902606.us.archive.org/35/items/shortpoetry_047_librivox/song_cjrg_teasdale_64kb.mp3'); 

wavesurfer.on('ready', function() { 
    var timeline = Object.create(WaveSurfer.Timeline); 

    timeline.init({ 
    wavesurfer: wavesurfer, 
    container: '#waveform-timeline' 
    });}); 


</script> 
<button onClick="wavesurfer.playPause()"> 
     Play 
    </button> 

    <span style="padding-left:3em; font-family:Arial; font-size:0.8em;"> Recorder in 2016</span> 
</section> 

<div id="sidecontent"> 
<p>where</p> 
</div> 

<div style="clear:both"> </div> 
</body> 
</html> 
+2

获取总时间,并减去当前时间 – Feathercrown

回答

2

这里是你如何能做到这一点:

我增加了一些元素来保存totalcurrentremaining时间用于演示:

<div>Total time: <span id="time-total">0.00</span> seconds</div> 
<div>Current time: <span id="time-current">0.00</span> seconds</div> 
<div>Ramaining time: <span id="time-remaining">0.00</span> seconds</div> 

wavesurferaudioprocess事件,该事件一个函数,而它的播放文件。我用它来获取和显示的时间是这样的:

wavesurfer.on('audioprocess', function() { 
    if(wavesurfer.isPlaying()) { 
     var totalTime = wavesurfer.getDuration(), 
      currentTime = wavesurfer.getCurrentTime(), 
      remainingTime = totalTime - currentTime; 

     document.getElementById('time-total').innerText = totalTime.toFixed(1); 
     document.getElementById('time-current').innerText = currentTime.toFixed(1); 
     document.getElementById('time-remaining').innerText = remainingTime.toFixed(1); 
    } 
}); 

这是基于你的代码工作的例子:

var wavesurfer = WaveSurfer.create({ 
 
    container: '#waveform', 
 
    waveColor: 'white', 
 
    progressColor: 'red', 
 
    audioRate: 1, 
 
    barWidth: 3, 
 
    height: 250, 
 
    pixelRatio:1 
 
}); 
 
wavesurfer.load('http://ia902606.us.archive.org/35/items/shortpoetry_047_librivox/song_cjrg_teasdale_64kb.mp3'); 
 

 
wavesurfer.on('ready', function() { 
 
    var timeline = Object.create(WaveSurfer.Timeline); 
 

 
    timeline.init({ 
 
    wavesurfer: wavesurfer, 
 
    container: '#waveform-timeline', 
 
    primaryFontColor: '#fff', 
 
    primaryColor: '#fff', 
 
    secondaryColor: '#fff', 
 
    secondaryFontColor: '#fff' 
 
    });}); 
 

 
wavesurfer.on('audioprocess', function() { 
 
    if(wavesurfer.isPlaying()) { 
 
     var totalTime = wavesurfer.getDuration(), 
 
     \t currentTime = wavesurfer.getCurrentTime(), 
 
     \t remainingTime = totalTime - currentTime; 
 
     
 
     document.getElementById('time-total').innerText = Math.round(totalTime * 1000); 
 
     document.getElementById('time-current').innerText = Math.round(currentTime * 1000); 
 
     document.getElementById('time-remaining').innerText = Math.round(remainingTime * 1000); 
 
    } 
 
});
body { 
 
    padding: 2em; 
 
    padding-top:4em; 
 
    font: "Times New Roman"; 
 
    color: white; 
 
    background-color: black; 
 
    letter-spacing: -.007em; 
 
} 
 
titel { line-height:1.5em; padding-bottom:13em;} 
 
.colme { 
 
    color: #B7B498 
 
} 
 
.subtitel { 
 
    font-style:italic;} 
 

 
#maincontent { 
 
    float:left; 
 
    width:700px; 
 
    padding-right:3em;} 
 

 
#sidecontent {float:left; width:300px;} 
 

 

 

 
.link {font-size:1em; float:left;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/wavesurfer.js/1.2.3/wavesurfer.min.js"> 
 
</script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/wavesurfer.js/1.2.3/plugin/wavesurfer.timeline.min.js"></script> 
 

 
<section id="maincontent"> 
 
<titel> 
 
<p>Lorem Ipsom<br> 
 
</p> 
 
</titel> 
 
<div id="waveform"></div> 
 
<div id="waveform-timeline"></div> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/wavesurfer.js/1.2.3/wavesurfer.min.js"> 
 
</script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/wavesurfer.js/1.2.3/plugin/wavesurfer.timeline.min.js"></script> 
 

 
<button onClick="wavesurfer.playPause()"> 
 
     Play 
 
    </button> 
 

 
    <span style="padding-left:3em; font-family:Arial; font-size:0.8em;"> Recorder in 2016</span> 
 
    
 
    
 
<div>Total time: <span id="time-total">0</span> milliseconds</div> 
 
<div>Current time: <span id="time-current">0</span> milliseconds</div> 
 
<div>Ramaining time: <span id="time-remaining">0</span> milliseconds</div> 
 

 
</div> 
 
</section> 
 

 
<div id="sidecontent"> 
 
<p>where</p> 
 
</div> 
 

 
<div style="clear:both"> </div>

UPDATE

对于millisecods,只需将秒数乘以1000即可得出结果。

document.getElementById('time-total').innerText = Math.round(totalTime * 1000); 

UPDATE 2

要chnage时间轴插件的颜色,使用时间轴插件选项。您可以像这样控制4种不同的颜色:

timeline.init({ 
    wavesurfer: wavesurfer, 
    container: '#waveform-timeline', 
    primaryFontColor: '#fff', 
    primaryColor: '#fff', 
    secondaryFontColor: '#fff', 
    secondaryColor: '#fff' 
}); 
+0

非常感谢Christos!我还有一个问题,我需要做什么才能以毫秒显示剩余时间和当前时间并更改格式? – Roland

+1

这很容易。查看我更新的答案并运行更新后的代码。 –

+0

另一个问题,如果没关系。我使用的是时间轴插件,但颜色并不相应,因为我使用黑色而不是白色背景。我是否只能编辑时间轴插件的js文件中的颜色,还是有办法修改它们而无需自己托管修改的版本? – Roland