javascript+html setting up a video preview

Source: Internet
Author: User

The first type:

Set the Video property poster

poster= "Img/poster.png">   <source src= "Http://www.huazuo.com/video/brand.mp4" type= "Video/mp4" />
</video>

The second type:

Set video first frame as preview

HTML code: (Adjusts the left and top of the output, covering the video)

<div style = "Position:relative" >
<video id = "video" class= "Videocontent" controls> <source src= "Video/brand.mp4" type= "Video/mp4"/> </video>
<div id = "Output" style = "position:absolute;left:0;left:0" >
</div>
</div>

 javascript Code:

var video, Output;var scale = 0.6;var initialize = function () {output = doc    Ument.getelementbyid ("Output");    Video = document.getElementById ("video"); Video.addeventlistener ('  Canvas.width = video. Videowidth *  scale; 
canvas.height = video . videoheight* scale ;
Canvas.getcontext (' 2d '). DrawImage (video, 0, 0, canvas.width, canvas.height);
var img = document.createelement ("img");
Img.src = Canvas.todataurl ("Image/png");
Output.appendchild (IMG); }; Initialize ();

Output will display the first frame after the video is loaded, and the output will be hidden when playing

Problem:

Canvas drawing, due to the security of the browser, if the use of canvas drawing in the process of using the image resources of the other, then the Todataurl () will throw a security exception:

Uncaught securityerror:failed to execute ' todataurl ' in ' htmlcanvaselement ': Tainted canvases May is not exported

Workaround:

1. Place the video under the current domain.

2, access to the server allows, the use of resources across domains, that is, set the Cors cross-domain configuration, access-control-allow-origin.

Then, when the client accesses the picture resource, it adds

Img.setattribute (' Crossorigin ', ' Anonymous ');



javascript+html setting up a video preview

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.