How to prevent HTML5 audio from predownload / streaming on load? - performance

I have a single page website which lists a collection of HTML5 audio players. The problem is the site has become slow because the following browsers start predownloading the content (mp3 and ogg)
Internet Explorer
Google Chrome
Firefox
Safari
(probably Opera)
I use the basic code to implement the players. Is there a way I can prevent the browsers from predownloading the audio files and only work when they click play?
<audio controls="controls" height="32" width="300" tabindex="0">
<source type="audio/mpeg" src="http://cdn.com/track.mp3"></source>
<source type="audio/ogg" src="http://cdn.com/track.ogg"></source>
</audio>

<audio controls="controls" preload="none">
<source src="song.ogg" type="audio/ogg" />
<source src="song.mp3" type="audio/mpeg" />
Your browser does not support the audio element.
</audio>
Note - preload="none" - can be used with VIDEO HTML5 and AUDIO HTML5.
The preload attribute is supported in all major browsers, except Internet Explorer and Opera.

MSIE still accounts for some 30% of all web traffic, so preload="none" is only a part solution. In a few pages where I had this problem, I add a small script to my page headers:
<script type="text/javascript">
function addAudio(t) {
var l=t.innerHTML.length;
var audioName=t.parentElement.id;
if( t.children.length==0) {
t.innerHTML=t.innerHTML+' <audio controls="controls"><source src="'+
audioName+'.ogg" type="audio/ogg" /><source src="'+
audioName+'.mp3" type="audio/mp3" /> No audio tag support</audio>';
}
}
</script>
and then use DHMTL to dynamically add the audio tag, for example:
<li id="2_Lesson_1_Hello"><span onclick="addAudio(this)">Γεια σας</span></li>
What this does is to define a list item containing a text span. When the person browsing clicks on the spanned text, the javascript fires and appends the <audio> tag. You could alternatively use the onmouseover attribute so that the audio tag is added on hover.
Add the preload attribute to the generated code if you wish. This is the simple approach, but if you are already using jQuery on your webpage, I note that this offers elegant alternatives.

Related

Can VideoJs display subtitles in chinese?

I just started using VideoJS. Quite impressed with the ease of initial setup of the Javascript library and the clean integration of videos into web pages.
However I encountered an issue of displaying subtitles in a foreign language (e.g. Chinese). The following is the code for embedding video
<video id="example_video_1" class="video-js vjs-default-skin" controls preload="none" width="640"
height="264" poster="Image/oceans-clip.png" data-setup="{}">
<source src="Video/oceans.mp4" type='video/mp4'/>
<track kind="captions" src="video-js/demo.captions_C.vtt" srclang="zh" label="Chinese" default></track>
<track kind="captions" src="video-js/demo.captions.vtt" srclang="en" label="English"></track>
<!-- Tracks need an ending tag thanks to IE9 -->
<track kind="subtitles" src="video-js/demo.captions_C.vtt" srclang="zh" label="Chinese" default></track>
<track kind="subtitles" src="video-js/demo.captions.vtt" srclang="en" label="English"></track>
<!-- Tracks need an ending tag thanks to IE9 -->
<p class="vjs-no-js">
To view this video please enable JavaScript, and consider upgrading to a web browser that
supports HTML5 video
</p>
</video>
The issues I have are:
1) The Chinese captions in "demo.captions_C.vtt" are not displayed, even if it is set as default. Instead, captions in "demo.captions.vtt is " are displayed.
2) I tried to select "Chinese" in the caption menu. There was no response and no change.
The "demo.captions_C.vtt" is provided below:
WEBVTT
00:00.000 --> 00:02.332
这是中文字幕
00:02.332 --> 00:10.947
海鸥的英雄礼赞音乐
00:10.947 --> 00:17.691
大海在咆哮
00:17.691 --> 00:50.279
鲸鱼在召唤
The only difference (apart from contents) I can tell between demo.captions.vtt and demo.captions_C.vtt is that the latter uses utf8 encoding, while the first one is using us-ascii.
I am wondering (a) if I have missed anything in the html code in integrating with the video tag in configuring tracks. Why only English is displayed. (b) has anyone successfully used vtt file that is using UTF-8 encoding? please let me know your procedures.
Here is a working example with Chinese subtitles:
https://jsfiddle.net/xrpnbwfz/1/
<video id="dotsub_example" class="video-js vjs-default-skin" width="640" height="264" poster="http://video-js.zencoder.com/oceans-clip.png" controls preload="auto" data-setup='[]'>
<source src="http://video-js.zencoder.com/oceans-clip.mp4" type='video/mp4' />
<source src="http://video-js.zencoder.com/oceans-clip.webm" type='video/webm; codecs="vp8, vorbis"' />
<source src="http://video-js.zencoder.com/oceans-clip.ogg" type='video/ogg; codecs="theora, vorbis"' />
<track kind='captions' src='https://dotsub.com/media/5d5f008c-b5d5-466f-bb83-2b3cfa997992/c/chi_hans/vtt' srclang='zh' label='Chinese' default />
<track kind='captions' src='https://dotsub.com/media/5d5f008c-b5d5-466f-bb83-2b3cfa997992/c/eng/vtt' srclang='en' label='English' />
<track kind='captions' src='https://dotsub.com/media/5d5f008c-b5d5-466f-bb83-2b3cfa997992/c/spa/vtt' srclang='es' label='Spanish' />
<track kind='captions' src='https://dotsub.com/media/5d5f008c-b5d5-466f-bb83-2b3cfa997992/c/fre_ca/vtt' srclang='fr' label='French' />
</video>
If you are using the latest version 4.12, there are some known issues with captions/subtitles: https://github.com/videojs/video.js/issues/1888 https://github.com/videojs/video.js/issues/1904
https://github.com/videojs/video.js/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+caption

