wp 8.1 start music from a background task - c#

I'm developing a Windows Phone 8.1 app (non-silverlight).
What I'm able to do at the moment:
1-I'm able to activate a background task from the UI that reads the accelerometer readings even when the screen is locked or the app is closed using
DeviceUseTrigger
and
BackgroundTaskBuilder
2-I'm able to activate a background task from the UI that plays an audio file even when the screen is locked or the app is closed using the
BackgroundMediaPlayer.Current
Now what I need to do is to play an alarm from the first task even when there is no UI. In other words the first task should be able to Run the second one. Using the BackgroundMediaPlayer does not work anywhere beside the UI thread...
Any help or idea would be appreciated!!

Have you tried using the Windows.Media.Playback.MediaPlayer (MSDN Link) class by any chance? I'm not sure if that works in a background task but it might be worth a try.

Related

Communicate between two backgroundtasks in windows phone 8.1

I'm facing a problem with background tasks in Windows Phone 8.1.
I tried to create a new background task with Device User Trigger and Audio, but I got the error saying:
Package could not be registered.
I saw this:
Windows phone 8.1 deployment error when trying to use background task with audio and device use trigger in SO
Windows phone 8.1 deployment error when trying to use background task with audio and device use trigger in the MSFT Forum.
I tried the solution they pointed but without success.
Anyone knows how I can make 2 background tasks communicate?
What worked for me was this:
in one of the Background Task (the audio one) I created a Timer and checked if a LocalSettings value was or not true.
The other Background task was the one responsible to set it to true, so that what I want to do in the audio will happen as soon as I set the value to true.
I know that this is a "strange" way to do it, but it works ;)

Accessing BackgroundMediaPlayer from another task

I have an app which has two background tasks: a background audio task and a Bluetooth task. What I want to do is modify the state of the audio playback from within the code in the Bluetooth task without having to go through the foreground app. I tried putting this in the Bluetooth task hoping it'd work:
if (BackgroundMediaPlayer.Current.CurrentState == MediaPlayerState.Playing)
BackgroundMediaPlayer.Current.Pause();
But it turns out it doesn't work. Even though the background audio is playing, when I access its state from the Bluetooth task its value is equal to MediaPlayerState.Closed(). Anyone have any idea how I can go about doing this? I thought this'd work because BackgroundMediaPlayer is a global object which only has one instance in the entire phone, but clearly the .Current property is clearly somehow specific to each project. I noticed that that its of type MediaPlayer, so is there any way I could place the object in a global container that I can access from both tasks?
Thanks in advance
I only answer this in case someone like me needs to still develop for WP8.1.
The only way I can think of doing this is to have the two background tasks in the
same solution and use ApplicationData.Current.LocalSettings to save a value into settings, and pull the changes at an interval from the settings in the music player task.
Basically the background audio task keeps an eye on the settings to see if the Bluetooth background task changes them and then responds by pausing or playing the audio.

Windows phone 8.1 background timer to stop music

Hello as part of an app that plays relaxing music in the background using the BackgroundMediaPlayer, I would like to implement a way of turning off the music after a set time..
What I was thinking was to have a TimerPicker for the user to pick when the music should stop and then add Timer as a supported task type to my MusicBackgroundTask under the Declarations in the appxmanifest.
Then properly in some way use a ThreadPoolTimer in my MusicBackgroundTask Windows Runtime Component to stop the music when it ticks, but I really cant find anywhere online that explains how to make timers in a background task work.
So if anyone have a good link that explains or better some working code it would be a great help, thank you very much..
Here is an MSDN sample that shows an IBackgroundTask implementation using a ThreadPoolTimer (see SampleBackgroundTask.cs): http://code.msdn.microsoft.com/windowsapps/Background-Task-Sample-9209ade9/sourcecode?fileId=43572&pathId=498327315
One potential reason for this not working is if you did not keep a reference to the object returned from IBackgroundTaskInstance.GetDeferral() which would cause your background task to complete prematurely after IBackgroundTask.Run returns. The sample linked above does this and I believe this is also required for the BackgroundMediaPlayer Audio task to continue playing music so I would be surprised if you didn't already have this.
Finally, the "Timer" task type in your MusicBackgroundTask declaration in the appxmanifest file will have no impact for your usage of ThreadPoolTimer. The "Timer" task type is to allow an IBackgroundTask entrypoint to be triggered by TimeTrigger and MaintenanceTrigger triggers.

Debugging Background Task Event cancellation in Win RT (c#)

I'm working on a Win Phone 8.1 App that uses the background audio player, but I'm having trouble, particularly when it comes to the cancellation event. I want to debug it myself to learn but I can't seem to find a way to manually cancel the background task other than to pause and wait 5 minutes while the app is suspended, something impossible while debugging.
Is there a way to do so cleanly (ie. Not artificially increasing memory usage)?
There come two ways to my mind, both quite easy to do:
invoke BackgroundMediaPlayer.Shutdown from your code
while debugging, hit Start, find Music app, open, start playing an audio file - hence there can be only one Instance of MediaPlayer, yours will be cancelled.

BackGround Task not working in a window phone 8 beta app

I have created a Window phone 8 app. Which changes the lockscreen image and notification in around 30 sec randomly.
My code works well in the emulator and device. When I install it through the code. My problem is that when I have submitted the app to the store as a beta app. And now I install the app from the store the app don't work as it was working. The lockscreen content don't change it remains static. I have checked the settings background agents that app is running in the background tasks but not showing the result.
What could be the cause that app is running well from the code but not from the xap uploaded to the store.
Link of app is http://www.windowsphone.com/s?appid=00d47e34-a551-4c98-b2a6-32e10babdc2f
am I missing any capability that need to fixed.
Your suggestions are welcome.
Because your title says you are using a background task and you are trying to do something every 30 secs, I assume that you're using ScheduledActionService.LaunchForTest to trigger your background task and which then performs the updating.
ScheduledActionService.LaunchForTest is only supported in DEBUG builds as a way to aid testing. It is not possible to submit an app to the store that is built in DEBUG mode so this will not work.
It is not possible to create an app which will update so often in the background. To have such an app would, potentially, drain the battery really quickly and so provide a very poor experience to users.
The best you can do is to have the background agent run on a periodic schedule (approx. every 30 mins) or trigger the updates via push notifications. (But this wouldn't work for the lock screen image.)

Categories