locked
Playing Media as a Background Process

    Question

  • I have a media player which leverages content from the external site YouTube. The content is protected by being loaded into an iFrame which my application cannot interrogate. I therefore cannot create my own video tag with the attribute which forces the playing media to persist in the background.

    Other than playing another media element silently in the background i cannot think of another way to solve this.

    @mr_switch

    Friday, September 16, 2011 10:00 PM

Answers

  • It suffices to have a single audio or video tag with msAudioCategory="media".  Unless and until YouTube adds an msAudioCategory="media" tag on their content that you're <iframe>-ing in, you could have an additional audio tag in your app which had the tag (but didn't necessarily actually play any audible audio.)
    Matthew van Eerde
    Thursday, September 22, 2011 12:54 AM

All replies

  • Hi Andrew,

    I really don't understand what the question is.  Could you perhaps rephrase it?

    Jeff


    Jeff Sanders (MSFT)
    Friday, September 16, 2011 11:36 PM
    Moderator
  • Hi Jeff

    The new Windows 8 application and power management is to suspend applications if they are not in view. In the example http://code.msdn.microsoft.com/windowsapps/Playback-Manager-e6526e67 a new attribute is assigned to the HTML5 audio tag "msAudioCategory" = "media" to allow the application to continue to run in the background.

    If my app is using a plugin to generate this tag i don't have access to change it.

    So my question is, how do i make music player persist in the Background given this scenario?

    @mr_switch

     

    Saturday, September 17, 2011 6:37 PM
  • Tuesday, September 20, 2011 1:52 PM
    Moderator
  • It suffices to have a single audio or video tag with msAudioCategory="media".  Unless and until YouTube adds an msAudioCategory="media" tag on their content that you're <iframe>-ing in, you could have an additional audio tag in your app which had the tag (but didn't necessarily actually play any audible audio.)
    Matthew van Eerde
    Thursday, September 22, 2011 12:54 AM
  • Excellent idea Matthew, thanks for chiming in!

    -Jeff


    Jeff Sanders (MSFT)
    Thursday, September 22, 2011 12:32 PM
    Moderator
  • [UPDATE]

    It looks like we have changed this in CP for the MediaElement. We now need to use "BackgroundCapableMedia"

    http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.media.audiocategory.aspx

    -James


    Windows Media SDK Technologies - Microsoft Developer Services - http://blogs.msdn.com/mediasdkstuff/

    Tuesday, March 6, 2012 2:17 AM
    Moderator
  • Hi jasanders,

    I wrote some code to test and found that, when background task was triggered, the audio could not be triggered as the MediaElement which is UI related could not be handled in background task code. There must be something wrong in my code, right?

    Thursday, April 26, 2012 6:59 AM
  • I am not sure what your code does, but have you followed the information here to enable Audio?

    http://msdn.microsoft.com/en-us/library/windows/apps/windows.applicationmodel.background.backgroundtaskbuilder.settrigger(v=VS.85).aspx

    -Jeff


    Jeff Sanders (MSFT)

    Thursday, April 26, 2012 4:05 PM
    Moderator
  • Hi jasanders,

    Thanks for your quick response. In my demo code, I scheduled a background task which is triggered by timeZone change system event. And In this background task I hope to play a audio file.

    Here is the code of register related:

    RegisterBackgroundTask(SampleBackgroundTaskEntryPoint,
                                       SampleBackgroundTaskName,
                                       new SystemTrigger(SystemTriggerType.TimeZoneChange, false),
                                       null);
    
    private void RegisterBackgroundTask(String taskEntryPoint, String name, IBackgroundTrigger trigger, IBackgroundCondition condition)
            {
                var builder = new BackgroundTaskBuilder();
    
                builder.Name = name;
                builder.TaskEntryPoint = taskEntryPoint;
                builder.SetTrigger(trigger);
    
                if (condition != null)
                {
                    builder.AddCondition(condition);
                }
    
                BackgroundTaskRegistration task = builder.Register();
    
                //AttachProgressAndCompletedHandlers(task);
    
                //UpdateBackgroundTaskUI(name, true);
            }

    As in background task, I hope to play a audio file, so I declared background task type as:

    <Extensions>
            <Extension Category="windows.backgroundTasks" EntryPoint="RemoteAlarmBackground.Alarm">
              <BackgroundTasks>
                <Task Type="audio" />
                <Task Type="systemEvent" />
              </BackgroundTasks>
            </Extension>
          </Extensions>

    I have followed the guidance of the link you suggested but don't know where to set audio source.

    It is ok for me to play a audio file without trigger as I could place a MediaElement on UI page and set the property as this:

    <MediaElement x:Name="media_element" HorizontalAlignment="Left" Height="30" Margin="330,181,0,0" VerticalAlignment="Top" Width="120" AudioCategory="BackgroundCapableMedia" AutoPlay="False" Source="Media/scream.mp3"/>

    But in this completely background task sample, how could I set the audio source as the background could not handle MediaElement which is UI related.

    Looking forward to your replay.

    Many thanks.

    Friday, April 27, 2012 3:11 AM
  • Take a look at this sample written by our friend BaBa

    Sample here http://babaandthepigman.wordpress.com/2012/03/17/metro-background-audio-c-consumer-preview/


    Windows Media SDK Technologies - Microsoft Developer Services - http://blogs.msdn.com/mediasdkstuff/

    Friday, April 27, 2012 11:32 PM
    Moderator
  • Hi James,

    I have the very same question with Lex.

    Yes, I had looked into this sample carefully and found there are two problems it can not handle.

    1. It handles "play", "pause" events in the code, but when you click the hardware volume button to raise volume up or down and click the "pause" button along with it, the music won't pause. (I downloaded the sample then run it, I'm sure it won't work.) 

    BTW, it could play background.

    2. In this sample, the music is triggered by ourselves, we place a MediaElement on the page of foreground then run it and the music plays immediately, then we turn to another app, the music won't stop, that's OK. But what if the event is triggered by a trigger such as "timeZone change". The background task for playing audio is not allowed to set a MediaElement into its page then handle it so I have no idea how to play it in this situation.

    Looking forward to you reply.

    Thanks.


    dollar zhang

    Saturday, April 28, 2012 1:48 AM
  • Hello Dollar,

    I'm not entirely sure why you are trying to play audio on the "timezone change" event. This does not really make any sense to me. Can you give me a bit of a more "real world" scenario? I would recommend that you use a toast to alert the user from a background event such as the timezone change rather than trying to play audio. Again if you can give me a specific reason why you want to play audio from a system message handler maybe I can give you a better way of doing things. 

    -James


    Windows Media SDK Technologies - Microsoft Developer Services - http://blogs.msdn.com/mediasdkstuff/


    Tuesday, May 1, 2012 10:41 PM
    Moderator
  • Hi James,

    It doesn't make any sense to play audio when timeZone change and obviously it will be more suitable to use toast notification here. As I am learning metro app developing and just curious if metro app has the ability to achieve this.  hehe


    dollar zhang

    Wednesday, May 2, 2012 3:22 AM