Debugging Background Task Event cancellation in Win RT (c#) - 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.

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

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.

Windows Phone 8 - 2 Background Audios Clash and both of the App terminates

around a week ago, I submitted an online Background Radio Streaming app for the Windows Phone store. The app was quite good (as I used the Emulator to test it, it was good on all the possible sectors) but when I submitted it for certification, it failed.
According the the error log, if someone is already playing a Music from Music + Video hub and then tries to open this app, both of the apps Crash and stop unexpectedly.
So far I understood, it is because the Music of Music + Video hub is also Background Music and for playing 2 Background Musics at the same time, the apps are Crashing. It can be some other reason but the described one seemed more logical to me.
So, is there anyone who can tell me how to change the state of the app of Music + Video hub? I want to pause or stop the app of Music + Video hub for the time being so that both of the states of the app are not same. In that way, the apps won't clash with each other in the background.
Can anyone help me in this regard?
Use gameHasControl to check for other BAP using music:
bool gameHasControl = Microsoft.Xna.Framework.Media.MediaPlayer.GameHasControl;
if (!gameHasControl)
{
MessageBox.Show("stopping other player"); // normally you should ask if to do that
BackgroundAudioPlayer.Instance.Stop();
}
Once it is Stopped, when you start your BAP, then old instance invokes Shutdown(), and your BAP will be new Instance, which you can normally use. The same is when your BAP is in memory and you start to play from Music+Video Hub.
Only watch out, because when you use XNA, you sometimes need to do:
FrameworkDispatcher.Update();
Otherwise your App will sometimes crash. Good luck.
EDIT - after comment
To make it work you need to add a reference to Microsoft.Phone.BackgroundAudio or use like this:
Microsoft.Phone.BackgroundAudio.BackgroundAudioPlayer.Instance.Stop();
BackgroundAudioPlayer is a Singleton which you are allowed to use - in this case - Stop it playing (of course give the choice to the User).

Control application execution under any condition in WP8

I'm trying to build a basic tracking app for Windows Phone 8. However I can't figure out which model to use. The app shall log the location in a definable interval (e.g. 5 sec or even 2 hours).
If I develop the app as an foreground application the tracking will stop if the user opens another program, the screen locks (you can configure the app to continue running under the lock screen), or you reveice a phone call. Overall the risk of the app not being executed is to high.
There are also background tasks that are scheduled by the OS, and are only allowed to run a few seconds or minutes depending on the current state of the phone. Is there some workaround to schedule the background task execution as defined by the user? E.g. the Email client can be set to fetch emails every 15 minutes, therefore there must be some way.
The thrid option would be using the push notification service. This however seems stupid as some third party Microsoft server has to be involved and other limitations may apply. And more importantly, the app must run to receive the notification.
What is best practise on this? I don't care about battery drain as long as the app works as wanted. Similar solutions must exists for tiles that shall be updated more frequently (30 minutes or even more seems awfully long)? Am I missing something?
A basic app that can call it's own sleep would be great, but if it get's killed after a few seconds, that won't do it. Or does sleep time not count?
I'm afraid you can't do anything to make sure about your app execution.
Standard foreground application is always less important than things like incoming phone call, so you have to assume that it can be paused or stopped at any time. You also can't force user to use your app - that's why user is able to exit the app at any time.
Background execution on Windows Phone cannot be made permanent - that's how that OS is designed. And mail application is not a good example because it's a build in app. It was made as a part of that system, so it has another set of rules (if any).
I don't really see a way to do that using Push Notifications. But even if there is one, I'm sure it's not how that feature should be used.

Categories