html5 about关键字,About HTML5 Audio and Video

About HTML5 Audio and Video

If you embed audio or video in your website, you should use HTML5.

HTML5 is the next major version of HTML, the primary standard that determines how web content interacts with browsers. HTML5 supports audio and video playback natively in the browser, without requiring a plug-in. With HTML5, you can add media to a webpage with just a line or two of code.

The HTML5 media elements provide simple fallback for browsers that still need to use plug-ins, so you can update your website to HTML5 today and still be compatible with older browsers.

When you use HTML5, you can create your own customized media controllers for rich interactivity using web-standard CSS and JavaScript.

The HTML5 and tags make it simple to add media to your website. Just include the or element, use the src attribute to identify the media source, and include the controls attribute.

There are no plug-ins to install or configure. The audio or video downloads and plays in your webpage with built-in controls.18b8f0d60014bb759b70137b61d91c02.png

In Safari, the built-in video controls include a play/pause button, volume control, and a time scrubber. In Safari 5.0 and later on the desktop and on iOS 4.2 on the iPad, the controls also include a full-screen playback toggle on the lower right. The controls automatically fade out when the video is playing and fade in when the user hovers over the video or touches it.

If you want to provide your own media controller on the desktop or iPad, just leave out the controls attribute. HTML5 media elements expose a full set of methods, properties, and events to JavaScript for interactivity, and because the media elements are HTML, they can be styled using CSS to create exactly the look and feel you want.

In Safari 5.1 and later, you can choose any HTML element and expand it to fill the screen, allowing you to use your own custom controls while playing video in full-screen mode.

At a Glance

Safari supports the and media elements on iOS 3.0 and later and in Safari 3.1 and later on the desktop (Mac OS X and Windows). Support for these media elements allows Safari and other HTML5-compliant browsers to play the indicated source media without using a plug-in.

To get the most out of HTML5 audio and video, you should first learn to create the HTML media elements, then learn how to control them using JavaScript, and finally learn to apply CSS styles to media elements and modify styles dynamically using JavaScript.Create the HTML5 Media Elements

To use HTML5 audio or video, start by creating an or element, specifying a source URL for the media, and including the controls attribute.

Add Optional Attributes

You can set additional attributes to tell Safari that the media should autoplay or loop, for example, or specify a video height and width. You set boolean attributes such as controls or autoplay by including or omitting them—no value is required.

controls autoplay height="480" width="640">

For more information, seeProvide Alternate Sources

Not all browsers can play all media sources. Some browsers are able to play MPEG-4 or MP3 files, while others play only files compressed using codecs such as Ogg Vorbis. Desktop computers can typically play media using a wider assortment of compressors than mobile devices. Safari supports streaming delivery using HTTP Live Streaming, while some other browsers support only HTTP download. To provide the best experience for everyone, you can provide multiple versions of your media. List the sources in order of preference using separate tags. The browser iterates through the list and plays the first source that it can.

You don’t have to rely on the file extension and delivery scheme to tell Safari about the media file. The tag accepts attributes for MIME type and codecs as well. For details, seeFall Back in Good Order

Browsers that don’t support HTML5 ignore the and tags, and HTML5-savvy browsers ignore anything between the opening and closing tags except tags, so it’s easy to specify fallback behavior for older browsers. Just put the fallback HTML between the opening and closing or tags (after any tags).

Your browser does not support HTML5 video.

Your fallback can be an tag for a browser that needs a plug-in to play your media, a redirect to another page, or a simple error message telling the user what the problem is.

For more information, including examples of how to use a plug-in as a fallback, seeTake Control Using JavaScript

HTML5 media elements expose methods, properties, and events to JavaScript. There are methods for playing, pausing, and changing the media source URL dynamically. There are properties—such as duration, volume, and playback rate—that you can read or set (some properties are read-only). In addition, there are DOM events that notify you, for example, when a media element is able to play through, begins to play, is paused by the user, or completes.

You can use JavaScript with HTML5 media elements to:Create your own interactive audio or video controller—for an example, see

Display a progress indicator that shows how much of the media has downloaded—for an example, see

Load another audio or video when the current one finishes playing—for an example, see

Slave multiple audio and/or video elements to a master controller to ensure your media elements are always synchronized—for an example, see

Test whether Safari can play the specified media type or file—for examples, seeSet the Style with CSS3

Relevant Chapter:

Because the and elements are standard HTML, you can customize them using CSS—set the background color, control opacity, add a reflection, move the element smoothly across the screen, or even rotate it in 3D. You can combine CSS with JavaScript to change media properties dynamically, in response to user input or movie events.

You can also change the CSS properties of other parts of your webpage in response to media events. For example, you could darken the background and reduce the opacity of the rest of the page—effectively “dimming the lights”—when a movie is playing, or highlight the title of the currently-playing song in a playlist.

Prerequisites

You should be familiar with HTML and JavaScript. Familiarity with CSS is helpful. To create image masks, you should be able to work with transparency (alpha channels).

See AlsoiOS Human Interface Guidelines—User interface guidelines for designing webpages and web applications for Safari on iOS.

Copyright © 2012 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2012-12-13

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值