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.

Transcoding happens either from one StorageFile to another or one RandomAccessStream to<br />

another, and in each case happens according to a MediaEncodingProfile. To set up a transcoding<br />

operation you call the MediaTranscoder prepareFileTranscodeAsync or prepareStream-<br />

TranscodeAsync method, which returns back a PrepareTranscodeResult object. This represents the<br />

operation that’s ready to go, but it won’t happen until you call that result’s transcodeAsync method. In<br />

JavaScript, each result is a promise, allowing you to provide completed and progress handlers for a<br />

single operation but also allowing you to combine operations with WinJS.Promise.join. This allows<br />

them to be set up and started later, which is useful for batch processing and doing automatic uploads to<br />

a service like YouTube while you’re sleeping! (And at times like these I’ve actually pulled ice packs from<br />

my freezer and placed them under my laptop as a poor-man’s cooling system….)<br />

The Transcoding media sample provides us with a couple of transcoding scenarios. In Scenario 1<br />

(js/presets.js) we can pick a video file, pick a target format, select a transcoding profile, and turn the<br />

machine loose to do the job (with progress being reported), as shown in Figure 10-7.<br />

FIGURE 10-7 The Transcoding media sample cranking away on a video of my then two-year-old son discovering the<br />

joys of a tape measure.<br />

The code that’s executed when you press the Transcode button is as follows (some bits omitted; this<br />

sample happens to use nested promises, which again isn’t recommended for proper error handling<br />

unless you want, as this code would show, to eat any exceptions that occur prior to the transcode-Async<br />

call):<br />

function onTranscode() {<br />

// Create transcode object.<br />

var transcoder = null;<br />

transcoder = new Windows.Media.Transcoding.MediaTranscoder();<br />

// Get transcode profile.<br />

449

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

Saved successfully!

Ooh no, something went wrong!