locked
Background Audio

    Question

  • My app (Modern Reader - a client for google reader) allows you to play Audio/Video.

    However when you alt tab away it stops playing the audio.

    I tried this:
    // Allow audio/video podcasts to run in the background.
    var audioTags = element.querySelectorAll('audio');
    for (var i = 0; i < audioTags.length; i++) {
    audioTags[i].msAudioCategory = "BackgroundCapableMedia";
    }
    var videoTags = element.querySelectorAll('video');
    for (var i = 0; i < videoTags.length; i++) {
    videoTags[i].msAudioCategory = "BackgroundCapableMedia";
    }

    But it still didn't fix the issue.
    Any ideas?

    Did I need that attribute to be set before adding the element to the DOM?
    It is set before the user can hit "play".

    Thanks

    Sunday, November 11, 2012 5:26 PM

Answers

All replies

  • Hi,

    First, please set "msAudioCategory""BackgroundCapableMedia".

    Second, add a "Background task" "Audio" in Declarations.

    And you may also refer to:http://code.msdn.microsoft.com/windowsapps/Playback-Manager-e6526e67 .


    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com
    Microsoft One Code Framework

    • Marked as answer by Song Tian Friday, November 16, 2012 9:06 AM
    • Unmarked as answer by Cobra Tap Friday, November 23, 2012 6:54 AM
    • Proposed as answer by Vinicius de Melo Rocha Friday, November 23, 2012 12:52 PM
    • Marked as answer by Cobra Tap Friday, November 23, 2012 8:46 PM
    Monday, November 12, 2012 8:59 AM
  • Also be sure to set the audio element's src value after setting msAudioCategory.
    • Marked as answer by Song Tian Friday, November 16, 2012 9:06 AM
    • Unmarked as answer by Cobra Tap Friday, November 23, 2012 6:54 AM
    • Proposed as answer by Vinicius de Melo Rocha Friday, November 23, 2012 12:52 PM
    • Marked as answer by Cobra Tap Friday, November 23, 2012 8:46 PM
    Monday, November 12, 2012 3:39 PM
  • Thanks guys. The sample was great and Kraig's tip was great as well.
    I was able to create a new app and get the background audio to work.
    PS I think you need these lines of code as well:

          var mediaControl = Windows.Media.MediaControl;
            mediaControl.addEventListener("soundlevelchanged", soundLevelChanged, false);
            // Add event listeners for the mandatory media commands.
            // These are necessary to play streams of type 'backgroundCapableMedia'
            mediaControl.addEventListener("playpausetogglepressed", playpause, false);
            mediaControl.addEventListener("playpressed", play, false);
            mediaControl.addEventListener("stoppressed", stop, false);
            mediaControl.addEventListener("pausepressed", pause, false);

    However in my real app it won't work.
    I did notice something strange.
    I get this error in Visual studio when I click Play and create the video/audio file:
    APPHOST9607: The app can't launch the URI at ms-appx://50712cobratap.modernreader/default.html because of this error: -2147024846.
    File: default.html

    Any ideas what that error is?

    Also on the Declarations page for Background Tasks I added audio and put "default.html" in "Start page" field.
    What does that really do?
    Does it actually try running that page again?
    I tried switching to some new test.html file but it didn't seem to help with the error or playback when alt tabbed.
    Friday, November 23, 2012 7:05 AM
  • Hi Cobra,

    I am sure there is no problem if you follow my suggestons. I am also create a sample on my side, and there is no problem.

    I am also suggest you reading the Playback Manager msAudioCategory sample carefully.

    Any more question, please feel free to reply.


    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com
    Microsoft One Code Framework

    Friday, November 23, 2012 7:10 AM
  • I have been studying my code around this area since the 12th when you gave me the sample.
    I know it works in the sample and in my blank app but it won't work in my real app.
    It must be in conflict with something else I am doing. But I have no idea what it is.
    Do you have any info about that error I pasted?

    Friday, November 23, 2012 9:33 AM
  • Hi,

    Do you  add a "Background task" "Audio" in Declarations?


    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com
    Microsoft One Code Framework

    Friday, November 23, 2012 9:38 AM
  • Yes.
    And when you do that it forces you to pick a "Start page". I used "default.html" which is my apps only page.
    Friday, November 23, 2012 10:05 AM
  • Yes.
    And when you do that it forces you to pick a "Start page".

    I used "default.html" which is my apps only page.

    Friday, November 23, 2012 10:05 AM
  • Hi,

    Then please share your project in skydirve. I will reproduce on my side.


    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com
    Microsoft One Code Framework

    Friday, November 23, 2012 10:08 AM
  • How do I only share it with you?
    I don't want everyone else to get my code.
    Friday, November 23, 2012 10:19 AM
  • Hi,

    Please send email to me.

    support-sotian@live.com


    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com
    Microsoft One Code Framework

    Friday, November 23, 2012 10:47 AM
  • Oh my god - I forgot to call the function that adds all the event listeners :-(

    Failure.

    Thanks guys

    Friday, November 23, 2012 8:46 PM