Windows phone 8.1 background timer to stop music - c#

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.

Related

UWP: how to run a task without slowing down?

i'm developing an uwp app in c# capable to play midi files. I created a ThreadPoolTimer to scan a tick of 1 ms and other ThreadPool to run an async task that launch the midi messages of files relative to the time scanned by tick. It works correctly however when i do some other operation with laptop it throw a slow down behaviour. Mainly when i turn the gui in background. I changed the ThreadPool.RunAsync() with a Task.Run() and it works better but i'm trying to understand what is the best way to do a real time app with uwp because i need to implement new tasks. Additionally, i want to change base priority of my app programmatically because it works better with high priority but i don't want to change it manually everytime.
Ho can i do this tasks? i'm searching on internet but i not found any useful information about this in uwp.
Thank you
Edit: I want to use this app for play music so i need to develop it with high performance feature. For this cause i need to launch it directly with high priority level.
maybe can i develop some library

How to play an internal sound with BackgroundMediaPlayer from my Universal Windows App

I'm creating a timer in C#. When the timer finishes the user will get a notification.
If my app is gets suspended, I schedule a notification with the ToastNotificationManager and in the toast XML set the sound to an internal sound, e.g.:
<audio src='ms-winsoundevent:Notification.Looping.Alarm10' loop='true'/>
However if my app runs in the foreground I don't want to schedule a notification, but just play the sound with the BackgroundMediaPlayer.
Of course I want to use the same alarm sound, as in the notification. But I haven't found a way to address the internal sounds there.
I've found this:
BackgroundMediaPlayer.Current.SetUriSource(new Uri("ms-appx:///Assets/Sounds/alarm01.wav"));
BackgroundMediaPlayer.Current.Play();
But this would require me to copy all system sounds to my app and I don't want to do that, since it would increase the size.
I've created a demo to reproduce the problem and found that you can use the internal sound directly by using
BackgroundMediaPlayer.Current.SetUriSource(new Uri("ms-winsoundevent:Notification.Looping.Alarm10"));
BackgroundMediaPlayer.Current.Play();
It worked in my UWP Project. I hope it will work in your Project too.

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.

wp 8.1 start music from a background task

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.

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.

Categories