Playing Media as a Background Process
-
16 September 2011 22:00
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
Semua Balasan
-
16 September 2011 23:36Moderator
Hi Andrew,
I really don't understand what the question is. Could you perhaps rephrase it?
Jeff
Jeff Sanders (MSFT) -
17 September 2011 18:37
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
-
20 September 2011 13:52Moderator
I think the Background Tasks sample is what you want http://code.msdn.microsoft.com/windowsapps/Background-Task-Sample-9209ade9
You can set the type to audio in there: http://msdn.microsoft.com/en-us/library/windows/apps/windows.applicationmodel.background.backgroundtaskbuilder.settrigger(v=VS.85).aspx
Jeff Sanders (MSFT) -
22 September 2011 0:54
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- Disarankan sebagai Jawaban oleh Jeff SandersMicrosoft Employee, Moderator 22 September 2011 12:32
- Ditandai sebagai Jawaban oleh Jeff SandersMicrosoft Employee, Moderator 14 Oktober 2011 12:53
-
22 September 2011 12:32Moderator
Excellent idea Matthew, thanks for chiming in!
-Jeff
Jeff Sanders (MSFT) -
06 Maret 2012 2:17Moderator
[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/
-
26 April 2012 6:59
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?
-
26 April 2012 16:05Moderator
I am not sure what your code does, but have you followed the information here to enable Audio?
-Jeff
Jeff Sanders (MSFT)
-
27 April 2012 3:11
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.
-
27 April 2012 23:32Moderator
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/
-
28 April 2012 1:48
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
-
01 Mei 2012 22:41Moderator
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/
- Diedit oleh James Dailey - MSFTMicrosoft Employee, Moderator 01 Mei 2012 22:41
- Disarankan sebagai Jawaban oleh James Dailey - MSFTMicrosoft Employee, Moderator 01 Mei 2012 22:41
-
02 Mei 2012 3:22
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