Accessing BackgroundMediaPlayer from another task - c#

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.

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

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.

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.

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).

Categories