Quickstart: adding audio to an app (HTML)

This topic explains the audio streaming categories and shows how to use the <audio> tag to add audio streaming capabilities to your app.

Windows 8 has provided a platform that makes it relatively simple to use HTML5 to add audio streaming capabilities to a Windows Store app.

The audio tag has the following attributes:

AttributeAllowed valuesDescription
autoplayautoplaySpecifies that the audio will start playing as soon as it is ready
controlscontrolsSpecifies that audio controls should be displayed (such as a play/pause button etc).
looploopSpecifies that the audio will start over again, every time it is finished
preload

auto

metadata

none

Specifies if and how the author thinks the audio should be loaded when the page loads
src<file path>Specifies the path to the audio file

 

Microsoft provides an additional attribute, msAudioCategory, that you can use to enhance the behavior of your audio-aware app. The following table shows the allowed values of this attribute, and brief descriptions of what they do.

Stream categoryDescriptionBackground Capable?
AlertLooping or longer running alert sounds:
  • Alarms
  • Ring tones
  • Ringing notification
  • Sounds that need to attenuate existing audio
No
BackgroundCapableMediaFor audio that needs to continue playing in the background. Examples include the following local media playback scenarios:
  • Local playlist
  • Streaming radio
  • Streaming playlist
  • Music videos
  • Streaming audio/radio, YouTube, Netflix, etc.
Yes
CommunicationsFor streaming communication audio such as the following:
  • VOIP
  • Real time chat or other type of phone call
Yes
ForeGroundOnlyMediaGames or other sounds designed only to work in the foreground, but will mute existing background media sounds.
  • Game audio needed for the game experience (dancing games, music games)
  • Feature films (designed to pause when they go to the background)
No
GameEffectsGame sound effects designed to mix with existing audio
  • Characters talking
  • All non-music sounds
No
GameMediaBackground music played by a gameNo
SoundEffectsGame or other sound effects designed to mix with existing audio:
  • Characters talking
  • Beeps, dings, brief sounds
No
OtherDefault audio type, and recommended for all audio media that does not need to continue playing in the background.No

 

Objective: To add audio capabilities to a Windows Store app using the simplest method

Prerequisites

You must be familiar with HTML, JavaScript, Windows events and event handling.

You should have a media player installed can play MPEG-Layer 3 (MP3) or other digital music files.

Time to complete: 15 minutes.

Instructions

Adding audio with the <audio> tag

When you add the <audio> tag, you must use the "controls" attribute to indicate that you want the controls (the buttons) to be displayed. The following step shows how to do this.

  • Copy and paste the following code into the location in your HTML file where you want the audio controls to be displayed:
    
    // Adding the <audio> tag to your app
    <audio controls="controls"> 
    <source src="song.mp3"/> 
    </audio>
    
    

Adding the msAudioCategory attribute

When you add the msAudioCategory attribute to your <audio> tag, you provide more functionality. The msAudioCategory attribute will associate certain enhanced behaviors with your audio tag, and that will improve the user experience for your app. The following step shows how to add the msAudioCategory attribute.

  • Add the msAudioCategory attribute within the <audio> tag as shown:
    
    // Enhancing behavior of audio tag
    // with the msAudioCategory attribute
    <audio msAudioCategory="BackgroundCapableMedia" controls="controls"> 
    <source src="song.mp3"/> 
    </audio>
    
    

Summary and next steps

It is important to think very carefully about the category that you select for your stream, because your app will behave differently in each case.

The next topic How to configure keys for media controls, shows you how to add and configure media buttons for your Windows Store app. You can then use these buttons to play, pause, stop or even fast forward an audio stream.

Related topics

How to configure keys for media controls
Configure keys for media sample
Playback manager sample

 

 

Show:
© 2015 Microsoft