Forum Moderators: open
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1">
<title>Stop Audio at specified point</title>
<!--<link rel="stylesheet" href="screen.css" media="screen">-->
<style media="screen">
body {
background-color: #f0f0f0;
font: 1em/150% verdana,arial,helvtica,sans-serif;
}
#audio-container {
max-width: 30em;
padding: 2.5em;
margin: 2em auto;
border: 0.06em solid #999;
border-radius: 1em;
box-shadow: inset 0 0 2em #999;
box-sizing: border-box;
background-color: #fff;
}
#audio-container audio {
display: block;
width: 100%;
}
</style>
</head>
<body>
<div id="audio-container">
<audio id="player" controls preload>
<source src="http://coothead.co.uk/audio/Leadbelly-GoodnightIrene.mp3" type="audio/mpeg">
</audio>
</div>
<script>
(function() {
'use strict';
var se;
var stoppoint=29; /* the audio stops here in this value plus 1 seconds */
var temp=stoppoint;
var delay=1000;
var p=document.getElementById('player');
p.addEventListener('play',playIt,false);
p.addEventListener('pause',pauseIt,false);
p.addEventListener('ended',playItAgain,false);
function playIt(){
if(p.currentTime>stoppoint){
clearTimeout(se);
if(stoppoint<p.duration-1) {
p.pause();
alert('This is for testing purposes only.\n\n'+
'Any code that is required will go here.');
}
stoppoint=p.duration;
return;
}
var se=setTimeout(playIt,delay);
}
function pauseIt(){
clearTimeout(se);
return;
}
function playItAgain(){
stoppoint=temp;
p.load();
}
}());
</script>
</body>
</html>
What does 'use strict' do?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1">
<title>Stop Audio at specified point</title>
<!--<link rel="stylesheet" href="screen.css" media="screen">-->
<style media="screen">
body {
background-color: #f0f0f0;
font: 1em/150% verdana,arial,helvtica,sans-serif;
}
#audio-container {
max-width: 30em;
padding: 2.5em;
margin: 2em auto;
border: 0.06em solid #999;
border-radius: 1em;
box-shadow: inset 0 0 2em #999;
box-sizing: border-box;
background-color: #fff;
}
#audio-container audio {
display: block;
width: 100%;
}
</style>
</head>
<body>
<div id="audio-container">
<audio id="player" controls preload>
<source src="http://coothead.co.uk/audio/Leadbelly-GoodnightIrene.mp3" type="audio/mpeg">
</audio>
</div>
<script>
(function() {
'use strict';
var p=document.getElementById('player');
var beforeend=30; /* adjust value to suit */
var stoppoint;
var temp;
var se;
var delay=1000;
var test=true;
function init() {
stoppoint=p.duration-beforeend;
temp=stoppoint;
}
p.addEventListener('play',playIt,false);
p.addEventListener('pause',pauseIt,false);
p.addEventListener('ended',playItAgain,false);
function playIt(){
if(p.currentTime>stoppoint){
clearTimeout(se);
if(test===true) {
test=false;
alert('This is for testing purposes only.\n\n'+
'Any code that is required will go here.');
}
stoppoint=p.duration;
return;
}
var se=setTimeout(playIt,delay);
}
function pauseIt(){
clearTimeout(se);
return;
}
function playItAgain(){
stoppoint=temp;
test=true;
p.load();
}
setTimeout(init,delay);
}());
</script>
</body>
</html>
No problem, you're very welcome. ;)