Html5 custom player core code

Source: Internet
Author: User

Comments: HTML5 provides a new video tag. You can play video directly without programming. You only need to write a few lines of simple code to customize the html of the player webpage.

The Code is as follows:
<Body style = "background-color: # 8EEE5EE;">
<Section id = "skin">
<Video id = "myMovie" width = "640" height = "360">
<Source src = "videos/Introduction.mp4">
</Video>
<Nav>
<Div id = "buttons">
<Button type = "button" id = "playButton"> Play </button>
</Div>
<Div id = "defaultBar">
<Div id = "progressBar"> </div>
</Div>
<Div style = "clear: both"> </div>
</Nav>
</Section>
</Body>

Css style

The Code is as follows:
Body {
Text-align: center;
}
Header, section, footer, aside, nav, article, hgroup {
Display: block;
}
# Skin {
Width: 700px;
Margin: 10px auto;
Padding: 5px;
Background: red;
Border: 4px solid black;
Border-radius: 20px;
}
Nav {
Margin: 5px 0px;
}
# Buttons {
Float: left;
Width: 70px;
Height: 22px;
}
# DefaultBar {
Position: relative;
Float: left;
Width: 600px;
Height: 14px;
Padding: 4px;
Border: 1px solid black;
Background: yellow;
}
/* ProgressBar is inside defaultBar */
# ProgressBar {
Position: absolute;
Width: 0px;/* use javascript to control changes */
Height: 14px;/* the same height as defaultBar */
Background: blue;
}

Javascript code

The Code is as follows:
Function doFisrt ()
{
BarSize = 600; // do not use the px Unit or var. It is a global variable.
MyMovie = document. getElementById ('mymovi ');
PlayButton = document. getElementById ('playclick ');
Bar = document. getElementById ('defaultbar ');
ProgressBar = document. getElementById ('ssssbar ');
PlayButton. addEventListener ('click', playOrPause, false); // the third parameter is always false. Register the event handler for the bubbling phase.
Bar. addEventListener ('click', clickedBar, false );
}
// Control movie playback and stop
Function playOrPause (){
If (! MyMovie. paused &&! MyMovie. ended ){
MyMovie. pause ();
PlayButton. innerHTML = 'play ';
Window. clearInterval (updatedBar );
} Else {
MyMovie. play ();
PlayButton. innerHTML = 'pause ';
UpdatedBar = setInterval (update, 500 );
}
}
// Control the dynamic display of the progress bar
Function update (){
If (! MyMovie. ended ){
Var size = parseInt (myMovie. currentTime * barSize/myMovie. duration );
ProgressBar. style. width = size + 'px ';
} Else {
ProgressBar. style. width = '0px ';
PlayButton. innerHTML = 'play ';
Window. clearInterval (updatedBar );
}
}
// How to control the progress bar by clicking the mouse
Function clickedBar (e ){
If (! MyMovie. paused &&! MyMovie. ended ){
Var mouseX = e. pageX-bar.offsetLeft;
Var newtime = mouseX * myMovie. duration/barSize; // new starting time
MyMovie. currentTime = newtime;
ProgressBar. style. width = mouseX + 'px ';
Window. clearInterval (updatedBar );
}
}
Window. addEventListener ('load', doFisrt, false );

Good stuff. I picked up the code.

Related Article

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.