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.

function nextHandler () {<br />

curSong++;<br />

//Enable previous button if we have at least one previous track<br />

if (curSong > 0) {<br />

mediaControl.addEventListener("previoustrackpressed", prevHandler);<br />

}<br />

if (curSong < playlist.length) {<br />

//playlist[curSong] should already be loaded<br />

playCurrent();<br />

//Set up the next preload<br />

var nextTrack = curSong + 1;<br />

}<br />

}<br />

if (nextTrack < playlist.length) {<br />

preload.src = playlist[nextTrack];<br />

} else {<br />

preload.src = null;<br />

mediaControl.removeEventListener("nexttrackpressed", nextHandler);<br />

}<br />

function prevHandler() {<br />

//If we're already playing the last song, add the next button handler again<br />

if (curSong == playlist.length - 1) {<br />

mediaControl.addEventListener("nexttrackpressed", nextHandler);<br />

}<br />

curSong--;<br />

if (curSong == 0) {<br />

mediaControl.removeEventListener("previoustrackpressed", prevHandler);<br />

}<br />

}<br />

playCurrent();<br />

preload.src = playlist[curSong + 1]; //This should always work<br />

function playCurrent() {<br />

audio1.src = playlist[curSong];<br />

audio1.play();<br />

mediaControl.trackName = "Segment " + (curSong + 1);<br />

}<br />

When playing sequential tracks like this from an app written in JavaScript and <strong>HTML</strong>, you might<br />

notice brief gaps between the tracks, especially if the first track flows directly into the second. This is a<br />

present limitation of the platform given the layers that exist between the <strong>HTML</strong> audio element and the<br />

low-level XAudio2 APIs that are ultimately doing the real work. You can mitigate the effects to some<br />

extent—for example, you can crossfade the two tracks or crossfade a third overlay track that contains a<br />

little of the first and a little of the second track. You can also use a negative time offset to start playing<br />

the next track slightly before the previous one ends. But if want you a truly seamless transition, you’ll<br />

430

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

Saved successfully!

Ooh no, something went wrong!