How to format the audio tag in Ruby Slim?

This is the HTML audio tag:
<audio controls autoplay loop muted>
<source src="horse.ogg" type="audio/ogg">
<source src="horse.mp3" type="audio/mpeg">
Your browser does not support the audio tag.
</audio>
How is this written in Slim code, including the control, auto play and loop attributes?
You can get the empty attributes with boolean attributes in Slim. I guess your HTML snippet translates to something like this:
audio controls=true autoplay=true loop=true muted=true
source src="horse.ogg" type="audio/ogg"
source src="horse.mp3" type="audio/mpeg"
| Your browser does not support the audio tag.
or shorter
audio(controls autoplay loop muted)
…
If you are using Rails, you can also use the audio_tag helper: https://apidock.com/rails/ActionView/Helpers/AssetTagHelper/audio_tag

Firefox and HTML5 video element – weird behaviour with flash fallback. Possible bugs in mediaelemnts.js

I'm working on embedding video to a web-page and am using mediaelements.js. I used following code from the mediaelements.js examples:
<video width="640" height="360" id="player2" poster="img/echo-hereweare.jpg" controls="controls" preload="none">
<source type="video/mp4" src="somevideo.mp4" />
<source type="video/webm" src="somevideo.webm" />
<source type="video/ogg" src="somevideo.ogv" />
<object width="640" height="360" type="application/x-shockwave-flash" data="flash/flashmediaelement.swf">
<param name="movie" value="flash/flashmediaelement.swf" />
<param name="flashvars" value="controls=true&file=somevideo.mp4" />
<img src="img/echo-hereweare.jpg" width="640" height="360" alt="Here we are"
title="No video playback capabilities" />
</object>
</video>
Then later on comes the script-code from the example:
<script>
$('audio,video').mediaelementplayer({
success: function(player, node) {
$('#' + node.id + '-mode').html('mode: ' + player.pluginType);
}
});
</script>
Then I discovered some strange thing in Firefox. When I'm using this code without starting mediaelements.js (not using the script above), firefox tries to play the .mp4, but naturally the console reports an error that the .mp4 format is not supported. So far so right.
In my understanding, the video element should now try the next format – .webm. Instead the flash fallback gets loaded (flashmediaelement.swf) (nothing gets played so far, as the play-button is not clicked yet, but still the .swf is loaded completely).
Then when I play the video the .webm gets downloaded and played – as expected.
When I use it with mediaelements.js an even weirder thing is happening – the .swf not only gets fully loaded once, but is requested at least one more time, without being downloaded (you see it in firebug's network tab – a yellow line with a busy spinner, as if it would load and load. But there is no error or any status code, nor a filesize displayed. It seems kind of like an "stuck" request. I would like to post a screenshot of it, but am not allowed at the moment.).
Additionaly I get the alert in Firebug's console, that the .webm cannot be decoded. The .webm-video starts loading but won't be loaded fully (status code 206). Then no video plays at all, because the .webm is tried to play but stops because of the decoding error. Shoudn't the flash fallback jump in then?
My questions are now:
Is this normal behaviour in Firefox for media elements – that the
fallback flashplayer gets loaded, even though it is not needed?
Why does it load two or three times in the second scenario
allthough it is not used either? I guess this is a bug in mediaelemnts.js
And third, why does firefox play the .webm without mediaelements.js
and doesn't with mediaelements.js? I guess another bug?
Thx for any help.
---NOTE---
I know I am using the .swf that comes with the mediaelements.js as a fallback solution, even when I am not using mediaelements.js. Does anyone know a simple lightweight flash video player? Or would I have to stick with flowplayer as the standard flash video player?
I can't have multiple versions of all my video files (there's lots and the whole site is downloaded) so I wrote this and it seems to work on current Safari, FireFox and Chrome -should work on everything else or add to the non MP4 list
<script type="text/javascript">
/*U might need to add other non-MP4 native browsers*/
if (navigator.userAgent.indexOf('Firefox'||'Chrome') != -1) {
document.write("<object type='application/x-shockwave-flash' data='player.swf' width='480' height='270'>
<param name='movie' value='player.swf' />
<param name='allowFullScreen' value='true'/>
<param name='wmode' value='transparent' />
<param name='flashVars' value='controlbar=over&file=video/sequence03.mp4' />
<span title='No video playback capabilities, please download the video below'>
<a href='video/sequence03.mp4'>Sequence 03</a></span></object>")
}
else {
document.write("<video width='480' height='270' controls><source src='video/sequence03.mp4' type='video/mp4' /></video>")
}
</script>

how to call play, pause on vlcplugin in mozilla firefox

In html page,I can embed vlcplugin as mentioned in videolan page, and get the reference to plugin using jquery.
But it seems that the play(), pause() methods are supported only by vlcplugin upto 0.8.5. How do you play and pause a recent version of plugin?
<embed id="vlcp"
type="application/x-vlc-plugin"
pluginspage="http://www.videolan.org"
name="VLC"
autoplay="no"
loop="no"
volume="100"
width="640"
height="480"
target="test.flv">
</embed>
<a id="playbutton" href="#">Play</a>
<a id="pausebutton" href="#">Pause</a>
I can get reference to the plugin as below
var player = document.getElementById("vlcp");
Now, what do I call to make the embedded plugin play the clip?
I am using firefox as browser, will embedding vlcplugin in html work in chrome ?
You have to use the Playlist object of your VLC player as showed here.
In your particular example, you didn't really create an actual playlist, but you implicitly added one item to it (your "test.flv").
Here's how you can now control your movie (regardless of whether it's Mozilla, Chrome, or IE) - code in CoffeeScript:
player = document.getElementById("vlcp")
if player and player.playlist
// you could also check whether the playlist isn't empty using
// player.playlist.items.count
playlist = player.playlist
// pick whichever action you need from below
playlist.play()
playlist.togglePause()
playlist.stop()
You can also check whether the player is currently playing or paused/stopped by using
// assuming you got the playlist like above
playlist.isPlaying
Here is a demo example:
<html>
<head><title>Demo of VLC mozilla plugin</title></head>
<body>
<h1>Demo of VLC mozilla plugin - Example 1</h1>
<embed type="application/x-vlc-plugin"
name="video1"
autoplay="no" loop="yes" width="400" height="300"
target="http://server.example.org/video1.vob" />
<br />
<a href="javascript:;" onclick='document.video1.play()'>Play video1</a>
<a href="javascript:;" onclick='document.video1.pause()'>Pause video1</a>
<a href="javascript:;" onclick='document.video1.stop()'>Stop video1</a>
<a href="javascript:;" onclick='document.video1.fullscreen()'>Fullscreen</a>
</body>
</html>
Other useful sources/links:
http://www.videolan.org/doc/play-howto/en/ch04.html
http://www.w3.org/2010/05/video/mediaevents.html

