Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Now that we have our newly encoded video, it's time to add it to our page using HTML5 markup. As we explore the markup necessary to use video with HTML5, we're also going to explore how to use fallback content to make our video more cross-browser compliant. So I have the trails.htm file opened from the 06_08 folder. I'm going to scroll down to where our video needs to be added into our code, which is around line 114 or so. We've got a nice little comment there that says place our video here. We'll just go ahead and remove that. Okay, so actually adding video to your page is extremely easy.
We're just going to use a video tag and inside our video tag, we're going to set a source for that. Our source for this one is going to be _video/explore_promo.ogv. So remember that's the video file that we encoded in our last exercise. We're going go ahead and give it a width of 420 and then give it a height of 236. That's going to be the height on that. I'm just going to close that and then close the video tag.
That's really all we need to do to place a video on a page. I'm going to go ahead and save this. I'm going to preview this using Firefox. So let's go ahead and preview that using Firefox. As you can see, our video would work just fine, but we don't have any controls. I can't play it. I can't stop it. I can't pause it, any of that stuff. So if you're looking for custom controls, you can use the HTML5 Video API to wire up your own, but if you're looking for just standard play, pause, seekbar controls, you can easily enable them yourself within the tag.
So I'm going to go back into my code. Inside the video tag, I'm just going to go ahead and type in controls. Again, this is a Boolean value and it just controls, just enables them. So I'm just going to type in controls. I'm also going to type in preload="false". I don't want it to preload the video before the user plays it. That might slow the rest of the page down. So I'm going to go ahead and save that. Now let's try this again. I'm going to test this in Firefox again. Scrolling down, now I have some controls. (Music Playing) (Male speaker: Beautiful scenery, plenty of places to ride.) Some audio controls, got a seekbar, working just fine. Cool! Now if I try the same exact video in Safari, eh nothing. This is because Safari doesn't support Ogg Theora file format.
So I'm going to back into my code. Now I'm going to change the file format from OGV to MP4. So if I save this and now test this in Safari - (Music Playing) The video works just fine. Okay, that's not a great solution having to keep going back and forth and switching file formats. So there's obviously a better solution for this. What we're going to use is inside the video tag, we can embed a source tag. The source tag allows us to define multiple video file formats in the order that we want them tried.
So the video player would first try to upload at the first resource. If that doesn't work, it will move on to the next one. So let's go into our code again and let's try that. So what I'm going to do is get rid of the source attribute inside the video tag. I'm just going to leave it with the width, height, and the controls and preload. Then inside the video tag itself, I'm going to create a source tag. For source, I'm type in for the first source, _video/explore_promo.mp4, and then I'm going to pass along a type for that.
The type of this is going to be video/mp4. Now if you're going to host these yourself, your server needs to understand that mime type. So again, you may need to go into your server and set those mime types. Now in order to save myself a little bit of typing, I'm simply going to copy this tag and paste it again and change mp4 to ogv and change again the file type to ogg. That's the mime type for that here. Now, you might be wondering well, is there a reason that you did this in the order that you did it? And actually there is.
The iPhone uses the first video source available and if it doesn't see one that it recognizes, it just sort of fails to implement the video. So there is a reason that the MP4 video is served first in this particular list. Okay, now I'm going to save this. I'll test this in Firefox first. (Music Playing) Video works fine. Now I'm going to back in. I'm going to test this in Safari. (Music Playing) Video works fine here as well. I could continue to test it in Chrome and it would work fine as well.
So our video is a little bit more cross-browser compliant. Now we can continue to provide fallback content. If we wanted to after the last source type for example, we could do an object tag that loads up some flash fallback content if it was unsuccessful loading the MP4 or the OGG file. So you can be a lot more specific with this if you want to. So I think you'll agree, even though we've had to deal with multiple codecs, the actual implementation wasn't that hard. Now keep in mind that this is just an overview of video deployment with HTML5. For a more thorough exploration of deploying HTML5 video, complete with code examples and detailed notes, check out Kroc Camen's fantastic article, "HTML5 - Video for Everybody." This has got some examples.
So this is something within the specification that you could deploy, if you felt it was right for your own sites and your own applications.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 104602 Viewers
56 Video lessons · 116518 Viewers
71 Video lessons · 85710 Viewers
131 Video lessons · 41016 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.