Setting msAudioCategory freezes the system RRS feed

  • Question

  • Hello guys,

    I've built a really simple app which reproduces all the mp3 files it finds in a folder picked by the user. The app implements the play to contract and I've decided it should also be able to play when it's the background, so I've decided to set the msAudioCategory attribute to the backgroundcapablemedia value.

    Unfortunately, when the user picks a folder and runs the code for loading the first music, the system gets to a halt state and I can't do anything else (need to hit the pysical reboot button). Here's the code I'm using in my app. Am I doing anything wrong? thanks.

    HTML file:

    <!DOCTYPE html>
        <meta charset="utf-8">

        <!-- WinJS references -->
        <link href="//Microsoft.WinJS.0.6/css/ui-ligth.css" rel="stylesheet">
        <script src="//Microsoft.WinJS.0.6/js/base.js"></script>
        <script src="//Microsoft.WinJS.0.6/js/ui.js"></script>

        <!-- leitormp3 references -->
        <link href="/css/default.css" rel="stylesheet">
        <script src="/js/default.js"></script>
        <h1>Reprodução de ficheiros MP3</h1>
        <div id="contentor">
            <audio id="audio" controls></audio>
            <div id="info"></div>
            <button>Abrir pasta</button>

    JavaScript File:

    // For an introduction to the Blank template, see the following documentation:
    // http://go.microsoft.com/fwlink/?LinkId=232509
    (function () {
        "use strict";

        var app = WinJS.Application;
        var audio = null, musicas = null, posAtual = -1, info = null;
        var playtoManager = null;
        app.onactivated = function (eventObject) {
            playtoManager = Windows.Media.PlayTo.PlayToManager.getForCurrentView();
            playtoManager.addEventListener("sourcerequested", tipoConteudoReproduzido)
            info = document.getElementById("info");
            audio = document.getElementById("audio");
            audio.setAttribute("msAudioCategory", "backgroundcapablemedia");
            audio.addEventListener("mediafocuslost", perdeuFoco);
            audio.addEventListener("mediafocusreceived", ganhouFoco);
            audio.addEventListener("ended", iniciaMusica);
                .listen("click", abreMp3);
        function perdeuFoco() {
        function ganhouFoco() {
        function tipoConteudoReproduzido(e) {
            var pedido = e.sourceRequest;
        var Pickers = Windows.Storage.Pickers
        function abreMp3() {
            var picker = new Pickers.FolderPicker();
            picker.suggestedStartLocation = Pickers.PickerLocationId.musicLibrary;
                .done(function (item) {

        function carregaMusicas(pasta) {
            if (!pasta) return;
            var ordem = Windows.Storage.Search.CommonFileQuery.orderByName;
            var tipos = [".mp3"];
            var opcoes = new Windows.Storage.Search.QueryOptions(ordem, tipos);
                .getFilesAsync().then(function (mp3s) {
                    musicas = mp3s;
                    posAtual = -1;
        function iniciaMusica(){
            if( !musicas ) return;
            posAtual = posAtual + 1;
            if(posAtual > musicas.length ){
                posAtual = 0;
            var musica = musicas[posAtual];
                .then( function(props){
                    info.innerHTML = props.trackNumber + "-" + 
                        props.title +" (" + 
                        new Date(props.duration).toLocaleTimeString() + 
                    audio.src = URL.createObjectURL(musica);
        app.oncheckpoint = function (eventObject) {
            // TODO: This application is about to be suspended. Save any state
            // that needs to persist across suspensions here. You might use the 
            // WinJS.Application.sessionState object, which is automatically
            // saved and restored across suspension. If you need to complete an
            // asynchronous operation before your application is suspended, call
            // eventObject.setPromise(). 



    <?xml version="1.0" encoding="utf-8"?>
    <Package xmlns="http://schemas.microsoft.com/appx/2010/manifest">
      <Identity Name="24ccc3aa-719c-4878-8292-32fb8d7cb7d0" Version="" Publisher="CN=luisabreu" />
        <Resource Language="x-generate" />
        <Application Id="App" StartPage="default.html">
          <VisualElements DisplayName="leitormp3" Logo="images\logo.png" SmallLogo="images\smalllogo.png" Description="leitormp3" ForegroundText="light" BackgroundColor="#000000">
            <DefaultTile ShowName="allLogos" />
            <SplashScreen Image="images\splashscreen.png" />
            <Extension Category="windows.backgroundTasks" StartPage="default.html">
                <Task Type="audio" />
        <Capability Name="internetClient" />

    Luis Abreu

    Sunday, May 13, 2012 8:21 PM


All replies

  • Found the problem: it seems like you need to handle the playpressed, stoppressed and  pausepressed events from MediaControl.

    Luis Abreu

    Sunday, May 13, 2012 8:53 PM
  • Btw, why do I need to handle these events? I mean, I've just set them up and added no code to them. I was under the impression that those events would only be useful when you needed to interact with those special keys which allows you to start/stop music without going to the app.

    can someone give more info about this topic?


    Luis Abreu

    Sunday, May 13, 2012 9:38 PM
  • And just some more info on this. It seems like freezing will happen whenever you miss one of the necessary steps. For instance, I've had the same problem in a different pc when I forgot to add the background audio task to the manifest of the app...

    Luis Abreu

    Monday, May 14, 2012 10:40 AM
  • So everything is OK when you use the correct syntax and manifest?

    Jeff Sanders (MSFT)

    Monday, May 14, 2012 3:34 PM
  • Yes your app must register for media transport controls when they are going to play in the background and you should implement them as follows:http://msdn.microsoft.com/en-us/library/windows/apps/hh452722.aspx


    Jeff Sanders (MSFT)

    Monday, May 14, 2012 3:48 PM
  • Hello Jeff.

    Yes, it does play. 

    I understand that failing one of the steps is enough for it not to work. However, can't really understand how missing an event which is only fired for hw buttons should freeze my system and force me to reboot it through the hw button...

    I'm going to read the white paper you've mentioned before complaining about the mediacontrol events requirement :)

    thanks again

    Luis Abreu

    Monday, May 14, 2012 9:23 PM
  • Hey Luis,

    Yes I agree it is bad (and I will try a repro and file a bug if it does repro) but you do have to play by the rules too :-)


    Jeff Sanders (MSFT)

    Tuesday, May 15, 2012 12:09 PM
  • yes, I know that.

    but it would really help if the quickstarts did mention it, right? ;)

    in order to reproduce it, open the sample which shows the background behavior, and, for instance, comment the mediacontrol events.  I've managed to reproduce it on my 2 machines by doing that.

    Luis Abreu

    Tuesday, May 15, 2012 8:59 PM