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.

function soundLevelChanged() {<br />

//Catch SoundLevel notifications and determine SoundLevel state.<br />

//If it's muted, we'll pause the player.<br />

var soundLevel = Windows.Media.MediaControl.soundLevel;<br />

//No actions are shown here, but the options are spelled out to show the enumeration<br />

switch (soundLevel) {<br />

case Windows.Media.SoundLevel.muted:<br />

break;<br />

case Windows.Media.SoundLevel.low:<br />

break;<br />

case Windows.Media.SoundLevel.full:<br />

break;<br />

}<br />

}<br />

appMuted();<br />

function appMuted() {<br />

if (audtag) {<br />

if (!audtag.paused) {<br />

audtag.pause();<br />

}<br />

}<br />

}<br />

Technically speaking, a handler for soundlevelchanged is not required here, but the other four are.<br />

Such a minimum implementation is part of the AudioPlayback example with this chapter, where the<br />

code also uses the MediaControl.isPlaying flag to set the play/pause button in the media control UI<br />

(see next section).<br />

A few additional notes about background audio:<br />

• The reason for distinct playpressed, pausepressed, and playpausepressed events is to<br />

support a variety of hardware where some devices have separate play and pause buttons<br />

and others have a single button for both.<br />

• If the audio is paused, a background audio app will be suspended like any other, but if<br />

the user presses a play button, the app will be resumed and audio will then continue<br />

playback.<br />

• The use of background audio is carefully evaluated with apps submitted to the Windows<br />

Store. If you attempt to play an inaudible track as a means to avoid being suspended,<br />

the app will fail Store certification.<br />

• A background audio app should be careful about how it uses the network for streaming<br />

media to support the low power state called connected standby. For details, refer to<br />

Writing a power savvy background media app.<br />

Now let’s see the other important reason why you must implement the media control events: the UI<br />

that Windows displays in response to hardware buttons.<br />

427

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

Saved successfully!

Ooh no, something went wrong!