replace image with a video embeded

Hi I'm trying to modify a web page so that it loads faster.
Since I have some videos embeded (blip.tv but can change it to youtube if it helps) I was wondering if you could load an image where the video should be and on click replace the image with the video and start playing (without reloading the whole page).
I think I've seen this before, but can't find it anywhere anymore!
right now the code to embed I use is:
<object data="http://blip.tv/play/gYMo_vAmAA" type="application/x-shockwave-flash" height="500" width="600"><param name="src" value="http://blip.tv/play/gYMo_vAmAA"><param name="allowfullscreen" value="true"></object>
Thanks
Quick and dirty: you could just set the embed code as a global variable somewhere:
<script type="text/javascript">
var embedCode = '<object data="http://blip.tv/play/gYMo_vAmAA" type="application/x-shockwave-flash" height="500" width="600"><param name="src" value="http://blip.tv/play/gYMo_vAmAA"><param name="allowfullscreen" value="true"></object>'
</script>
Then put the image in a container div and replace the container's innerHTML onclick:
<div id="videocontainer">
<img src="yourimage.jpg" onclick="document.getElementById('videocontainer').innerHTML = embedCode;" height="500" width="600" />
</div>
There's a Google code project called SWFObject, which is perfect for what you need. It's a cross-browser javascript library for loading flash - and you could use it to replace your image with the flash video when someone clicks on the image, for example.

Resources