JavaScript implements a simple HTML5 video player

Source: Internet
Author: User
Tags bind

Web video audio players are not unfamiliar, in IE we can run ActiveX to embed Microsoft Media Player or other local players, of course, most of us are using Flash to make the player. In the rapid development of HTML5 today, let us try to use HTML5 to create a Web page player, after all, whether it is a PC or mobile devices, HTML5 is the trend of the future

Effect:

The code is simple.

Js

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5, 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 11 9 120 Define ("Html5_video_player", ['.. /avalon-min '], function (Avalon) {function formattime (seconds) {var seconds = math.round (seconds); var minutes = Math.flo or (SECONDS/60); seconds = math.floor (seconds% 60); minutes = (minutes >= 10)? Minutes: "0" + minutes; seconds = (seconds >= 10)? Seconds: "0" + seconds; Return minutes + ":" + seconds;} var playing=false,mute=false,vol=50,fs=false,active=false,inactivitytimeout=timer=null; Avalon.bind ($ (' control_btn '), ' click ', function () {if (!playing) {$ (' html5_video '). Play (); $ (' control_btn '). Classname= ' html5_video_pause_btn inline-block '; Playing=true; }else{$ (' html5_video '). Pause (); $ (' control_btn '). Classname= ' html5_video_play_btn inline-block '; playing=false;}}); Avalon.bind ($ (' Video_bar '), ' click ', Function (e) {var a= (e.clientx-$ (' Video_bar '). offsetleft)/$ (' Video_bar '). offsetwidth; $ (' Html5_video '). CurrentTime =a.tofixed (2) *$ (' Html5_video '). Duration; }); Avalon.bind ($ (' Vol_bar '), ' click ', Function (e) {var a= (' Vol_bar ').offsetLeft-8)/$ (' Vol_bar '). offsetwidth; vol=$ (' Html5_video '). Volume =a; $ (' Vol_value '). style.width=a*100+ '% '; }); Avalon.bind ($ (' Mute_icon '), ' click ', function () {if (!mute) {$ (' html5_video '). volume=0; $ (' Mute_icon '). Classname= ' Html5_video_mute1 '; Mute=true; }else{$ (' Html5_video '). Volume=vol $ (' Mute_icon '). Classname= ' Html5_video_mute '; mute=false;}}); Avalon.bind ($ (' Html5_video '), ' Loadedmetadata ', function () {$ (' html5_video_duration '). Innerhtml=formattime ($ (' Html5_video '). Duration); $ (' Html5_video '). volume=0; }); Avalon.bind ($ (' Html5_video '), ' timeupdate ', function () {$ (' html5_play_time '). Innerhtml=formattime ($ (' Html5_video ') ). CurrentTime); $ (' Video_progress_bar '). style.left=$ (' Html5_video '). currenttime/$ (' Html5_video '). duration*100+ '% '; }); Avalon.bind ($ (' Html5_video_fullscreen '), ' click ', Function (e) {if (!FS) {Toggle_fullscreen ();} else{Exit_fullscreen ();}); Document.onmozfullscreenchange = function () {if ($ (' Html5_video '). ClientWidth +2!= Document.documentElement.clientWidth) {exit_fullscreen (); } }; Document.onwebkitfullscreenchange = function () {if ($ (' Html5_video '). clientwidth!= Document.documentElement.clientWidth) {Exit_fullscreen ();}}; function Exit_fullscreen () {$ (' html5_video '). Classname= '; fs = false; active=false; $ (' Video_control '). Classname= '; if (document.exitfullscreen) {Document.exitfullscreen ();} else if (Document.webkitcancelfullscreen) { Document.webkitcancelfullscreen (); else if (document.mozcancelfullscreen) {Document.mozcancelfullscreen ();}} function Toggle_fullscreen () {$ (' html5_video '). Classname= ' Video_fs '; fs = true; $ (' Video_control '). Classname= ' Fullscreen '; var elem=$ (' Html5_video '); if (elem.msrequestfullscreen) {Elem.msrequestfullscreen ();} else if (Elem.mozrequestfullscreen) { Elem.mozrequestfullscreen (); else if (elem.webkitrequestfullscreen) {Elem.webkitrequestfullscreen ();}} function updatebuffered () {var v = $ (' html5_video '); var r = v.buffered; if (r) {for (var i=0; i<r.length; i++) {var Start = R.staRT (i); var end = R.end (i); } $ (' Video_buffer_bar '). style.width=end/$ (' Html5_video '). duration*100+ '% '; } setinterval (updatebuffered,500); Function B () {if (active) {$ (' Video_control '). style.display= ' none '; active=false; Console.log (active);} avalon.bind ( $ (' Html5_video '), ' MouseMove ', function (e) {if (fs) {cleartimeout (inactivitytimeout); active=true; $ (' Video_control ') . style.display= ' block '; InactivityTimeout = settimeout (b, 5000); } }); });

Html

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The

Css

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5, 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 11 9 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145-146 @CHARSET "UTF-8";   #wrap_html5_video {padding:10px; width:360px}   #vol_bar, #video_bar, #vol_value {height:9px; background- Color: #999999; }   #vol_bar {width:100px; Cursor:pointer}   #vol_value {background-color: #179df7; width:50%;}   # html5_video {display:block; border:1px solid #c0deed;}   #video_buffer_bar {background-color: #179DF7; width:0; }   #video_progress_bar, #video_buffer_bar {position:absolute; height:100%}   #video_progress_bar {Backgro Und-color: #0066FF; width:2px; left:0; }   HTML5_VIDEO_PAUSE_BTN,.HTML5_VIDEO_PLAY_BTN {width:40px; height:40px; cursor:pointer;}   Html5_video_ play_btn {background:url ("http://localhost/twitter/images/html5_video.jpg") 0 0 no-repeat;   Html5_video_play _btn:hover {background:url ("http://localhost/twitter/images/html5_video.jpg") -41px 0 no-repeat;}   html5_ video_pause_btn {background:url ("http://localhost/twitter/images/html5_video.jPG ") 0-42px no-repeat; }   Html5_video_pause_btn:hover {background:url ("http://localhost/twitter/images/html5_video.jpg") -41px- 42PX no-repeat; }   #play_control A, #vol_bar {vertical-align:middle;}   #html5_video_fullscreen {width:25px; background:ur L ("http://localhost/twitter/images/html5_video.jpg") 0-310px no-repeat; height:18px; }   #play_control #html5_video_time {font-size:14px}   #play_control Li, #play_control ul {font-size:0;} & nbsp #play_control li:last-child {position:absolute right:0;}   html5_video_mute1 {background:url ("Http://localhos T/twitter/images/html5_video.jpg ") no-repeat scroll-79px-170px rgba (0, 0, 0, 0); }   Html5_video_mute {background:url ("http://localhost/twitter/images/html5_video.jpg") no-repeat scroll 0- 170px rgba (0, 0, 0, 0); }   #mute_icon {cursor:pointer; display:inline-block; height:15px; width:18px;}   Html5_video_mute:hover {Background:url ("Http://localhost/twitteR/images/html5_video.jpg ") -19px-170px no-repeat; }   #play_control li {height:40px; vertical-align:top margin:0 5px;}   #play_control Li:after {display:i Nline-block; width:0; height:100%; Vertical-align:middle; Content: '; }   #play_control, #video_bar, #vol_bar {position:relative}   body. fullscreen {position:fixed; left:0; bot tom:0; width:100%; Overflow:hidden; z-index:2147483647; Background-color: #fff; }   Video::-webkit-media-controls {Display:none!important}

        Note : more Wonderful tutorials please focus on the triple graphics and text tutorial channels,

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.