23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

It’s also helpful to understand that video and audio are closely related, since they’re part of the same<br />

spec. In fact, if you want to just play the audio portion of a video, you can use the Audio object in<br />

JavaScript:<br />

//Play just the audio of a video<br />

var movieAudio = new Audio("http://www.kraigbrockschmidt.com/downloads/media/ModelRocket1.mp4");<br />

movieAudio.load();<br />

movieAudio.play();<br />

For any given video element, you can set the width and height to control the playback size (as to<br />

100% for full-screen). This is important when your app switches between view states, and you’ll likely<br />

have <strong>CSS</strong> styles for video elements in your various media queries. Also, if you have a control to play full<br />

screen, simply make the video the size of the viewport (after also calling<br />

Windows.UI.ViewManage-ment.ApplicationView.tryUnsnap if you’re in the snapped view). In<br />

addition, when you create a video element with the controls attribute, it will automatically have a<br />

full-screen control on the far right that does exactly what you expect within a Windows Store app:<br />

In short, you don’t need to do anything special to make this work. When the video is full screen, a<br />

similar button (or the ESC key) returns to the normal app view.<br />

Note In case you’re wondering, the audio and video elements don’t provide any <strong>CSS</strong> pseudo-selectors<br />

for styling the controls bar. As my son’s preschool teacher would say (in reference to handing out<br />

popsicles, but it works here too), “You get what you get and you don’t throw a fit and you’re happy<br />

with it.” If you’d like to do something different with these controls, you’ll need to turn off the defaults<br />

and provide controls of your own that would call the element methods appropriately.<br />

When implementing your own controls, be sure to set a timeout to make the controls disappear (either<br />

hiding them or changing the z-index) when they’re not being used. This is especially important if you<br />

have a full-screen button for video like the built-in controls, where you would basically resize the<br />

element to match the screen dimensions. When you do this, Windows will automatically detect this<br />

full-screen video state and do some performance optimizations, but not if any other element is front of<br />

the video. It’s also a good idea to disable any animations you might be running and disable<br />

unnecessary background processes like web workers.<br />

You can use the various events of the video element to know when the video is played and paused<br />

through the controls, among other things (though there is not an event for going full-screen), but you<br />

should also respond appropriately when hardware buttons for media control are used. For this purpose,<br />

listen for events coming from the Windows.Media.MediaControl object, such as playpressed,<br />

pausepressed, and so on. (These are WinRT object events, so call removeEventListener as needed.)<br />

Refer to the Configure keys for media sample for a demonstration, but adding the listeners generally<br />

looks like this:<br />

417

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!