23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

You use the same BitmapPropertySet for any properties you might pass to an encoder’s bitmap-<br />

Properties.setPropertiesAsync call. Here’s we’re just using the same mechanism for encoder options.<br />

As for custom codecs, this simply means that the first argument to BitmapEncoder.createAsync (as<br />

well as BitmapDecoder.createAsync) is the GUID (a class identifier or CLSID) for that codec, the<br />

implementation of which must be provided by a DLL. Details on how to write one of these is provided in<br />

How to Write a WIC-Enabled Codec. The catch is that including custom image codecs in your package is<br />

not presently supported. If the codec is already on the system (that is, installed via the desktop), it will<br />

work. However, the Windows Store policies do not allow apps to be dependent on other apps, so it’s<br />

unlikely that you can even ship such an app unless it’s preinstalled on some specific OEM device and the<br />

DLL is part of the system image. (An app written in C++ can do more here, but that’s beyond the scope<br />

of this book.)<br />

In short, for apps written in JavaScript and <strong>HTML</strong>, you’re really limited, for all practical purposes, to<br />

image formats that are inherently supported in the system.<br />

Do note that these restrictions do not exist for custom audio and video codecs. The Media extensions<br />

sample shows how to do this with a custom video codec, as we’ll see in the next section.<br />

Manipulating Audio and Video<br />

As with images, if all we want to do is load the contents of a StorageFile into an audio or video<br />

element, we can just pass that StorageFile to URL.createObjectUrl and assign the result to a src<br />

attribute. Similarly, if we want to get at the raw data, we can just use the StorageFile.openAsync or<br />

openReadAsync methods to obtain a file stream.<br />

To be honest, opening the file is probably the farthest you’d ever go in JavaScript with raw audio or<br />

video, if even that. While chewing on an image is a marginally acceptable process in the JavaScript<br />

environment, churning on audio and especially video is really best done in a highly performant C++<br />

DLL. In fact, many third-party, platform-neutral C/C++ libraries for such manipulations are readily<br />

available that you should be able to directly incorporate into such a DLL. In this case you might as well<br />

just let the DLL open the file itself!<br />

That said, WinRT does provide for transcoding (converting) between different media formats and<br />

provides an extensibility model for custom codecs, effects, and scheme handlers. In fact, we’ve already<br />

seen how to apply custom video effects through the Media extensions sample, and the same DLLs can<br />

also be used within an encoding process, where all that the JavaScript code really does is glue the right<br />

components together (which it’s very good at doing). Let’s see how this works with transcoding video<br />

first and then with custom codecs.<br />

Transcoding<br />

Transcoding both audio and video is accomplished through the Windows.Media.Transcoding.-<br />

MediaTranscoder class, which supports output formats of mp3 and wma for audio, and mp4, wmv, and<br />

m4a for video. The transcoding process also allows you to apply effects and to trim start and end times.<br />

448

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

Saved successfully!

Ooh no, something went wrong!