The following javascript function serves and autoplays a audio file (via a HTML 5 audio tag), cuts the mp3 playback at 6 seconds and loops + autoplays the audio from the beginning.
javascript:
function updateaudio() {
var a_str = '<audio autoplay source src="audio/coolsound.mp3" type="audio/mpeg"></audio>';
document.getElementById('audio_span').innerHTML = a_str;
}
setInterval(updateaudio, 6000)
html:
<div><span id="audio_span"></span><script src="js/audio.js"></script></div>
Is there any using the html5 audio attribute to set autoplay playback to once off i.e. non-looping? Alternatively, is there another way to achieve this (via either javascript or html5)? Upon the audio event ending, I would like to set a flag and stop any playback.
Don't use setInterval() if you don't want a loop.
If you simply want to pause the playback after six seconds do:
setTimeout( function () {
document.getElementById('audio_span').pause();
}, 6000)):
Related
So, I need to autoplay couple of videos with same class on page load. That is fine.
Then, when videos starts playing, when they reach for 1 second, I need them to stop automatically. The other part is what I need.
<video playsinline muted loop class="video">
<source src="video-source">
</video>
let videos = document.querySelectorAll('.video');
window.addEventListener('DOMContentLoaded', () => {
videos.forEach(el => {
el.play();
});
});
I have tried doing something like this, get currentTime and then make an IF statement but no luck. It always console loggs 0.
function myFunction() {
videos.forEach(el => {
console.log(el.currentTime);
if(el.currentTime = 5) {
el.pause();
}
});
}
Thanks!
The issue you will have with your current attempt is that it only runs once, you need to have a way of checking your logic at intervals so that you can continuously check the time lapsed.
You could potentially look at using the timeupdate video event, this runs repeatedly whilst a video is playing.
In a project I am currently working on, I have a VideoJS player which I want to change the source of when the video ends.
Using the VideoJS API, I am able to get a Javascript script to run when the player has reached an 'ended' state, but I can't find any code in their documentation or on StackOverflow explaining how to do this.
HTML
<video
id="my_video_1"
style="display:block;"
class="video-js vjs-default-skin vjs-nofull vjs-big-play-centered"
controls autoplay preload="none"
width="800px"
poster='res/img/poster.jpg'
data-setup='{ "fluid": true, "sources": [{ "type": "video/mp4", "src": "res/vid/vid1.mp4"}] }'
>
</video>
Is there any way of dynamically changing the src to a new file when the video ends?
The VideoJS has an API of 'player.on' which is then followed by whichever listener is needed - in this case, ('ended').
Where I was falling down was that I was trying to use document.GetElementByID and update the src that way.
By changing the VideoJS code in the HTML to the style above, I was able to run the below listening event to update the src on ended and load a new video.
The next issue I had was the video was looping. When the video ended, the same function was called infinitely.
To counter this, I added a Boolean value of 'executed' which changes to True on the first play.
<script>
var video = videojs('my_video_1').ready(function(){
var player = this;
var executed = false;
player.on('ended', function() {
if (!executed) {
player.src({"type":"video/mp4", "src":"res/vid/vid2.mp4"});
player.play();
executed = true
}
});
});
</script>
The first video now plays, followed by the second one when the first has finished without any user interaction.
I hope this helps anyone else who may experience a similar issue.
I am using the audio HTML element to play audio in a webpage, it looks like this:
<audio src="http://www.mydomain/someaudio.mp3" preload="none" />
This causes the problem that the length of the audio track is 00:00 until the user clicks the play button.
Unfortunately I can't use the metadata preload value because the server the file is being
served from does not support it.
Is it possible to specify the length of the audio track in the HTML instead or via JavaScript?
Please try this
<script>
function myOnCanPlayFunction() {
console.log('Can play');
alert(document.getElementById('myaudio').duration);
}
function myOnCanPlayThroughFunction() {
console.log('Can play through');
alert(document.getElementById('myaudio').duration);
}
You check event occur in oncanplay or oncanplaythrough and duration property is length of the audio in seconds
Hope this help!
document.getElementById('audio_1').addEventListener('ended', function() {
this.play();
this.currentTime = 0;
}, false);
In this music does not start on page load, it repeats n number of times, once started..but not once page load
You never start the playback. You need to call .play() onload or use the autoplay attribute in your <audio> tag.
Oh, and you can achieve looping much easier without any JavaScript. Simply use the loop attribute in your <audio> tag.
I am attempting to build a video sequencer that is capable of playing back a list of video URLs seamlessly in series. There cannot be a gap between the videos; the playback needs to be as close to frame-accurate as possible.
Attempt #1
I used a fairly obvious and straightforward approach, similar to that mentioned in this thread.
HTML 5 video or audio playlist
The issue with this approach was that each time one video ended and the subsequent video was specified as the new source, that new video still needed to load, resulting in a potentially long gap. If it is possible to force all the videos to preload even before they are named in video.src, this approach could still potentially work.
Attempt #2
I rewrote the script so each video in the list would result in a separate video element being created, but not attached to the document (using createElement). Then as each video ended, I removed the previous node and appended the next element.
Code executed on page load:
for (i in timelinePlay)
if (timelinePlay[i] != null)
{
var element = document.createElement('video');
element.id = 'video1-' + (i);
element.src = timelinePlay[i]['URL'];
element.preload = 'load';
video1Array[i] = element;
}
Code for video 'ended' event:
videoElement.addEventListener("ended", function (event) {
if (this.currentTime > 0)
{
if (player.hasChildNodes())
while (player.childNodes.length > 0)
player.removeChild(player.firstChild);
player = document.getElementById('canvas-player');
player.appendChild(video1Array[timelineCurrent]);
nextVideo = document.getElementById('video1-' + timelineCurrent);
nextVideo.play();
timelineCurrent++;
}
}, false);
(Note that these code examples are partial and somewhat simplified. Also, I do realize that my use of Objects as Associative Arrays is not best practice)
The result of this modification was MUCH closer, because the videos were now loaded by the time they were required to play, but there was still a short and inconsistent gap between the videos.
Attempt #3
I replaced the 'ended' event listener with a 'timeupdate' listener, beginning as follows:
nextVideo.addEventListener("timeupdate", function (event)
{
if (this.currentTime > (this.duration - 0.1) && this.currentTime > 1)
{
this.removeEventListener("timeupdate", function () { return; }, false);
('this.currentTime > 1' is simply to ensure that the previous video actually plays)
My hope was that the gap between videos was close enough to being consistent that starting the next video an arbitrary 0.1 seconds before the previous video ended would correct the gap to an acceptable extent. The result was that the timing was indeed better, but it was skipping videos. I attribute the skipped videos to misfiring of the 'timeupdate' event, but I could be mistaken.
Other alternative options I had also considered:
SMIL Script (seems to be obsolete, and would likely have the same syncing issues anyway)
ffmpeg on the backend to concatenate the videos (my host will not allow shell scripts)
FYI I am developing for Safari 5.0.3 at the moment
I had a similar problem that I managed to solve now thanks to your hints. The result is a possibly dynamic list of video elements that may be played back one after another without gaps.
Instead of a native Video Tag I use jwplayer on several video elements.
On startup all elements begin to play one second, are paused and rewound to zero.
Then one by another is played and made visible with display: block.
<ul class="playlist">
<li>
<video class="playlist" id="vid01" poster="img/preview.jpg">
<source src="vid/v01.mp4" type="video/mp4">
</video>
</li>
<li>
<video class="playlist" id="vid02" poster="img/preview.jpg">
<source src="vid/v02.mp4" type="video/mp4">
</video>
</li>
<li>
<video class="playlist" id="vid03" poster="img/preview.jpg">
<source src="vid/v03.mp4" type="video/mp4">
</video>
</li>
<li>
<video class="playlist" id="vid04" poster="img/preview.jpg">
<source src="vid/v04.mp4" type="video/mp4">
</video>
</li>
</ul>
On added to stage, 'preload' the video one or two seconds:
(Excerpt)
var isPrebuffering = false,
isSeeking = false;
$player = jwplayer(videoId).setup({
width: '800px',
height: '450px',
autoplay: false,
modes: [
{ type: 'html5' }
],
events: {
onReady: function(e) {
prebuffer();
},
// There is a quirk in jwplayer that makes it impossible to pause at the
// new position directly after seeking. So we have to work around that.
onTime: function(e) {
if((e.position > 1.0 && isPrebuffering) || isSeeking) {
finishPrebuffering();
}
}
}
});
function prebuffer() {
isPrebuffering = true;
$player.setMute(true);
$player.play();
};
function finishPrebuffering() {
if(isPrebuffering) {
isPrebuffering = false;
isSeeking = true;
$player.seek(0);
}
if($player.getPosition() === 0) {
isSeeking = false;
$player.pause();
$player.setMute(false);
}
};
The mediaElement interface of HTML5 can be quite confusing sometimes between preload, load and buffered.
But have you tried to put an event on your video to detect for example 3 seconds before the end of your video ?
Then in ajax try to load the next video in a new div.
On the ended event of your video you could switch the div, so your next video will be enough loaded to be played.
And so on for the next videos.
http://www.w3.org/TR/html5/video.html#media-elements
You might be able to do so with http://popcornjs.org/. They provide js hooks, on a frame by frame level into the video using html5 video tag. There are a lot of events etc.